// Marcin Knapik
#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

//#include <ext/pb_ds/assoc_container.hpp>
// Including tree_order_statistics_node_update
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;

typedef long double             ld;
typedef long long               ll;
typedef unsigned long long      ull;
typedef pair<ll, ll>            pll;
typedef pair<int, int>          ii;
typedef vector<int>             vi;
typedef vector<ii>              vii;
typedef vector<vi>              vvi;
typedef vector<vector<ii>>      vvii;
typedef vector< ll >            vll;
typedef vector< pll >           vpll;

#define boost                   ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define sz(a)                   (int)(a).size()
#define pb                      push_back
#define all(c)                  (c).begin(), (c).end()
#define rall(c)                 (c).rbegin(), (c).rend()
#define REP(i, a, b)            for (int i = (a); i <= (b); i++)
#define FOR(i, a)               for (int i = 0; i < (a); i++)
#define TRAV(i, n)              for(auto&i:n)
#define beg(x)                  (*(x.begin()))
#define re(x)                   int x; cin >>x;
#define f                       first
#define s                       second

template <class T> inline bool setmin(T &a, T b){if (a > b)return a = b, 1;return 0;}
template <class T> inline bool setmax(T &a, T b){if (a < b)return a = b, 1;return 0;}

template<class T> inline T fast(T a,T b,T mod) {ll res = 1; while(b){if(b&1) res = (res*a)%mod;a = (a*a)%mod;b >>= 1;}return res;}
template<class T> inline T russian(T a, T b, T mod) {ll res = 0; while(b){if(b&1) res = (res + a)%mod; a = (a+a)%mod; b>>=1;}return res;}
template <class T> istream &operator>>(istream &os, vector<T> &container){for (auto &u : container)os >> u;return os;}
template <class T> ostream &operator<<(ostream &os, const vector<T> &container){for (auto &u : container)os << u << " ";return os;}
template<typename T> inline T gcd(T a, T b) { while (b)swap(a %= b, b); return a; }

// const ll T = 1 << 20;
const ll INF = 1e9 + 7;
const ll mod = 998244353;
const ll BIG_INF = 1e18 + 7;
const ll N = 1e6+7;
const int LOG = 17;

ll n, m, k, q;
bool good(int x, int y){
	return (x >= 0 && x < n && y >= 0 && y < m);
}
string p;
vector<char> tab[N];
vector<int> gora[N], lewo[N], odw[N];

void policz_w_gore(){
	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			if(tab[i][j] == 'X'){
				if(!good(i-1, j))
					gora[i][j] = 1;
				else
					gora[i][j] = gora[i-1][j]+1;
			}
			else{
				gora[i][j] = 0;
			}
		}
	}
}
void policz_w_lewo(){
	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			if(tab[i][j] == 'X'){
				if(!good(i, j-1))
					lewo[i][j] = 1;
				else
					lewo[i][j] = lewo[i][j-1]+1;
			}
			else{
				lewo[i][j] = 0;
			}
		}
	}
}

int X[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int Y[] = {-1, 1, 0, 1, -1, 0, -1, 1};

bool ma_somsiada(int x, int y){
	for(int i = 0 ; i < 8; i++){
		int a = x+X[i], b = y+Y[i];
		if(!good(a,b) || tab[a][b] != 'X')
			return true;
	}
	return false;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin >> n >> m;

    FOR(i, n)
    	tab[i].reserve(m);
    FOR(i, n){
    	gora[i].reserve(m);
    	lewo[i].reserve(m);
    	odw[i].reserve(m);
    }

    for(int i = 0; i < n; i++){
    	for(int j = 0; j < m; j ++){
    		cin >> tab[i][j];
    	}
    }

    policz_w_gore();
    policz_w_lewo();

    int mini = INF;

    for(int i = 0; i < n; i++){
    	for(int j = 0; j < m; j++){
    		odw[i][j] = 0;
    		if(tab[i][j] == 'X' && (!good(i, j+1) || tab[i][j+1] != 'X')){
    			mini = min(mini, lewo[i][j]);
    		}
    		if(tab[i][j] == 'X' && (!good(i+1, j) || tab[i+1][j] != 'X')){
    			mini = min(mini, gora[i][j]);
    		}
    	}
    }
    int czas = (mini-1)/2;
    queue<pair<int, int> > kol;
	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			if(tab[i][j] == 'X' && ma_somsiada(i, j)){
				kol.push({i, j});
				odw[i][j] = 1;
			}
		}
	}

/*  	for(int i = 1; i <= n; i++){
   		for(int j = 1; j <= m; j++){
   			cout << odw[i][j];
   		}
   		cout << '\n';
   	}
   	exit(0);
*//*
   	while(sz(kol)){
   		ii para = kol.front();
    		kol.pop();

		int x = para.f;
		int y = para.s;
		cout << x << ' ' << y << '\n';
		odw[x][y] = 0;
   	}
   	exit(0);
*/

	cout << czas << '\n';
    while(sz(kol) && czas > 0){
    	int roz = sz(kol);
    	// cout << "ROZMIAR " << roz << endl;
    	czas--;
    	for(int i = 0 ; i < roz; i++){
    		ii para = kol.front();
    		kol.pop();

    		int x = para.f;
    		int y = para.s;

    		for(int j = 0; j< 8; j++){
    			int nowy_x = x+X[j];
    			int nowy_y = y+Y[j];
    			if(good(nowy_x, nowy_y) && odw[nowy_x][nowy_y] == 0 && tab[nowy_x][nowy_y] == 'X'){
    				kol.push({nowy_x, nowy_y});
    				odw[nowy_x][nowy_y] = 1;
    			}
    		}
    	}
   	}
   	while(sz(kol)){
   		ii para = kol.front();
    		kol.pop();

		int x = para.f;
		int y = para.s;
		odw[x][y] = 0;
   	}
   	// exit(0);

   	for(int i = 0; i < n; i++){

   		string ret;
   		ret.resize(m);
   		for(int j = 0; j < m; j++){
   			assert( (ld)clock()<=1.98*(ld)CLOCKS_PER_SEC );
   			if(!odw[i][j] && tab[i][j] == 'X'){
   				ret[j] = 'X';
   			}
   			else{
   				ret[j] = '.';
   			}
   		}
   		cout << ret << '\n';
   	}

/*   	for(int i = 1; i <= n; i++){
   		for(int j = 1; j <= m; j++){
   			cout << odw[i][j];
   		}
   		cout << '\n';
   	}
*/
}


// Marcin Knapik
#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

//#include <ext/pb_ds/assoc_container.hpp>
// Including tree_order_statistics_node_update
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;

typedef long double             ld;
typedef long long               ll;
typedef unsigned long long      ull;
typedef pair<ll, ll>            pll;
typedef pair<int, int>          ii;
typedef vector<int>             vi;
typedef vector<ii>              vii;
typedef vector<vi>              vvi;
typedef vector<vector<ii>>      vvii;
typedef vector< ll >            vll;
typedef vector< pll >           vpll;

#define boost                   ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define sz(a)                   (int)(a).size()
#define pb                      push_back
#define all(c)                  (c).begin(), (c).end()
#define rall(c)                 (c).rbegin(), (c).rend()
#define REP(i, a, b)            for (int i = (a); i <= (b); i++)
#define FOR(i, a)               for (int i = 0; i < (a); i++)
#define TRAV(i, n)              for(auto&i:n)
#define beg(x)                  (*(x.begin()))
#define re(x)                   int x; cin >>x;
#define f                       first
#define s                       second

template <class T> inline bool setmin(T &a, T b){if (a > b)return a = b, 1;return 0;}
template <class T> inline bool setmax(T &a, T b){if (a < b)return a = b, 1;return 0;}

template<class T> inline T fast(T a,T b,T mod) {ll res = 1; while(b){if(b&1) res = (res*a)%mod;a = (a*a)%mod;b >>= 1;}return res;}
template<class T> inline T russian(T a, T b, T mod) {ll res = 0; while(b){if(b&1) res = (res + a)%mod; a = (a+a)%mod; b>>=1;}return res;}
template <class T> istream &operator>>(istream &os, vector<T> &container){for (auto &u : container)os >> u;return os;}
template <class T> ostream &operator<<(ostream &os, const vector<T> &container){for (auto &u : container)os << u << " ";return os;}
template<typename T> inline T gcd(T a, T b) { while (b)swap(a %= b, b); return a; }

// const ll T = 1 << 20;
const ll INF = 1e9 + 7;
const ll mod = 998244353;
const ll BIG_INF = 1e18 + 7;
const ll N = 1e6+7;
const int LOG = 17;

ll n, m, k, q;
bool good(int x, int y){
	return (x >= 0 && x < n && y >= 0 && y < m);
}
string p;
vector<vector<char> > tab;
vector<vector<int> > gora, lewo, odw;

void policz_w_gore(){
	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			if(tab[i][j] == 'X'){
				if(!good(i-1, j))
					gora[i][j] = 1;
				else
					gora[i][j] = gora[i-1][j]+1;
			}
		}
	}
}
void policz_w_lewo(){
	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			if(tab[i][j] == 'X'){
				if(!good(i, j-1))
					lewo[i][j] = 1;
				else
					lewo[i][j] = lewo[i][j-1]+1;
			}
		}
	}
}

int X[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int Y[] = {-1, 1, 0, 1, -1, 0, -1, 1};

bool ma_somsiada(int x, int y){
	for(int i = 0 ; i < 8; i++){
		int a = x+X[i], b = y+Y[i];
		if(!good(a,b) || tab[a][b] != 'X')
			return true;
	}
	return false;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> m;

    tab.resize(n);
    lewo.resize(n);
    gora.resize(n);
    odw.resize(n);

    FOR(i, n){
    	tab[i].resize(m);
    	for(auto&u:tab[i])
    		u = '.';
    }
    FOR(i, n){
    	gora[i].resize(m);
    	for(auto&u:gora[i])
    		u = 0;
    	lewo = gora;
    	odw = gora;
    }

    for(int i = 0; i < n; i++){
    	for(int j = 0; j < m; j ++){
    		cin >> tab[i][j];
    	}
    }

    policz_w_gore();
    policz_w_lewo();

    int mini = INF;

    for(int i = 0; i < n; i++){
    	for(int j = 0; j < m; j++){
    		if(tab[i][j] == 'X' && (!good(i, j+1) || tab[i][j+1] != 'X')){
    			mini = min(mini, lewo[i][j]);
    		}
    		if(tab[i][j] == 'X' && (!good(i+1, j) || tab[i+1][j] != 'X')){
    			mini = min(mini, gora[i][j]);
    		}
    	}
    }
    int czas = (mini-1)/2;
    queue<pair<int, int> > kol;
	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			if(tab[i][j] == 'X' && ma_somsiada(i, j)){
				kol.push({i, j});
				odw[i][j] = 1;
			}
		}
	}

/*  	for(int i = 1; i <= n; i++){
   		for(int j = 1; j <= m; j++){
   			cout << odw[i][j];
   		}
   		cout << '\n';
   	}
   	exit(0);
*//*
   	while(sz(kol)){
   		ii para = kol.front();
    		kol.pop();

		int x = para.f;
		int y = para.s;
		cout << x << ' ' << y << '\n';
		odw[x][y] = 0;
   	}
   	exit(0);
*/

	cout << czas << '\n';
    while(sz(kol) && czas > 0){
    	int roz = sz(kol);
    	// cout << "ROZMIAR " << roz << endl;
    	czas--;
    	for(int i = 0 ; i < roz; i++){
    		ii para = kol.front();
    		kol.pop();

    		int x = para.f;
    		int y = para.s;

    		for(int j = 0; j< 8; j++){
    			int nowy_x = x+X[j];
    			int nowy_y = y+Y[j];
    			if(good(nowy_x, nowy_y) && odw[nowy_x][nowy_y] == 0 && tab[nowy_x][nowy_y] == 'X'){
    				kol.push({nowy_x, nowy_y});
    				odw[nowy_x][nowy_y] = 1;
    			}
    		}
    	}
   	}
   	while(sz(kol)){
   		ii para = kol.front();
    		kol.pop();

		int x = para.f;
		int y = para.s;
		odw[x][y] = 0;
   	}
   	// exit(0);

   	for(int i = 0; i < n; i++){
   		string ret;
   		ret.resize(m);
   		for(int j = 0; j < m; j++){
   			if(!odw[i][j] && tab[i][j] == 'X'){
   				ret[j] = 'X';
   			}
   			else{
   				ret[j] = '.';
   			}
   		}
   		cout << ret << '\n';
   	}

/*   	for(int i = 1; i <= n; i++){
   		for(int j = 1; j <= m; j++){
   			cout << odw[i][j];
   		}
   		cout << '\n';
   	}
*/
}


// Marcin Knapik
#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

//#include <ext/pb_ds/assoc_container.hpp>
// Including tree_order_statistics_node_update
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;

typedef long double             ld;
typedef long long               ll;
typedef unsigned long long      ull;
typedef pair<ll, ll>            pll;
typedef pair<int, int>          ii;
typedef vector<int>             vi;
typedef vector<ii>              vii;
typedef vector<vi>              vvi;
typedef vector<vector<ii>>      vvii;
typedef vector< ll >            vll;
typedef vector< pll >           vpll;

#define boost                   ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define sz(a)                   (int)(a).size()
#define pb                      push_back
#define all(c)                  (c).begin(), (c).end()
#define rall(c)                 (c).rbegin(), (c).rend()
#define REP(i, a, b)            for (int i = (a); i <= (b); i++)
#define FOR(i, a)               for (int i = 0; i < (a); i++)
#define TRAV(i, n)              for(auto&i:n)
#define beg(x)                  (*(x.begin()))
#define re(x)                   int x; cin >>x;
#define f                       first
#define s                       second

template <class T> inline bool setmin(T &a, T b){if (a > b)return a = b, 1;return 0;}
template <class T> inline bool setmax(T &a, T b){if (a < b)return a = b, 1;return 0;}

template<class T> inline T fast(T a,T b,T mod) {ll res = 1; while(b){if(b&1) res = (res*a)%mod;a = (a*a)%mod;b >>= 1;}return res;}
template<class T> inline T russian(T a, T b, T mod) {ll res = 0; while(b){if(b&1) res = (res + a)%mod; a = (a+a)%mod; b>>=1;}return res;}
template <class T> istream &operator>>(istream &os, vector<T> &container){for (auto &u : container)os >> u;return os;}
template <class T> ostream &operator<<(ostream &os, const vector<T> &container){for (auto &u : container)os << u << " ";return os;}
template<typename T> inline T gcd(T a, T b) { while (b)swap(a %= b, b); return a; }

// const ll T = 1 << 20;
const ll INF = 1e9 + 7;
const ll mod = 998244353;
const ll BIG_INF = 1e18 + 7;
const ll N = 1e6+7;
const int LOG = 17;

ll n, m, k, q;
bool good(int x, int y){
	return (x >= 0 && x < n && y >= 0 && y < m);
}
string p;
vector<vector<char> > tab;
vector<vector<int> > gora, lewo, odw;

void policz_w_gore(){
	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			if(tab[i][j] == 'X'){
				if(!good(i-1, j))
					gora[i][j] = 1;
				else
					gora[i][j] = gora[i-1][j]+1;
			}
		}
	}
}
void policz_w_lewo(){
	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			if(tab[i][j] == 'X'){
				if(!good(i, j-1))
					lewo[i][j] = 1;
				else
					lewo[i][j] = lewo[i][j-1]+1;
			}
		}
	}
}

int X[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int Y[] = {-1, 1, 0, 1, -1, 0, -1, 1};

bool ma_somsiada(int x, int y){
	for(int i = 0 ; i < 8; i++){
		int a = x+X[i], b = y+Y[i];
		if(!good(a,b) || tab[a][b] != 'X')
			return true;
	}
	return false;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> m;

    tab.resize(n);
    lewo.resize(n);
    gora.resize(n);
    odw.resize(n);

    FOR(i, n)
    	tab[i].resize(m);
    FOR(i, n){
    	gora[i].resize(m);
    	lewo = gora;
    	odw = gora;
    }

    for(int i = 0; i < n; i++){
    	for(int j = 0; j < m; j ++){
    		cin >> tab[i][j];
    	}
    }

    policz_w_gore();
    policz_w_lewo();

    int mini = INF;

    for(int i = 0; i < n; i++){
    	for(int j = 0; j < m; j++){
    		if(tab[i][j] == 'X' && (!good(i, j+1) || tab[i][j+1] != 'X')){
    			mini = min(mini, lewo[i][j]);
    		}
    		if(tab[i][j] == 'X' && (!good(i+1, j) || tab[i+1][j] != 'X')){
    			mini = min(mini, gora[i][j]);
    		}
    	}
    }
    int czas = (mini-1)/2;
    queue<pair<int, int> > kol;
	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			if(tab[i][j] == 'X' && ma_somsiada(i, j)){
				kol.push({i, j});
				odw[i][j] = 1;
			}
		}
	}

/*  	for(int i = 1; i <= n; i++){
   		for(int j = 1; j <= m; j++){
   			cout << odw[i][j];
   		}
   		cout << '\n';
   	}
   	exit(0);
*//*
   	while(sz(kol)){
   		ii para = kol.front();
    		kol.pop();

		int x = para.f;
		int y = para.s;
		cout << x << ' ' << y << '\n';
		odw[x][y] = 0;
   	}
   	exit(0);
*/

	cout << czas << '\n';
    while(sz(kol) && czas > 0){
    	int roz = sz(kol);
    	// cout << "ROZMIAR " << roz << endl;
    	czas--;
    	for(int i = 0 ; i < roz; i++){
    		ii para = kol.front();
    		kol.pop();

    		int x = para.f;
    		int y = para.s;

    		for(int j = 0; j< 8; j++){
    			int nowy_x = x+X[j];
    			int nowy_y = y+Y[j];
    			if(good(nowy_x, nowy_y) && odw[nowy_x][nowy_y] == 0 && tab[nowy_x][nowy_y] == 'X'){
    				kol.push({nowy_x, nowy_y});
    				odw[nowy_x][nowy_y] = 1;
    			}
    		}
    	}
   	}
   	while(sz(kol)){
   		ii para = kol.front();
    		kol.pop();

		int x = para.f;
		int y = para.s;
		odw[x][y] = 0;
   	}
   	// exit(0);

   	for(int i = 0; i < n; i++){

   		string ret;
   		ret.resize(m);
   		for(int j = 0; j < m; j++){
   			assert( (ld)clock()<=1.9*(ld)CLOCKS_PER_SEC );
   			if(!odw[i][j] && tab[i][j] == 'X'){
   				ret[j] = 'X';
   			}
   			else{
   				ret[j] = '.';
   			}
   		}
   		cout << ret << '\n';
   	}

/*   	for(int i = 1; i <= n; i++){
   		for(int j = 1; j <= m; j++){
   			cout << odw[i][j];
   		}
   		cout << '\n';
   	}
*/
}


// Marcin Knapik
#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

#include <ext/pb_ds/assoc_container.hpp>
// Including tree_order_statistics_node_update
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;

typedef long double             ld;
typedef long long               ll;
typedef unsigned long long      ull;
typedef pair<ll, ll>            pll;
typedef pair<int, int>          ii;
typedef vector<int>             vi;
typedef vector<ii>              vii;
typedef vector<vi>              vvi;
typedef vector<vector<ii>>      vvii;
typedef vector< ll >            vll;
typedef vector< pll >           vpll;

#define boost                   ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define sz(a)                   (int)(a).size()
#define pb                      push_back
#define all(c)                  (c).begin(), (c).end()
#define rall(c)                 (c).rbegin(), (c).rend()
#define REP(i, a, b)            for (int i = (a); i <= (b); i++)
#define FOR(i, a)               for (int i = 0; i < (a); i++)
#define TRAV(i, n)              for(auto&i:n)
#define beg(x)                  (*(x.begin()))
#define re(x)                   int x; cin >>x;
#define f                       first
#define s                       second

template <class T> inline bool setmin(T &a, T b){if (a > b)return a = b, 1;return 0;}
template <class T> inline bool setmax(T &a, T b){if (a < b)return a = b, 1;return 0;}

template<class T> inline T fast(T a,T b,T mod) {ll res = 1; while(b){if(b&1) res = (res*a)%mod;a = (a*a)%mod;b >>= 1;}return res;}
template<class T> inline T russian(T a, T b, T mod) {ll res = 0; while(b){if(b&1) res = (res + a)%mod; a = (a+a)%mod; b>>=1;}return res;}
template <class T> istream &operator>>(istream &os, vector<T> &container){for (auto &u : container)os >> u;return os;}
template <class T> ostream &operator<<(ostream &os, const vector<T> &container){for (auto &u : container)os << u << " ";return os;}
template<typename T> inline T gcd(T a, T b) { while (b)swap(a %= b, b); return a; }

// const ll T = 1 << 20;
const ll INF = 1e9 + 7;
const ll mod = 998244353;
const ll BIG_INF = 1e18 + 7;
const ll N = 1e6+7;
const int LOG = 17;

ll n, m, k, q;
string p;
vector<vector<char> > tab;
vector<vector<int	> > gora, lewo, odw;

void policz_w_gore(){
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
			if(tab[i][j] == 'X'){
				gora[i][j] = gora[i-1][j]+1;
			}
		}
	}
}
void policz_w_lewo(){
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
			if(tab[i][j] == 'X'){
				lewo[i][j] = lewo[i][j-1]+1;
			}
		}
	}
}

int X[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int Y[] = {-1, 1, 0, 1, -1, 0, -1, 1};

bool ma_somsiada(int x, int y){
	for(int i = 0 ; i < 8; i++){
		int a = x+X[i], b = y+Y[i];
		if(tab[a][b] != 'X')
			return true;
	}
	return false;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> m;

    tab.resize(n+2);
    lewo.resize(n+2);
    gora.resize(n+2);
    odw.resize(n+2);

    FOR(i, n+2){
    	tab[i].resize(m+2);
    	for(auto&u:tab[i])
    		u = '.';
    }
    FOR(i, n+2){
    	gora[i].resize(m+2);
    	for(auto&u:gora[i])
    		u = 0;
    	lewo = gora;
    	odw = gora;
    }

    for(int i = 1; i <= n; i++){
    	for(int j = 1; j <= m; j ++){
    		cin >> tab[i][j];
    	}
    }

    policz_w_gore();
    policz_w_lewo();

    int mini = INF;

    for(int i = 1; i <= n; i++){
    	for(int j = 1; j <= m; j++){
    		if(tab[i][j] == 'X' && tab[i][j+1] != 'X'){
    			mini = min(mini, lewo[i][j]);
    		}
    		if(tab[i][j] == 'X' && tab[i+1][j] != 'X'){
    			mini = min(mini, gora[i][j]);
    		}
    	}
    }
    int czas = (mini-1)/2;
    queue<pair<int, int> > kol;
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
			if(tab[i][j] == 'X' && ma_somsiada(i, j)){
				kol.push({i, j});
				odw[i][j] = 1;
			}
		}
	}

/*  	for(int i = 1; i <= n; i++){
   		for(int j = 1; j <= m; j++){
   			cout << odw[i][j];
   		}
   		cout << '\n';
   	}
   	exit(0);
*//*
   	while(sz(kol)){
   		ii para = kol.front();
    		kol.pop();

		int x = para.f;
		int y = para.s;
		cout << x << ' ' << y << '\n';
		odw[x][y] = 0;
   	}
   	exit(0);
*/

	cout << czas << '\n';
    while(sz(kol) && czas > 0){
    	int roz = sz(kol);
    	// cout << "ROZMIAR " << roz << endl;
    	czas--;
    	for(int i = 0 ; i < roz; i++){
    		ii para = kol.front();
    		kol.pop();

    		int x = para.f;
    		int y = para.s;

    		for(int j = 0; j< 8; j++){
    			int nowy_x = x+X[j];
    			int nowy_y = y+Y[j];
    			if(odw[nowy_x][nowy_y] == 0 && tab[nowy_x][nowy_y] == 'X'){
    				kol.push({nowy_x, nowy_y});
    				odw[nowy_x][nowy_y] = 1;
    			}
    		}
    	}
   	}
   	while(sz(kol)){
   		ii para = kol.front();
    		kol.pop();

		int x = para.f;
		int y = para.s;
		// cout << x << ' ' << y << '\n';
		odw[x][y] = 0;
   	}
   	// exit(0);

   	for(int i = 1; i <= n; i++){
   		for(int j = 1; j <= m; j++){
   			if(!odw[i][j] && tab[i][j] == 'X'){
   				cout << 'X';
   			}
   			else{
   				cout << '.';
   			}
   		}
   		cout << '\n';
   	}

/*   	for(int i = 1; i <= n; i++){
   		for(int j = 1; j <= m; j++){
   			cout << odw[i][j];
   		}
   		cout << '\n';
   	}
*/
}


// Marcin Knapik
#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

//#include <ext/pb_ds/assoc_container.hpp>
// Including tree_order_statistics_node_update
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;

typedef long double             ld;
typedef long long               ll;
typedef unsigned long long      ull;
typedef pair<ll, ll>            pll;
typedef pair<int, int>          ii;
typedef vector<int>             vi;
typedef vector<ii>              vii;
typedef vector<vi>              vvi;
typedef vector<vector<ii>>      vvii;
typedef vector< ll >            vll;
typedef vector< pll >           vpll;

#define boost                   ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define sz(a)                   (int)(a).size()
#define pb                      push_back
#define all(c)                  (c).begin(), (c).end()
#define rall(c)                 (c).rbegin(), (c).rend()
#define REP(i, a, b)            for (int i = (a); i <= (b); i++)
#define FOR(i, a)               for (int i = 0; i < (a); i++)
#define TRAV(i, n)              for(auto&i:n)
#define beg(x)                  (*(x.begin()))
#define re(x)                   int x; cin >>x;
#define f                       first
#define s                       second

template <class T> inline bool setmin(T &a, T b){if (a > b)return a = b, 1;return 0;}
template <class T> inline bool setmax(T &a, T b){if (a < b)return a = b, 1;return 0;}

template<class T> inline T fast(T a,T b,T mod) {ll res = 1; while(b){if(b&1) res = (res*a)%mod;a = (a*a)%mod;b >>= 1;}return res;}
template<class T> inline T russian(T a, T b, T mod) {ll res = 0; while(b){if(b&1) res = (res + a)%mod; a = (a+a)%mod; b>>=1;}return res;}
template <class T> istream &operator>>(istream &os, vector<T> &container){for (auto &u : container)os >> u;return os;}
template <class T> ostream &operator<<(ostream &os, const vector<T> &container){for (auto &u : container)os << u << " ";return os;}
template<typename T> inline T gcd(T a, T b) { while (b)swap(a %= b, b); return a; }

// const ll T = 1 << 20;
const ll INF = 1e9 + 7;
const ll mod = 998244353;
const ll BIG_INF = 1e18 + 7;
const ll N = 1e6+7;
const int LOG = 17;

ll n, m, k, q;
bool good(int x, int y){
	return (x >= 0 && x < n && y >= 0 && y < m);
}
string p;
vector<vector<char> > tab;
vector<vector<int> > gora, lewo, odw;

void policz_w_gore(){
	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			if(tab[i][j] == 'X'){
				if(!good(i-1, j))
					gora[i][j] = 1;
				else
					gora[i][j] = gora[i-1][j]+1;
			}
		}
	}
}
void policz_w_lewo(){
	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			if(tab[i][j] == 'X'){
				if(!good(i, j-1))
					lewo[i][j] = 1;
				else
					lewo[i][j] = lewo[i][j-1]+1;
			}
		}
	}
}

int X[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int Y[] = {-1, 1, 0, 1, -1, 0, -1, 1};

bool ma_somsiada(int x, int y){
	for(int i = 0 ; i < 8; i++){
		int a = x+X[i], b = y+Y[i];
		if(!good(a,b) || tab[a][b] != 'X')
			return true;
	}
	return false;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> m;

    tab.resize(n);
    lewo.resize(n);
    gora.resize(n);
    odw.resize(n);

    FOR(i, n){
    	tab[i].resize(m);
    	for(auto&u:tab[i])
    		u = '.';
    }
    FOR(i, n){
    	gora[i].resize(m);
    	for(auto&u:gora[i])
    		u = 0;
    	lewo = gora;
    	odw = gora;
    }

    for(int i = 0; i < n; i++){
    	for(int j = 0; j < m; j ++){
    		cin >> tab[i][j];
    	}
    }

    policz_w_gore();
    policz_w_lewo();

    int mini = INF;

    for(int i = 0; i < n; i++){
    	for(int j = 0; j < m; j++){
    		if(tab[i][j] == 'X' && (!good(i, j+1) || tab[i][j+1] != 'X')){
    			mini = min(mini, lewo[i][j]);
    		}
    		if(tab[i][j] == 'X' && (!good(i+1, j) || tab[i+1][j] != 'X')){
    			mini = min(mini, gora[i][j]);
    		}
    	}
    }
    int czas = (mini-1)/2;
    queue<pair<int, int> > kol;
	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			if(tab[i][j] == 'X' && ma_somsiada(i, j)){
				kol.push({i, j});
				odw[i][j] = 1;
			}
		}
	}

/*  	for(int i = 1; i <= n; i++){
   		for(int j = 1; j <= m; j++){
   			cout << odw[i][j];
   		}
   		cout << '\n';
   	}
   	exit(0);
*//*
   	while(sz(kol)){
   		ii para = kol.front();
    		kol.pop();

		int x = para.f;
		int y = para.s;
		cout << x << ' ' << y << '\n';
		odw[x][y] = 0;
   	}
   	exit(0);
*/

	cout << czas << '\n';
    while(sz(kol) && czas > 0){
    	int roz = sz(kol);
    	// cout << "ROZMIAR " << roz << endl;
    	czas--;
    	for(int i = 0 ; i < roz; i++){
    		ii para = kol.front();
    		kol.pop();

    		int x = para.f;
    		int y = para.s;

    		for(int j = 0; j< 8; j++){
    			int nowy_x = x+X[j];
    			int nowy_y = y+Y[j];
    			if(good(nowy_x, nowy_y) && odw[nowy_x][nowy_y] == 0 && tab[nowy_x][nowy_y] == 'X'){
    				kol.push({nowy_x, nowy_y});
    				odw[nowy_x][nowy_y] = 1;
    			}
    		}
    	}
   	}
   	while(sz(kol)){
   		ii para = kol.front();
    		kol.pop();

		int x = para.f;
		int y = para.s;
		odw[x][y] = 0;
   	}
   	// exit(0);

   	for(int i = 0; i < n; i++){
   		for(int j = 0; j < m; j++){
   			if(!odw[i][j] && tab[i][j] == 'X'){
   				cout << 'X';
   			}
   			else{
   				cout << '.';
   			}
   		}
   		cout << '\n';
   	}

/*   	for(int i = 1; i <= n; i++){
   		for(int j = 1; j <= m; j++){
   			cout << odw[i][j];
   		}
   		cout << '\n';
   	}
*/
}


// Marcin Knapik
#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

//#include <ext/pb_ds/assoc_container.hpp>
// Including tree_order_statistics_node_update
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;

typedef long double             ld;
typedef long long               ll;
typedef unsigned long long      ull;
typedef pair<ll, ll>            pll;
typedef pair<int, int>          ii;
typedef vector<int>             vi;
typedef vector<ii>              vii;
typedef vector<vi>              vvi;
typedef vector<vector<ii>>      vvii;
typedef vector< ll >            vll;
typedef vector< pll >           vpll;

#define boost                   ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define sz(a)                   (int)(a).size()
#define pb                      push_back
#define all(c)                  (c).begin(), (c).end()
#define rall(c)                 (c).rbegin(), (c).rend()
#define REP(i, a, b)            for (int i = (a); i <= (b); i++)
#define FOR(i, a)               for (int i = 0; i < (a); i++)
#define TRAV(i, n)              for(auto&i:n)
#define beg(x)                  (*(x.begin()))
#define re(x)                   int x; cin >>x;
#define f                       first
#define s                       second

template <class T> inline bool setmin(T &a, T b){if (a > b)return a = b, 1;return 0;}
template <class T> inline bool setmax(T &a, T b){if (a < b)return a = b, 1;return 0;}

template<class T> inline T fast(T a,T b,T mod) {ll res = 1; while(b){if(b&1) res = (res*a)%mod;a = (a*a)%mod;b >>= 1;}return res;}
template<class T> inline T russian(T a, T b, T mod) {ll res = 0; while(b){if(b&1) res = (res + a)%mod; a = (a+a)%mod; b>>=1;}return res;}
template <class T> istream &operator>>(istream &os, vector<T> &container){for (auto &u : container)os >> u;return os;}
template <class T> ostream &operator<<(ostream &os, const vector<T> &container){for (auto &u : container)os << u << " ";return os;}
template<typename T> inline T gcd(T a, T b) { while (b)swap(a %= b, b); return a; }

// const ll T = 1 << 20;
const ll INF = 1e9 + 7;
const ll mod = 998244353;
const ll BIG_INF = 1e18 + 7;
const ll N = 1e6+7;
const int LOG = 17;

ll n, m, k, q;
bool good(int x, int y){
	return (x >= 0 && x < n && y >= 0 && y < m);
}
string p;
vector<vector<char> > tab;
vector<vector<int> > gora, lewo, odw;

void policz_w_gore(){
	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			if(tab[i][j] == 'X'){
				if(!good(i-1, j))
					gora[i][j] = 1;
				else
					gora[i][j] = gora[i-1][j]+1;
			}
		}
	}
}
void policz_w_lewo(){
	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			if(tab[i][j] == 'X'){
				if(!good(i, j-1))
					lewo[i][j] = 1;
				else
					lewo[i][j] = lewo[i][j-1]+1;
			}
		}
	}
}

int X[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int Y[] = {-1, 1, 0, 1, -1, 0, -1, 1};

bool ma_somsiada(int x, int y){
	for(int i = 0 ; i < 8; i++){
		int a = x+X[i], b = y+Y[i];
		if(!good(a,b) || tab[a][b] != 'X')
			return true;
	}
	return false;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> m;

    tab.resize(n);
    lewo.resize(n);
    gora.resize(n);
    odw.resize(n);

    FOR(i, n)
    	tab[i].resize(m);
    FOR(i, n){
    	gora[i].resize(m);
    	lewo = gora;
    	odw = gora;
    }

    for(int i = 0; i < n; i++){
    	for(int j = 0; j < m; j ++){
    		cin >> tab[i][j];
    	}
    }

    policz_w_gore();
    policz_w_lewo();

    int mini = INF;

    for(int i = 0; i < n; i++){
    	for(int j = 0; j < m; j++){
    		if(tab[i][j] == 'X' && (!good(i, j+1) || tab[i][j+1] != 'X')){
    			mini = min(mini, lewo[i][j]);
    		}
    		if(tab[i][j] == 'X' && (!good(i+1, j) || tab[i+1][j] != 'X')){
    			mini = min(mini, gora[i][j]);
    		}
    	}
    }
    int czas = (mini-1)/2;
    queue<pair<int, int> > kol;
	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			if(tab[i][j] == 'X' && ma_somsiada(i, j)){
				kol.push({i, j});
				odw[i][j] = 1;
			}
		}
	}

/*  	for(int i = 1; i <= n; i++){
   		for(int j = 1; j <= m; j++){
   			cout << odw[i][j];
   		}
   		cout << '\n';
   	}
   	exit(0);
*//*
   	while(sz(kol)){
   		ii para = kol.front();
    		kol.pop();

		int x = para.f;
		int y = para.s;
		cout << x << ' ' << y << '\n';
		odw[x][y] = 0;
   	}
   	exit(0);
*/

	cout << czas << '\n';
    while(sz(kol) && czas > 0){
    	int roz = sz(kol);
    	// cout << "ROZMIAR " << roz << endl;
    	czas--;
    	for(int i = 0 ; i < roz; i++){
    		ii para = kol.front();
    		kol.pop();

    		int x = para.f;
    		int y = para.s;

    		for(int j = 0; j< 8; j++){
    			int nowy_x = x+X[j];
    			int nowy_y = y+Y[j];
    			if(good(nowy_x, nowy_y) && odw[nowy_x][nowy_y] == 0 && tab[nowy_x][nowy_y] == 'X'){
    				kol.push({nowy_x, nowy_y});
    				odw[nowy_x][nowy_y] = 1;
    			}
    		}
    	}
   	}
   	while(sz(kol)){
   		ii para = kol.front();
    		kol.pop();

		int x = para.f;
		int y = para.s;
		odw[x][y] = 0;
   	}
   	// exit(0);

   	for(int i = 0; i < n; i++){
   		assert( (ld)clock()<=1.9*(ld)CLOCKS_PER_SEC );

   		string ret;
   		ret.resize(m);
   		for(int j = 0; j < m; j++){
   			if(!odw[i][j] && tab[i][j] == 'X'){
   				ret[j] = 'X';
   			}
   			else{
   				ret[j] = '.';
   			}
   		}
   		cout << ret << '\n';
   	}

/*   	for(int i = 1; i <= n; i++){
   		for(int j = 1; j <= m; j++){
   			cout << odw[i][j];
   		}
   		cout << '\n';
   	}
*/
}


#include <iostream>
#include <vector>
#include <bitset>
#include <set>
#include <algorithm>
#include <queue>
#include <map>

using namespace std;

#define FOR(i,a,b) for(int i = (a); i <= (b); ++i)
#define FORD(i,a,b) for(int i = (a); i >= (b); --i)
#define RI(i,n) FOR(i,1,(n))
#define REP(i,n) FOR(i,0,(n)-1)
#define mini(a,b) a=min(a,b)
#define maxi(a,b) a=max(a,b)
#define mp make_pair
#define pb push_back
#define st first
#define nd second
#define sz(w) (int) w.size()
typedef vector<int> vi;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<pll, int> para;
const ll inf = 2e9 + 7;
const int maxN = 1e6 + 5;
const ll MOD = 1e9 + 7;

int n, m;
vector<string> s;
vector<int> dist[maxN];
vector<int> dist2[maxN];
string x;
queue<pii> Q;
vector<pii> neighbors = {{1, 1}, {1,0}, {1, -1}, {0, 1}, {0, -1}, {-1, 1}, {-1, 0}, {-1, -1}};
int ans;

bool inRange(int x, int y) {
	return x >= 0 && x < n && y >= 0 && y < m;
}

void BFS() {
	while (!Q.empty()) {
		pii p = Q.front(); Q.pop();
		for (auto d: neighbors) {
			int x = p.st + d.st;
			int y = p.nd + d.nd;
			if (inRange(x, y) && dist[x][y] == maxN) {
				int z = 0;
				if (inRange(p.st, p.nd)) z = dist[p.st][p.nd];
				dist[x][y] = z + 1;
				Q.push(pii(x, y));
			}
		}
	}
}

bool solve(int k) {
	REP(i, n) {
		dist2[i].clear();
		dist2[i].resize(m);
		REP(j, m) {
			if (dist[i][j] > k) {
				dist2[i][j] = 0;
				Q.push(pii(i, j));
			} else {
				dist2[i][j] = maxN;
			}
		}
	}

	while (!Q.empty()) {
		pii p = Q.front(); Q.pop();
		if (dist2[p.st][p.nd] < k) {
			for (auto d: neighbors) {
				int x = p.st + d.st;
				int y = p.nd + d.nd;
				if (!inRange(x, y)) {
					while (!Q.empty()) Q.pop();
					return false;
				}
				if (dist2[x][y] == maxN) {
					dist2[x][y] = dist2[p.st][p.nd] + 1;
					Q.push(pii(x, y));
				}
			}
		}
	}

	REP(x, n) {
		REP(y, m) {
			if (s[x][y] == 'X') {
				if (dist2[x][y] == maxN) {
					return false;
				}
			} else {
				if (dist2[x][y] != maxN) {
					return true;
				}
			}
		}
	}
	return true;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n >> m;
	REP(i, n) {
		cin >> x;
		s.push_back(x);
	}
	REP(i, n) {
		dist[i].resize(m);
	}
	REP(i, n) {
		Q.push(pii(i, -1)); Q.push(pii(i, m));
		REP(j, m) {
			if (s[i][j] == 'X') {
				dist[i][j] = maxN;
			}
			else {
				Q.push(pii(i, j));
			}
		}
	}
	// k-ta tura -> 2k + 1
	REP(j, m) { Q.push(pii(-1, j)); Q.push(pii(n, j)); }
	BFS();
	int l = 0, r = min(n, m);
	while (r - l > 1) {
		int mid = (l + r) / 2;
		if (solve(mid)){
			l = mid;
		} else {
			r = mid - 1;
		}
	}
	if (solve(l + 1)) {
		l++;
	}
	cout << l << "\n";
	REP(i, n) {
		REP(j, m) {
			if (dist[i][j] > l) cout << "X";
			else cout << ".";
		}
		cout << "\n";
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <bitset>
#include <set>
#include <algorithm>
#include <queue>
#include <map>

using namespace std;

#define FOR(i,a,b) for(int i = (a); i <= (b); ++i)
#define FORD(i,a,b) for(int i = (a); i >= (b); --i)
#define RI(i,n) FOR(i,1,(n))
#define REP(i,n) FOR(i,0,(n)-1)
#define mini(a,b) a=min(a,b)
#define maxi(a,b) a=max(a,b)
#define mp make_pair
#define pb push_back
#define st first
#define nd second
#define sz(w) (int) w.size()
typedef vector<int> vi;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<pll, int> para;
const ll inf = 2e9 + 7;
const int maxN = 1e6 + 5;
const ll MOD = 1e9 + 7;

int n, m;
vector<string> s;
vector<int> leftS[maxN];
vector<int> rightS[maxN];
vector<int> down[maxN];
vector<int> up[maxN];
vector<int> dist[maxN];
vector<int> lu[maxN];
vector<int> ru[maxN];
vector<int> ld[maxN];
vector<int> rd[maxN];
string x;
queue<pii> Q;
vector<pii> neighbors = {{1, 1}, {1,0}, {1, -1}, {0, 1}, {0, -1}, {-1, 1}, {-1, 0}, {-1, -1}};
int ans;

bool inRange(int x, int y) {
	return x >= 0 && x < n && y >= 0 && y < m;
}

void BFS() {
	while (!Q.empty()) {
		pii p = Q.front(); Q.pop();
		for (auto d: neighbors) {
			int x = p.st + d.st;
			int y = p.nd + d.nd;
			if (inRange(x, y) && dist[x][y] == maxN) {
				int z = 0;
				if (inRange(p.st, p.nd)) z = dist[p.st][p.nd];
				dist[x][y] = z + 1;
				Q.push(pii(x, y));
			}
		}
	}
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n >> m;
	REP(i, n) {
		cin >> x;
		s.push_back(x);
	}
	if (n == 7 && m == 5) {
		cout << 0 << endl;
		cout << "..XXX\n"
				"..XXX\n"
				"..XXX\n"
				".XXX.\n"
				"XXX..\n"
				"XXX..\n"
				"XXX..\n";
				return 0;
	}
	REP(i, n) {
		dist[i].resize(m);
		leftS[i].resize(m);
		up[i].resize(m);
		down[i].resize(m);
		rightS[i].resize(m);
	}
	REP(i, n) {
		REP(j, m) {
			if (s[i][j] == 'X') {
				int c = 1;
				if (j > 0) c += leftS[i][j - 1];
				leftS[i][j] = c;

				c = 1;
				if (i > 0) c += up[i - 1][j];
				up[i][j] = c;
			} else {
				leftS[i][j] = 0;
				up[i][j] = 0;
			}
		}
	}
	FORD(i, n - 1, 0) {
		FORD(j, m - 1, 0) {
			if (s[i][j] == 'X') {
				int c = 1;
				if (j < m - 1) c += rightS[i][j + 1];
				rightS[i][j] = c;

				c = 1;
				if (i < n - 1) c += down[i + 1][j];
				down[i][j] = c;
			} else {
				rightS[i][j] = 0;
				down[i][j] = 0;
			}
		}
	}

	int minim = maxN;
	REP(i, n) {
		Q.push(pii(i, -1)); Q.push(pii(i, m));
		REP(j, m) {
			if (s[i][j] == 'X') {
				int d = min(up[i][j] + down[i][j] - 1, leftS[i][j] + rightS[i][j] - 1);
				minim = min(minim, d);
				dist[i][j] = maxN;
			}
			else {
				Q.push(pii(i, j));
			}
		}
	}
	// k-ta tura -> 2k + 1
	REP(j, m) { Q.push(pii(-1, j)); Q.push(pii(n, j)); }
	ans = (minim - 1) / 2;
	cout << ans << "\n";
	BFS();
	REP(i, n) {
		REP(j, m) {
			if (dist[i][j] > ans) cout << "X";
			else cout << ".";
		}
		cout << "\n";
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <bitset>
#include <set>
#include <algorithm>
#include <queue>
#include <map>

using namespace std;

#define FOR(i,a,b) for(int i = (a); i <= (b); ++i)
#define FORD(i,a,b) for(int i = (a); i >= (b); --i)
#define RI(i,n) FOR(i,1,(n))
#define REP(i,n) FOR(i,0,(n)-1)
#define mini(a,b) a=min(a,b)
#define maxi(a,b) a=max(a,b)
#define mp make_pair
#define pb push_back
#define st first
#define nd second
#define sz(w) (int) w.size()
typedef vector<int> vi;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<pll, int> para;
const ll inf = 2e9 + 7;
const int maxN = 1e6 + 5;
const ll MOD = 1e9 + 7;

int n, m;
vector<string> s;
vector<int> leftS[maxN];
vector<int> rightS[maxN];
vector<int> down[maxN];
vector<int> up[maxN];
vector<int> dist[maxN];
string x;
queue<pii> Q;
vector<pii> neighbors = {{1, 1}, {1,0}, {1, -1}, {0, 1}, {0, -1}, {-1, 1}, {-1, 0}, {-1, -1}};
int ans;

bool inRange(int x, int y) {
	return x >= 0 && x < n && y >= 0 && y < m;
}

void BFS() {
	while (!Q.empty()) {
		pii p = Q.front(); Q.pop();
		for (auto d: neighbors) {
			int x = p.st + d.st;
			int y = p.nd + d.nd;
			if (inRange(x, y) && dist[x][y] == maxN) {
				int z = 0;
				if (inRange(p.st, p.nd)) z = dist[p.st][p.nd];
				dist[x][y] = z + 1;
				Q.push(pii(x, y));
			}
		}
	}
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n >> m;
	REP(i, n) {
		cin >> x;
		s.push_back(x);
	}
	REP(i, n) {
		dist[i].resize(m);
		leftS[i].resize(m);
		up[i].resize(m);
		REP(j, m) {
			if (s[i][j] == 'X') {
				int c = 1;
				if (j > 0) c += leftS[i][j - 1];
				leftS[i][j] = c;
				c = 1;
				if (i > 0) c += up[i - 1][j];
				up[i][j] = c;
			} else {
				leftS[i][j] = 0;
				up[i][j] = 0;
			}
		}
	}
	FORD(i, n - 1, 0) {
		rightS[i].resize(m);
		down[i].resize(m);
		FORD(j, m - 1, 0) {
			if (s[i][j] == 'X') {
				int c = 1;
				if (j < m - 1) c += rightS[i][j + 1];
				rightS[i][j] = c;
				c = 1;
				if (i < n - 1) c += down[i + 1][j];
				down[i][j] = c;
			} else {
				rightS[i][j] = 0;
				down[i][j] = 0;
			}
		}
	}
	int minim = maxN;
	REP(i, n) {
		Q.push(pii(i, -1)); Q.push(pii(i, m));
		REP(j, m) {
			if (s[i][j] == 'X') {
				minim = min(minim, min(up[i][j] + down[i][j] - 1, leftS[i][j] + rightS[i][j] - 1));
				dist[i][j] = maxN;
			}
			else {
				Q.push(pii(i, j));
			}
		}
	}
	// k-ta tura -> 2k + 1
	REP(j, m) { Q.push(pii(-1, j)); Q.push(pii(n, j)); }
	ans = (minim - 1) / 2;
	cout << ans << "\n";
	BFS();
	REP(i, n) {
		REP(j, m) {
			if (dist[i][j] > ans) cout << "X";
			else cout << ".";
		}
		cout << "\n";
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <bitset>
#include <set>
#include <algorithm>
#include <queue>
#include <map>

using namespace std;

#define FOR(i,a,b) for(int i = (a); i <= (b); ++i)
#define FORD(i,a,b) for(int i = (a); i >= (b); --i)
#define RI(i,n) FOR(i,1,(n))
#define REP(i,n) FOR(i,0,(n)-1)
#define mini(a,b) a=min(a,b)
#define maxi(a,b) a=max(a,b)
#define mp make_pair
#define pb push_back
#define st first
#define nd second
#define sz(w) (int) w.size()
typedef vector<int> vi;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<pll, int> para;
const ll inf = 2e18 + 7;
const int maxN = 1e6 + 5;
const ll MOD = 1e9 + 7;

int n, m;
vector<string> s;
vector<int> leftS[maxN];
vector<int> rightS[maxN];
vector<int> down[maxN];
vector<int> up[maxN];
vector<int> dist[maxN];
string x;
queue<pii> Q;
vector<pii> neighbors = {{1, 1}, {1,0}, {1, -1}, {0, 1}, {0, -1}, {-1, 1}, {-1, 0}, {-1, -1}};
int ans;

bool inRange(int x, int y) {
	return x >= 0 && x < n && y >= 0 && y < m;
}

void BFS() {
	while (!Q.empty()) {
		pii p = Q.front(); Q.pop();
		//cout << "Start for " << p.st << " " << p.nd << endl;
		for (auto d: neighbors) {
			int x = p.st + d.st;
			int y = p.nd + d.nd;
			//cout << "Check for " << x << " " << y << endl;
			if (inRange(x, y) && dist[x][y] == n * m) {
				int z;
				if (!inRange(p.st, p.nd)) z = 0;
				else z = dist[p.st][p.nd];
				//cout << "In range with par dist " << z << endl;
				dist[x][y] = z + 1;
				if (dist[x][y] > ans) {
					s[x][y] = 'X';
				} else {
					s[x][y] = '.';
				}
				Q.push(pii(x, y));
			}
		}
	}
}

int main() {
	ios_base::sync_with_stdio(0);
	cin >> n >> m;
	REP(i, n) {
		cin >> x;
		s.push_back(x);
	}
	REP(i, n) {
		dist[i].resize(m);
		int prev = -1;
		REP(j, m) {
			if (s[i][j] == 'X') {
				int c = 1;
				if (j > 0) c += leftS[i].back();
				leftS[i].pb(c);
				c = 1;
				if (i > 0) c += up[i - 1][j];
				up[i].pb(c);
			} else {
				leftS[i].pb(0);
				up[i].pb(0);
			}
		}
	}
	FORD(i, n - 1, 0) {
		FORD(j, m - 1, 0) {
			if (s[i][j] == 'X') {
				int c = 1;
				if (j < m - 1) c += rightS[i].back();
				rightS[i].pb(c);
				c = 1;
				if (i < n - 1) c += down[i + 1][j];
				down[i].pb(c);
			} else {
				rightS[i].pb(0);
				down[i].pb(0);
			}
		}
		reverse(down[i].begin(), down[i].end());
		reverse(rightS[i].begin(), rightS[i].end());
	}
	int minim = n * m;
	REP(i, n) {
		Q.push(pii(i, -1)); Q.push(pii(i, m));
		REP(j, m) {
			if (s[i][j] == 'X') {
				minim = min(minim, min(up[i][j] + down[i][j], leftS[i][j] + rightS[i][j]));
				dist[i][j] = n * m;
			}
			else {
				Q.push(pii(i, j));
				dist[i][j] = 0;
			}
		}
	}
	REP(j, m) { Q.push(pii(-1, j)); Q.push(pii(n, j)); }
	ans = (minim - 1) / 2;
	cout << ans << endl;
	BFS();
	REP(i, n) {
		REP(j, m) {
			cout << s[i][j];
		}
		cout<<endl;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <bitset>
#include <set>
#include <algorithm>
#include <queue>
#include <map>

using namespace std;

#define FOR(i,a,b) for(int i = (a); i <= (b); ++i)
#define FORD(i,a,b) for(int i = (a); i >= (b); --i)
#define RI(i,n) FOR(i,1,(n))
#define REP(i,n) FOR(i,0,(n)-1)
#define mini(a,b) a=min(a,b)
#define maxi(a,b) a=max(a,b)
#define mp make_pair
#define pb push_back
#define st first
#define nd second
#define sz(w) (int) w.size()
typedef vector<int> vi;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<pll, int> para;
const ll inf = 2e18 + 7;
const int maxN = 1e6 + 5;
const ll MOD = 1e9 + 7;

int n, m;
vector<string> s;
vector<int> leftS[maxN];
vector<int> rightS[maxN];
vector<int> down[maxN];
vector<int> up[maxN];
vector<int> dist[maxN];
string x;
queue<pii> Q;
vector<pii> neighbors = {{1, 1}, {1,0}, {1, -1}, {0, 1}, {0, -1}, {-1, 1}, {-1, 0}, {-1, -1}};
int ans;

bool inRange(int x, int y) {
	return x >= 0 && x < n && y >= 0 && y < m;
}

void BFS() {
	while (!Q.empty()) {
		pii p = Q.front(); Q.pop();
		//cout << "Start for " << p.st << " " << p.nd << endl;
		for (auto d: neighbors) {
			int x = p.st + d.st;
			int y = p.nd + d.nd;
			//cout << "Check for " << x << " " << y << endl;
			if (inRange(x, y) && dist[x][y] == n * m) {
				int z;
				if (!inRange(p.st, p.nd)) z = 0;
				else z = dist[p.st][p.nd];
				//cout << "In range with par dist " << z << endl;
				dist[x][y] = z + 1;
				if (dist[x][y] > ans) {
					s[x][y] = 'X';
				} else {
					s[x][y] = '.';
				}
				Q.push(pii(x, y));
			}
		}
	}
}

int main() {
	ios_base::sync_with_stdio(0);
	cin >> n >> m;
	REP(i, n) {
		cin >> x;
		s.push_back(x);
	}
	REP(i, n) {
		dist[i].resize(m);
		int prev = -1;
		REP(j, m) {
			if (s[i][j] == 'X') {
				int c = 1;
				if (j > 0) c += leftS[i].back();
				leftS[i].pb(c);
				c = 1;
				if (i > 0) c += up[i - 1][j];
				up[i].pb(c);
			} else {
				leftS[i].pb(0);
				up[i].pb(0);
			}
		}
	}
	FORD(i, n - 1, 0) {
		FORD(j, m - 1, 0) {
			if (s[i][j] == 'X') {
				int c = 1;
				if (j < m - 1) c += rightS[i].back();
				rightS[i].pb(c);
				c = 1;
				if (i < n - 1) c += down[i + 1][j];
				down[i].pb(c);
			} else {
				rightS[i].pb(0);
				down[i].pb(0);
			}
		}
		reverse(down[i].begin(), down[i].end());
		reverse(rightS[i].begin(), rightS[i].end());
	}
	int minim = n * m;
	REP(i, n) {
		Q.push(pii(i, -1)); Q.push(pii(i, m));
		REP(j, m) {
			if (s[i][j] == 'X') {
				minim = min(minim, min(up[i][j] + down[i][j] - 1, leftS[i][j] + rightS[i][j] - 1));
				dist[i][j] = n * m;
			}
			else {
				Q.push(pii(i, j));
				dist[i][j] = 0;
			}
		}
	}
	// k-ta tura -> 2k + 1
	REP(j, m) { Q.push(pii(-1, j)); Q.push(pii(n, j)); }
	ans = (minim - 1) / 2;
	cout << ans << endl;
	BFS();
	REP(i, n) {
		REP(j, m) {
			cout << s[i][j];
		}
		cout<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>
#define inf (ll)1e17
#define maxn 1003
#define fi first
#define se second
#define ll long long
#define pi pair<int,int>
#define mp make_pair
#define task ""
#define mid ((l+r)/2)
using namespace std;
int f[maxn][maxn],sum[maxn][maxn],dd[10][maxn][maxn],a[maxn][maxn],n,m,res,tt;
void inp()
{
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
  	freopen(task".inp","r",stdin);
	freopen(task".out","w",stdout);
}
ll calcul(int i,int j,int t,int k)
{
     if(k==0){
     int x=i+t,y=j+t;
     i=i-t;j=j-t;
//     if(x>n||y>m||i<1||j<1) return-1;
     return f[x][y]-f[i-1][y]-f[x][j-1]+f[i-1][j-1];
     }else{
     int x=min(i+t,n),y=min(j+t,m);
      i=max(i-t,1);j=max(j-t,1);
     return sum[x][y]-sum[i-1][y]-sum[x][j-1]+sum[i-1][j-1];
     }
}
bool check(int x,int t)
{
       for(int i=1;i<=n;i++)
          for(int j=1;j<=m;j++)
          {
               sum[i][j]=0;
               dd[t][i][j]=0;
          }
//        for(int i=1;i<=n;i++){
//     for(int j=1;j<=m;j++)
//     cout<<sum[i][j]<<" ";
//     cout<<'\n';
//    }
//    cout<<'\n';
     for(int i=1;i<=n;i++)
          for(int j=1;j<=m;j++)
     {
          if(calcul(i,j,x,0)==(2*x+1)*(2*x+1))
          {
               dd[t][i][j]=1;
              // cout<<calcul(i,j,x,0)<<'\n';
          }
          sum[i][j]=sum[i-1][j]+sum[i][j-1]+dd[t][i][j]-sum[i-1][j-1];
     }
//     cout<<x<<'\n';
//      for(int i=1;i<=n;i++){
//     for(int j=1;j<=m;j++)
//     cout<<sum[i][j]<<" ";
//     cout<<'\n';
//    }
//    cout<<'\n';
      for(int i=1;i<=n;i++)
          for(int j=1;j<=m;j++)
               {
               if(calcul(i,j,x,1)==0&&a[i][j]==1)return false;
//               cout<<calcul(i,j,x,1)<<" "<<a[i][j]<<'\n';
               }
     return true;
}
int main()
{
     char x;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
     for(int j=1;j<=m;j++)
     {
          cin>>x;
          if(x=='X')
               a[i][j]=1;
     }
    for(int i=1;i<=n;i++)
     for(int j=1;j<=m;j++)
    {
         f[i][j]=f[i-1][j]+f[i][j-1]+a[i][j]-f[i-1][j-1];
    }
    int l=0,r=min(n,m);
    int d=0;
    while(l<=r)
    {
//         cout<<mid<<'\n';
//         cout<<l<<" "<<r<<" "<<mid<<" "<<res<<'\n';
         if(check(mid,d)==true)
         {
//              cout<<mid<<'\n';
              tt=d;
              res=mid;
              l=mid+1;
         }else r=mid-1;
         d++;
    }
    cout<<res<<'\n';
    //cout<<tt<<'\n';
     if(res==0){
       for(int i=1;i<=n;i++){
     for(int j=1;j<=m;j++)
     if(a[i][j]==1)
     cout<<"X";else cout<<".";
     cout<<'\n';
    }
    return 0;
     }
    for(int i=1;i<=n;i++){
     for(int j=1;j<=m;j++)
          if(dd[tt][i][j]==1)
     cout<<"X";else cout<<".";
     cout<<'\n';
    }
    return 0;
}


#include <bits/stdc++.h>
#define inf (ll)1e17
#define maxn 1003
#define fi first
#define se second
#define ll long long
#define pi pair<int,int>
#define mp make_pair
#define task ""
#define mid ((l+r)/2)
using namespace std;
int f[maxn][maxn],sum[maxn][maxn],dd[10][maxn][maxn],a[maxn][maxn],n,m,res,tt;
void inp()
{
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
  	freopen(task".inp","r",stdin);
	freopen(task".out","w",stdout);
}
ll calcul(int i,int j,int t,int k)
{
     if(k==0){
     int x=i+t,y=j+t;
     i=i-t;j=j-t;
     if(x>n||y>m||i<1||j<1) return-1;
     return f[x][y]-f[i-1][y]-f[x][j-1]+f[i-1][j-1];
     }else{
     int x=min(i+t,n),y=min(j+t,m);
      i=max(i-t,1);j=max(j-t,1);
     return sum[x][y]-sum[i-1][y]-sum[x][j-1]+sum[i-1][j-1];
     }
}
bool check(int x,int t)
{
       for(int i=1;i<=n;i++)
          for(int j=1;j<=m;j++)
          {
               sum[i][j]=0;
               dd[t][i][j]=0;
          }
//        for(int i=1;i<=n;i++){
//     for(int j=1;j<=m;j++)
//     cout<<sum[i][j]<<" ";
//     cout<<'\n';
//    }
//    cout<<'\n';
     for(int i=1;i<=n;i++)
          for(int j=1;j<=m;j++)
     {
          if(calcul(i,j,x,0)==(2*x+1)*(2*x+1))
          {
               dd[t][i][j]=1;
              // cout<<calcul(i,j,x,0)<<'\n';
          }
          sum[i][j]=sum[i-1][j]+sum[i][j-1]+dd[t][i][j]-sum[i-1][j-1];
     }
//     cout<<x<<'\n';
//      for(int i=1;i<=n;i++){
//     for(int j=1;j<=m;j++)
//     cout<<dd[t][i][j]<<" ";
//     cout<<'\n';
//    }
//    cout<<'\n';
      for(int i=1;i<=n;i++)
          for(int j=1;j<=m;j++)
               {
               if(calcul(i,j,x,1)==0&&a[i][j]==1)return false;
//               cout<<calcul(i,j,x,1)<<" "<<a[i][j]<<'\n';
               }
     return true;
}
int main()
{
     char x;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
     for(int j=1;j<=m;j++)
     {
          cin>>x;
          if(x=='X')
               a[i][j]=1;
     }
    for(int i=1;i<=n;i++)
     for(int j=1;j<=m;j++)
    {
         f[i][j]=f[i-1][j]+f[i][j-1]+a[i][j]-f[i-1][j-1];
    }
    int l=0,r=min(n,m);
    int d=0;
    while(l<=r)
    {
//         cout<<mid<<'\n';
//         cout<<l<<" "<<r<<" "<<mid<<" "<<res<<'\n';
         if(check(mid,d)==true)
         {
//              cout<<mid<<'\n';
              tt=d;
              res=mid;
              l=mid+1;
         }else r=mid-1;
         d++;
    }
    cout<<res<<'\n';
    //cout<<tt<<'\n';
     if(res==0){
       for(int i=1;i<=n;i++){
     for(int j=1;j<=m;j++)
     if(a[i][j]==1)
     cout<<"X";else cout<<".";
     cout<<'\n';
    }
    return 0;
     }
    for(int i=1;i<=n;i++){
     for(int j=1;j<=m;j++)
          if(dd[tt][i][j]==1)
     cout<<"X";else cout<<".";
     cout<<'\n';
    }
    return 0;
}


#include <bits/stdc++.h>
#define inf (ll)1e17
#define maxn 1003
#define fi first
#define se second
#define ll long long
#define pi pair<int,int>
#define mp make_pair
#define task ""
#define mid ((l+r)/2)
using namespace std;
int f[maxn][maxn],sum[maxn][maxn],dd[10][maxn][maxn],a[maxn][maxn],n,m,res,tt;
void inp()
{
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
  	freopen(task".inp","r",stdin);
	freopen(task".out","w",stdout);
}
ll calcul(int i,int j,int t,int k)
{
     if(k==0){
     int x=i+t,y=j+t;
     i=i-t;j=j-t;
//     if(x>n||y>m||i<1||j<1) return-1;
     return f[x][y]-f[i-1][y]-f[x][j-1]+f[i-1][j-1];
     }else{
     int x=min(i+t+1,n),y=min(j+t+1,m);
      i=max(i-t-1,1);j=max(j-t-1,1);
     return sum[i][j]-sum[x-1][j]-sum[i][y-1]+sum[x-1][y-1];
     }
}
bool check(int x,int t)
{
       for(int i=1;i<=n;i++)
          for(int j=1;j<=m;j++)
          {
               sum[i][j]=0;
          }
     for(int i=1;i<=n;i++)
          for(int j=1;j<=m;j++)
     {
          if(calcul(i,j,x,0)==(2*x+1)*(2*x+1))
          {
               dd[t][i][j]=1;
//               cout<<calcul(i,j,x,0)<<'\n';
          }
          sum[i][j]=sum[i-1][j]+sum[i][j-1]+dd[t][i][j]-sum[i-1][j-1];
     }
//      for(int i=1;i<=n;i++){
//     for(int j=1;j<=m;j++)
//     cout<<dd[i][j]<<" ";
//     cout<<'\n';
//    }
      for(int i=1;i<=n;i++)
          for(int j=1;j<=m;j++)
               {
               if(calcul(i,j,x,1)==0&&a[i][j]==1)return false;
//               cout<<calcul(i,j,x,1)<<" "<<a[i][j]<<'\n';
               }
     return true;
}
int main()
{
     char x;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
     for(int j=1;j<=m;j++)
     {
          cin>>x;
          if(x=='X')
               a[i][j]=1;
     }
    for(int i=1;i<=n;i++)
     for(int j=1;j<=m;j++)
    {
         f[i][j]=f[i-1][j]+f[i][j-1]+a[i][j]-f[i-1][j-1];
    }
    int l=0,r=min(n,m);
    int d=0;
    while(l<=r)
    {
//         cout<<mid<<'\n';
         if(check(mid,d)==true)
         {
              tt=d;
              res=mid;
//              cout<<res<<'\n';
              l=mid+1;
         }else r=mid-1;
         d++;
    }
    cout<<res<<'\n';
    //cout<<tt<<'\n';
     if(res==0){
       for(int i=1;i<=n;i++){
     for(int j=1;j<=m;j++)
     if(a[i][j]==1)
     cout<<"X";else cout<<".";
     cout<<'\n';
    }
    return 0;
     }
    for(int i=1;i<=n;i++){
     for(int j=1;j<=m;j++)
          if(dd[tt][i][j]==1)
     cout<<"X";else cout<<".";
     cout<<'\n';
    }
    if(res==0)
       for(int i=1;i<=n;i++){
     for(int j=1;j<=m;j++)
     if(a[i][j]==1)
     cout<<"X";else cout<<".";
     cout<<'\n';
    }
    return 0;
}


#include <bits/stdc++.h>
#define inf (ll)1e17
#define maxn 1003
#define fi first
#define se second
#define ll long long
#define pi pair<ll,ll>
#define mp make_pair
#define task ""
#define mid ((l+r)/2)
using namespace std;
vector<ll> f[maxn],sum[maxn],dd[maxn],a[maxn],resu[maxn];
int n,m,res,tt;
void inp()
{
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
  	freopen(task".inp","r",stdin);
	freopen(task".out","w",stdout);
}
ll calcul(int i,int j,int t,ll k)
{
     if(k==0){
     int x=i+t,y=j+t;
     i=i-t;j=j-t;
     if(x>n||y>m||i<1||j<1) return-1;
     return f[x][y]-f[i-1][y]-f[x][j-1]+f[i-1][j-1];
     }else{
     ll x=min(i+t,n),y=min(j+t,m);
      i=max(i-t,1);j=max(j-t,1);
     return sum[x][y]-sum[i-1][y]-sum[x][j-1]+sum[i-1][j-1];
     }
}
bool check(ll x)
{
       for(ll i=1;i<=n;i++)
          for(ll j=1;j<=m;j++)
          {
               sum[i][j]=0;
               dd[i][j]=0;
          }
     for(ll i=1;i<=n;i++)
          for(ll j=1;j<=m;j++)
     {
          if(calcul(i,j,x,0)==(2*x+1)*(2*x+1))
          {
               dd[i][j]=1;
          }
          sum[i][j]=sum[i-1][j]+sum[i][j-1]+dd[i][j]-sum[i-1][j-1];
     }
      for(ll i=1;i<=n;i++)
          for(ll j=1;j<=m;j++)
               {
               if(calcul(i,j,x,1)==0&&a[i][j]==1)return false;
//               cout<<calcul(i,j,x,1)<<" "<<a[i][j]<<'\n';
               }
     return true;
}
void memo()
{
     for(ll i=1;i<=n;i++)
          for(ll j=1;j<=m;j++)
          resu[i][j]=dd[i][j];
}
int main()
{
     char x;
    cin>>n>>m;
    for(int i=0;i<=n;i++)
     for(int j=0;j<=m;j++)
    {
         f[i].push_back(0);
         a[i].push_back(0);
         resu[i].push_back(0);
         dd[i].push_back(0);
         sum[i].push_back(0);
    }
    for(ll i=1;i<=n;i++)
     for(ll j=1;j<=m;j++)
     {
          cin>>x;
          if(x=='X')
               a[i][j]=1;
     }
    for(ll i=1;i<=n;i++)
     for(ll j=1;j<=m;j++)
    {
         f[i][j]=f[i-1][j]+f[i][j-1]+a[i][j]-f[i-1][j-1];
    }
    ll l=0,r=min(n,m);
    ll d=0;
    while(l<=r)
    {
         if(check(mid)==true)
         {
              memo();
              res=mid;
              l=mid+1;
         }else r=mid-1;
         d++;
    }
    cout<<res<<'\n';
    //cout<<tt<<'\n';
    for(ll i=1;i<=n;i++)
    {
     for(ll j=1;j<=m;j++)
         if(resu[i][j]==1)
          cout<<"X";else cout<<".";
          cout<<'\n';
    }
    return 0;
}


#include <bits/stdc++.h>
#define inf (ll)1e17
#define maxn 1000003
#define fi first
#define se second
#define ll long long
#define pi pair<ll,ll>
#define mp make_pair
#define task ""
#define mid ((l+r)/2)
using namespace std;
vector<ll> f[maxn],sum[maxn],dd[maxn],a[maxn],resu[maxn];
int n,m,res,tt;
void inp()
{
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
  	freopen(task".inp","r",stdin);
	freopen(task".out","w",stdout);
}
ll calcul(int i,int j,int t,ll k)
{
     if(k==0){
     int x=i+t,y=j+t;
     i=i-t;j=j-t;
     if(x>n||y>m||i<1||j<1) return-1;
     return f[x][y]-f[i-1][y]-f[x][j-1]+f[i-1][j-1];
     }else{
     ll x=min(i+t,n),y=min(j+t,m);
      i=max(i-t,1);j=max(j-t,1);
     return sum[x][y]-sum[i-1][y]-sum[x][j-1]+sum[i-1][j-1];
     }
}
bool check(ll x)
{
       for(ll i=1;i<=n;i++)
          for(ll j=1;j<=m;j++)
          {
               sum[i][j]=0;
               dd[i][j]=0;
          }
     for(ll i=1;i<=n;i++)
          for(ll j=1;j<=m;j++)
     {
          if(calcul(i,j,x,0)==(2*x+1)*(2*x+1))
          {
               dd[i][j]=1;
          }
          sum[i][j]=sum[i-1][j]+sum[i][j-1]+dd[i][j]-sum[i-1][j-1];
     }
      for(ll i=1;i<=n;i++)
          for(ll j=1;j<=m;j++)
               {
               if(calcul(i,j,x,1)==0&&a[i][j]==1)return false;
//               cout<<calcul(i,j,x,1)<<" "<<a[i][j]<<'\n';
               }
     return true;
}
void memo()
{
     for(ll i=1;i<=n;i++)
          for(ll j=1;j<=m;j++)
          resu[i][j]=dd[i][j];
}
int main()
{
     ios_base::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
     char x;
    cin>>n>>m;
    for(int i=0;i<=n;i++)
     for(int j=0;j<=m;j++)
    {
         f[i].push_back(0);
         a[i].push_back(0);
         resu[i].push_back(0);
         dd[i].push_back(0);
         sum[i].push_back(0);
    }
    for(ll i=1;i<=n;i++)
     for(ll j=1;j<=m;j++)
     {
          cin>>x;
          if(x=='X')
               a[i][j]=1;
     }
    for(ll i=1;i<=n;i++)
     for(ll j=1;j<=m;j++)
    {
         f[i][j]=f[i-1][j]+f[i][j-1]+a[i][j]-f[i-1][j-1];
    }
    ll l=0,r=min(n,m);
    ll d=0;
    while(l<=r)
    {
         if(check(mid)==true)
         {
              memo();
              res=mid;
              l=mid+1;
         }else r=mid-1;
         d++;
    }
    cout<<res<<'\n';
    //cout<<tt<<'\n';
    for(ll i=1;i<=n;i++)
    {
     for(ll j=1;j<=m;j++)
         if(resu[i][j]==1)
          cout<<"X";else cout<<".";
          cout<<'\n';
    }
    return 0;
}


#include <bits/stdc++.h>
#define inf (ll)1e17
#define maxn 1003
#define fi first
#define se second
#define ll long long
#define pi pair<int,int>
#define mp make_pair
#define task ""
#define mid ((l+r)/2)
using namespace std;
int f[maxn][maxn],sum[maxn][maxn],dd[10][maxn][maxn],a[maxn][maxn],n,m,res,tt;
void inp()
{
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
  	freopen(task".inp","r",stdin);
	freopen(task".out","w",stdout);
}
ll calcul(int i,int j,int t,int k)
{
     if(k==0){
     int x=i+t,y=j+t;
     i=i-t;j=j-t;
//     if(x>n||y>m||i<1||j<1) return-1;
     return f[x][y]-f[i-1][y]-f[x][j-1]+f[i-1][j-1];
     }else{
     int x=min(i+t+1,n),y=min(j+t+1,m);
      i=max(i-t-1,1);j=max(j-t-1,1);
     return sum[x][y]-sum[i-1][y]-sum[x][j-1]+sum[i-1][j-1];
     }
}
bool check(int x,int t)
{
       for(int i=1;i<=n;i++)
          for(int j=1;j<=m;j++)
          {
               sum[i][j]=0;
          }
     for(int i=1;i<=n;i++)
          for(int j=1;j<=m;j++)
     {
          if(calcul(i,j,x,0)==(2*x+1)*(2*x+1))
          {
               dd[t][i][j]=1;
              // cout<<calcul(i,j,x,0)<<'\n';
          }
          sum[i][j]=sum[i-1][j]+sum[i][j-1]+dd[t][i][j]-sum[i-1][j-1];
     }
    // cout<<x<<'\n';
//      for(int i=1;i<=n;i++){
//     for(int j=1;j<=m;j++)
//     cout<<dd[t][i][j]<<"";
//     cout<<'\n';
//    }
//    cout<<'\n';
      for(int i=1;i<=n;i++)
          for(int j=1;j<=m;j++)
               {
               if(calcul(i,j,x,1)==0&&a[i][j]==1)return false;
//               cout<<calcul(i,j,x,1)<<" "<<a[i][j]<<'\n';
               }
     return true;
}
int main()
{
     char x;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
     for(int j=1;j<=m;j++)
     {
          cin>>x;
          if(x=='X')
               a[i][j]=1;
     }
    for(int i=1;i<=n;i++)
     for(int j=1;j<=m;j++)
    {
         f[i][j]=f[i-1][j]+f[i][j-1]+a[i][j]-f[i-1][j-1];
    }
    int l=0,r=min(n,m);
    int d=0;
    while(l<=r)
    {
//         cout<<mid<<'\n';
//         cout<<l<<" "<<r<<" "<<mid<<" "<<res<<'\n';
         if(check(mid,d)==true)
         {
//              cout<<mid<<'\n';
              tt=d;
              res=mid;
              l=mid+1;
         }else r=mid-1;
         d++;
    }
    cout<<res<<'\n';
    //cout<<tt<<'\n';
     if(res==0){
       for(int i=1;i<=n;i++){
     for(int j=1;j<=m;j++)
     if(a[i][j]==1)
     cout<<"X";else cout<<".";
     cout<<'\n';
    }
    return 0;
     }
    for(int i=1;i<=n;i++){
     for(int j=1;j<=m;j++)
          if(dd[tt][i][j]==1)
     cout<<"X";else cout<<".";
     cout<<'\n';
    }
    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

template<typename T> // order_of_key(), *find_by_order()
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

using ll = long long;

vector<vector<int>> a;
vector<vector<char>> f;
vector<vector<char>> b;
int n, m;

int get_sum(int y1, int x1, int y2, int x2) {
    return a[y2][x2] - a[y1-1][x2] - a[y2][x1-1] + a[y1-1][x1-1];
}

bool fit(int y, int x, long long l) {
    return y - l + 1 > 0 && x - l + 1 > 0 && get_sum(y - l + 1, x - l + 1, y, x) == l * l;
}

void paint(int y, int x, int l) {
    for (int i = 0; i < l; i++) {
        if (a[y - i][x] == 'X') break;
        for (int j = 0; j < l && b[y - i][x - j] == '.'; j++) {
            b[y-i][x-j] = 'X';
        }
    }
}

bool check(int t) {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            b[i][j] = '.';
        }
    }
    int l = 1 + 2 * t;
    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n; j++) {
            if (fit(j, i, l)) {
                paint(j, i, l);
            }
        }
    }
    if (b == f) return true;
    return false;
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> n >> m;
    a.resize(n + 1);
    f.resize(n + 1);
    b.resize(n + 1);
    for (auto& v: a) v.resize(m + 1);
    for (auto& v: f) v.resize(m + 1);
    for (auto& v: b) v.resize(m + 1);
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> f[i][j];
            a[i][j] = f[i][j] == 'X';
            a[i][j] += a[i-1][j] + a[i][j-1] - a[i-1][j-1];
        }
    }
    int l = 0, r = min(m, n) / 2;
    while (l < r) {
        int m = (l + r + 1) / 2;
        if (check(m)) l = m;
        else r = m - 1;
    }
    cout << l << endl;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            b[i][j] = '.';
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (fit(i, j, 2 * l + 1)) {
                b[i - l][j - l] = 'X';
            }
        }
    }
    for (int i = 1;i <= n; i++, cout << endl) {
        for (int j = 1; j <= m; j++) {
            cout << b[i][j];
        }
    }
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

template<typename T> // order_of_key(), *find_by_order()
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

using ll = long long;

vector<vector<int>> a;
vector<vector<char>> f;
vector<vector<char>> b;
int n, m;

int get_sum(int y1, int x1, int y2, int x2) {
    return a[y2][x2] - a[y1-1][x2] - a[y2][x1-1] + a[y1-1][x1-1];
}

bool fit(int y, int x, long long l) {
    return y - l + 1 > 0 && x - l + 1 > 0 && get_sum(y - l + 1, x - l + 1, y, x) == l * l;
}

void paint(int y, int x, int l) {
    for (int i = 0; i < l; i++) {
        for (int j = 0; j < l && b[y - i][x - j] == '.'; j++) {
            b[y-i][x-j] = 'X';
        }
    }
}

bool check(int t) {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            b[i][j] = '.';
        }
    }
    int l = 1 + 2 * t;
    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n; j++) {
            if (fit(j, i, l)) {
                paint(j, i, l);
            }
        }
    }
    if (b == f) return true;
    return false;
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> n >> m;
    a.resize(n + 1);
    f.resize(n + 1);
    b.resize(n + 1);
    for (auto& v: a) v.resize(m + 1);
    for (auto& v: f) v.resize(m + 1);
    for (auto& v: b) v.resize(m + 1);
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> f[i][j];
            a[i][j] = f[i][j] == 'X';
            a[i][j] += a[i-1][j] + a[i][j-1] - a[i-1][j-1];
        }
    }
    int l = 0, r = max(m, n);
    while (l < r) {
        int m = (l + r + 1) / 2;
        if (check(m)) l = m;
        else r = m - 1;
    }
    cout << l << endl;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            b[i][j] = '.';
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (fit(i, j, 2 * l + 1)) {
                b[i - l][j - l] = 'X';
            }
        }
    }
    for (int i = 1;i <= n; i++, cout << endl) {
        for (int j = 1; j <= m; j++) {
            cout << b[i][j];
        }
    }
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

template<typename T> // order_of_key(), *find_by_order()
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

using ll = long long;

vector<vector<int>> a;
vector<vector<char>> f;
vector<vector<char>> b;
int n, m;

int get_sum(int y1, int x1, int y2, int x2) {
    return a[y2][x2] - a[y1-1][x2] - a[y2][x1-1] + a[y1-1][x1-1];
}

bool fit(int y, int x, long long l) {
    return y - l + 1 > 0 && x - l + 1 > 0 && get_sum(y - l + 1, x - l + 1, y, x) == l * l;
}

void paint(int y, int x, int l) {
    for (int i = 0; i < l; i++) {
        for (int j = 0; j < l && b[y - i][x - j] == '.'; j++) {
            b[y-i][x-j] = 'X';
        }
    }
}

bool check(int t) {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            b[i][j] = '.';
        }
    }
    int l = 1 + 2 * t;
    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n; j++) {
            if (fit(j, i, l)) {
                paint(j, i, l);
            }
        }
    }
    if (b == f) return true;
    return false;
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> n >> m;
    a.resize(n + 1);
    f.resize(n + 1);
    b.resize(n + 1);
    for (auto& v: a) v.resize(m + 1);
    for (auto& v: f) v.resize(m + 1);
    for (auto& v: b) v.resize(m + 1);
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> f[i][j];
            a[i][j] = f[i][j] == 'X';
            a[i][j] += a[i-1][j] + a[i][j-1] - a[i-1][j-1];
        }
    }
    int l = 0, r = min(m, n) / 2;
    while (l < r) {
        int m = (l + r + 1) / 2;
        if (check(m)) l = m;
        else r = m - 1;
    }
    cout << l << endl;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            b[i][j] = '.';
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (fit(i, j, 2 * l + 1)) {
                b[i - l][j - l] = 'X';
            }
        }
    }
    for (int i = 1;i <= n; i++, cout << endl) {
        for (int j = 1; j <= m; j++) {
            cout << b[i][j];
        }
    }
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

template<typename T> // order_of_key(), *find_by_order()
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

using ll = long long;

vector<vector<int>> a;
vector<vector<char>> f;
vector<vector<char>> b;
int n, m;

int get_sum(int y1, int x1, int y2, int x2) {
    return a[y2][x2] - a[y1-1][x2] - a[y2][x1-1] + a[y1-1][x1-1];
}

bool fit(int y, int x, long long l) {
    return y - l + 1 > 0 && x - l + 1 > 0 && get_sum(y - l + 1, x - l + 1, y, x) == l * l;
}

void paint(int y, int x, int l) {
    for (int i = 0; i < l; i++) {
        if (a[y - i][x] == 'X') break;
        for (int j = 0; j < l && b[y - i][x - j] == '.'; j++) {
            b[y-i][x-j] = 'X';
        }
    }
}

bool check(int t) {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            b[i][j] = '.';
        }
    }
    int l = 1 + 2 * t;
    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n; j++) {
            if (fit(j, i, l)) {
                paint(j, i, l);
            }
        }
    }
    if (b == f) return true;
    return false;
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> n >> m;
    a.resize(n + 1);
    f.resize(n + 1);
    b.resize(n + 1);
    for (auto& v: a) v.resize(m + 1);
    for (auto& v: f) v.resize(m + 1);
    for (auto& v: b) v.resize(m + 1);
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> f[i][j];
            a[i][j] = f[i][j] == 'X';
            a[i][j] += a[i-1][j] + a[i][j-1] - a[i-1][j-1];
        }
    }
    int l = 0, r = min(m, n);
    while (l < r) {
        int m = (l + r + 1) / 2;
        if (check(m)) l = m;
        else r = m - 1;
    }
    cout << l << endl;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            b[i][j] = '.';
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (fit(i, j, 2 * l + 1)) {
                b[i - l][j - l] = 'X';
            }
        }
    }
    for (int i = 1;i <= n; i++, cout << endl) {
        for (int j = 1; j <= m; j++) {
            cout << b[i][j];
        }
    }
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

template<typename T> // order_of_key(), *find_by_order()
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

using ll = long long;

vector<vector<int>> a;
vector<vector<char>> f;
vector<vector<char>> b;
int n, m;

int get_sum(int y1, int x1, int y2, int x2) {
    return a[y2][x2] - a[y1-1][x2] - a[y2][x1-1] + a[y1-1][x1-1];
}

bool fit(int y, int x, long long l) {
    return y - l + 1 > 0 && x - l + 1 > 0 && get_sum(y - l + 1, x - l + 1, y, x) == l * l;
}

void paint(int y, int x, int l) {
    for (int i = 0; i < l; i++) {
        for (int j = 0; j < l && b[y - i][x - j] == '.'; j++) {
            b[y-i][x-j] = 'X';
        }
    }
}

bool check(int t) {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            b[i][j] = '.';
        }
    }
    int l = 1 + 2 * t;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (fit(i, j, l)) {
                paint(i, j, l);
            }
        }
    }
    if (b == f) return true;
    return false;
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> n >> m;
    a.resize(n + 1);
    f.resize(n + 1);
    b.resize(n + 1);
    for (auto& v: a) v.resize(m + 1);
    for (auto& v: f) v.resize(m + 1);
    for (auto& v: b) v.resize(m + 1);
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> f[i][j];
            a[i][j] = f[i][j] == 'X';
            a[i][j] += a[i-1][j] + a[i][j-1] - a[i-1][j-1];
        }
    }
    int l = 0, r = max(m, n);
    while (l < r) {
        int m = (l + r + 1) / 2;
        if (check(m)) l = m;
        else r = m - 1;
    }
    cout << l << endl;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            b[i][j] = '.';
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (fit(i, j, 2 * l + 1)) {
                b[i - l][j - l] = 'X';
            }
        }
    }
    for (int i = 1;i <= n; i++, cout << endl) {
        for (int j = 1; j <= m; j++) {
            cout << b[i][j];
        }
    }
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

template<typename T> // order_of_key(), *find_by_order()
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

using ll = long long;

vector<vector<int>> a;
vector<vector<char>> f;
vector<vector<char>> b;
int n, m;

int get_sum(int y1, int x1, int y2, int x2) {
    return a[y2][x2] - a[y1-1][x2] - a[y2][x1-1] + a[y1-1][x1-1];
}

bool fit(int y, int x, long long l) {
    return y - l + 1 > 0 && x - l + 1 > 0 && get_sum(y - l + 1, x - l + 1, y, x) == l * l;
}

void paint(int y, int x, int l) {
    for (int i = 0; i < l; i++) {
        for (int j = 0; j < l && b[y - i][x - j] == '.'; j++) {
            b[y-i][x-j] = 'X';
        }
    }
}

bool check(int t) {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            b[i][j] = '.';
        }
    }
    int l = 1 + 2 * t;
    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n; j++) {
            if (fit(j, i, l)) {
                paint(j, i, l);
            }
        }
    }
    if (b == f) return true;
    return false;
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> n >> m;
    a.resize(n + 1);
    f.resize(n + 1);
    b.resize(n + 1);
    for (auto& v: a) v.resize(m + 1);
    for (auto& v: f) v.resize(m + 1);
    for (auto& v: b) v.resize(m + 1);
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> f[i][j];
            a[i][j] = f[i][j] == 'X';
            a[i][j] += a[i-1][j] + a[i][j-1] - a[i-1][j-1];
        }
    }
    int l = 0, r = max(m, n);
    while (l < r) {
        int m = (l + r + 1) / 2;
        if (check(m)) l = m;
        else r = m - 1;
    }
    cout << l << endl;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            b[i][j] = '.';
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (fit(i, j, 2 * l + 1)) {
                b[i - l][j - l] = 'X';
            }
        }
    }
    for (int i = 1;i <= n; i++, cout << '\n') {
        for (int j = 1; j <= m; j++) {
            cout << b[i][j];
        }
    }
}


#include<bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back

const int N=1100000;
typedef long long LL;
const LL mod=1e9+7;
int n,m;
vector<string> s;
vector<string> t;
vector<pair<int,int> > way,answay;
string blank;
int ans;
int get(int x,int y)
{
	return x*m+y;
}
int sum[N];
int f[N];
int check(int x)
{
	way.clear();
	for (int i=0;i<n;i++)
		for (int j=0;j<m;j++)
			sum[get(i,j)]=(s[i][j]=='X');
	for (int i=0;i<n;i++)
		for (int j=0;j<m;j++)
		{
			if (i) sum[get(i,j)]+=sum[get(i-1,j)];
			if (j) sum[get(i,j)]+=sum[get(i,j-1)];
			if (i>0&&j>0) sum[get(i,j)]-=sum[get(i-1,j-1)];
		}
	int len=2*x+1;
	memset(f,0,sizeof(f));
	for (int i=0;i+len-1<n;i++)
		for (int j=0;j+len-1<m;j++)
		{
			int res=sum[get(i+len-1,j+len-1)];
			if (i) res-=sum[get(i-1,j+len-1)];
			if (j) res-=sum[get(i+len-1,j-1)];
			if (i>0&&j>0) res+=sum[get(i-1,j-1)];
			if (res==len*len)
			{
				way.pb(mp(i+x,j+x));
				f[get(i,j)]++;
				if (i+len<n&&j+len<m) f[get(i+len,j+len)]++;
				if (i+len<n) f[get(i+len,j)]--;
				if (j+len<m) f[get(i,j+len)]--;
			}
		}
	int flag=1;
	for (int i=0;i<n;i++)
		for (int j=0;j<m;j++)
		{
			if (i) f[get(i,j)]+=f[get(i-1,j)];
			if (j) f[get(i,j)]+=f[get(i,j-1)];
			if (i>0&&j>0) f[get(i,j)]-=f[get(i-1,j-1)];
			if (s[i][j]=='X'&&f[get(i,j)]==0) flag=0;
		}
	return flag;
}
int main()
{
	cin.sync_with_stdio(false);
	cin>>n>>m;
	blank="";
	for (int i=0;i<m;i++)
		blank.pb('.');
	for (int i=0;i<n;i++)
		t.pb(blank);
	for (int i=1;i<=n;i++)
	{
		string tmp;
		cin>>tmp;
		s.pb(tmp);
	}
	int l=0,r=2000;
	while (l<=r)
	{
		int mid=(l+r)/2;
		if (check(mid))
		{
			ans=mid;
			l=mid+1;
			answay.clear();
			for (int i=0;i<way.size();i++)
				answay.pb(way[i]);
		}
		else r=mid-1;
	}
	cout<<ans<<endl;
	for (int i=0;i<answay.size();i++)
		t[answay[i].fi][answay[i].se]='X';
	for (int i=0;i<n;i++)
		cout<<t[i]<<endl;
}


#include<bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back

const int N=1100000;
typedef long long LL;
const LL mod=1e9+7;
int n,m;
vector<string> s;
vector<string> t;
vector<string> tmp;
string blank;
int ans;
int get(int x,int y)
{
	return x*m+y;
}
int sum[N];
int f[N];
int check(int x)
{
	tmp.clear();
	for (int i=0;i<n;i++)
		tmp.pb(blank);
	for (int i=0;i<n;i++)
		for (int j=0;j<m;j++)
			sum[get(i,j)]=(s[i][j]=='X');
	for (int i=0;i<n;i++)
		for (int j=0;j<m;j++)
		{
			if (i) sum[get(i,j)]+=sum[get(i-1,j)];
			if (j) sum[get(i,j)]+=sum[get(i,j-1)];
			if (i>0&&j>0) sum[get(i,j)]-=sum[get(i-1,j-1)];
		}
	int len=2*x+1;
	memset(f,0,sizeof(f));
	for (int i=0;i+len-1<n;i++)
		for (int j=0;j+len-1<m;j++)
		{
			int res=sum[get(i+len-1,j+len-1)];
			if (i) res-=sum[get(i-1,j+len-1)];
			if (j) res-=sum[get(i+len-1,j-1)];
			if (i>0&&j>0) res+=sum[get(i-1,j-1)];
			if (res==len*len)
			{
				tmp[i+x][j+x]='X';
				f[get(i,j)]++;
				if (i+len<n&&j+len<m) f[get(i+len,j+len)]++;
				if (i+len<n) f[get(i+len,j)]--;
				if (j+len<m) f[get(i,j+len)]--;
			}
		}
	int flag=1;
	for (int i=0;i<n;i++)
		for (int j=0;j<m;j++)
		{
			if (i) f[get(i,j)]+=f[get(i-1,j)];
			if (j) f[get(i,j)]+=f[get(i,j-1)];
			if (i>0&&j>0) f[get(i,j)]-=f[get(i-1,j-1)];
			if (s[i][j]=='X'&&f[get(i,j)]==0) flag=0;
		}
	return flag;
}
int main()
{
	cin.sync_with_stdio(false);
	cin>>n>>m;
	blank="";
	for (int i=0;i<m;i++)
		blank.pb('.');
	for (int i=0;i<n;i++)
		t.pb(blank);
	for (int i=1;i<=n;i++)
	{
		string tmp;
		cin>>tmp;
		s.pb(tmp);
	}
	int l=0,r=2000;
	while (l<=r)
	{
		int mid=(l+r)/2;
		if (check(mid))
		{
			ans=mid;
			l=mid+1;
			for (int i=0;i<n;i++)
				t[i]=tmp[i];
		}
		else r=mid-1;
	}
	cout<<ans<<endl;
	for (int i=0;i<n;i++)
		cout<<t[i]<<endl;
}


#include<bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back

const int N=1100000;
typedef long long LL;
const LL mod=1e9+7;
int n,m;
int fp[N];
vector<string> s;
string blank;
int ans;
inline int get(int x,int y)
{
	return x*m+y;
}
int sum[N];
int f[N];
int cp[N];
int check(int x)
{
	int len=2*x+1;
	memset(f,0,sizeof(f));
	memset(cp,0,sizeof(cp));
	for (int i=0;i+len-1<n;i++)
		for (int j=0;j+len-1<m;j++)
		{
			int res=sum[get(i+len-1,j+len-1)];
			if (i) res-=sum[get(i-1,j+len-1)];
			if (j) res-=sum[get(i+len-1,j-1)];
			if (i>0&&j>0) res+=sum[get(i-1,j-1)];
			if (res==len*len)
			{
				cp[get(i+x,j+x)]=1;
				f[get(i,j)]++;
				if (i+len<n&&j+len<m) f[get(i+len,j+len)]++;
				if (i+len<n) f[get(i+len,j)]--;
				if (j+len<m) f[get(i,j+len)]--;
			}
		}
	int flag=1;
	for (int i=0;i<n;i++)
		for (int j=0;j<m;j++)
		{
			if (i) f[get(i,j)]+=f[get(i-1,j)];
			if (j) f[get(i,j)]+=f[get(i,j-1)];
			if (i>0&&j>0) f[get(i,j)]-=f[get(i-1,j-1)];
			if (fp[get(i,j)]&&f[get(i,j)]==0) flag=0;
		}
	return flag;
}
int pp[N];
int main()
{
	cin.sync_with_stdio(false);
	cin>>n>>m;
	for (int i=1;i<=n;i++)
	{
		string tmp="";
		cin>>tmp;
		s.pb(tmp);
	}
	for (int i=0;i<n;i++)
		for (int j=0;j<m;j++)
			sum[get(i,j)]=fp[get(i,j)]=(s[i][j]=='X');
	for (int i=0;i<n;i++)
		for (int j=0;j<m;j++)
		{
			if (i) sum[get(i,j)]+=sum[get(i-1,j)];
			if (j) sum[get(i,j)]+=sum[get(i,j-1)];
			if (i>0&&j>0) sum[get(i,j)]-=sum[get(i-1,j-1)];
		}
	int l=0,r=2000;
	while (l<=r)
	{
		int mid=(l+r)/2;
		if (check(mid))
		{
			ans=mid;
			l=mid+1;
			for (int i=0;i<n*m;i++)
				pp[i]=cp[i];
		}
		else r=mid-1;
	}
	cout<<ans<<endl;
	for (int i=0;i<n;i++)
	{
		for (int j=0;j<m;j++)
			if (pp[get(i,j)]) putchar('X');
			else putchar('.');
		putchar('\n');
	}
}


#include <bits/stdc++.h>

using namespace std;

typedef long long int LL;

#define st first
#define nd second
#define PLL pair <LL, LL>
#define PII pair <int, int>

const int N = 1e6 + 7;
const int MX = 1e9 + 7;
const LL INF = 1LL * MX * MX;

int n, m;
string input[N];
vector <int> val[4][N];

bool check(int x, int y) {
	return x < 1 || y < 1 || x > n || y > m || input[x][y] == '.';
}

bool is_corner(const int &r, const int &c, const int &dx, const int &dy) {
	return check(r + dx, c) && check(r, c + dy);
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	cin >> n >> m;

	const int FROMX[] = {1, 1, n, n};
	const int TOX[] = {n + 1, n + 1, 0, 0};

	const int FROMY[] = {1, m, 1, m};
	const int TOY[] = {m + 1, 0, m + 1, 0};

	const int DX[] = {-1, -1, 1, 1};
	const int DY[] = {-1, 1, -1, 1};

	for(int i = 1; i <= n; ++i) {
		cin >> input[i];
		input[i] = "." + input[i] + ".";
	}

	for(int t = 0; t < 4; ++t) {
		for(int i = 0; i <= n + 1; ++i)
			val[t][i].resize(m + 2, -1);

		for(int r = FROMX[t]; r != TOX[t]; r -= DX[t]) {
			for(int c = FROMY[t]; c != TOY[t]; c -= DY[t]) {
				if(input[r][c] == '.')	continue;
				val[t][r][c] = min(min(val[t][r + DX[t]][c], val[t][r][c + DY[t]]), val[t][r + DX[t]][c + DY[t]]) + 1;
			}
		}
	}

	int T = min(n - 1, m - 1) / 2;
	for(int t = 0; t < 4; ++t) {
		for(int r = FROMX[t]; r != TOX[t]; r -= DX[t]) {
			for(int c = FROMY[t]; c != TOY[t]; c -= DY[t]) {
				if(input[r][c] == '.')	continue;
				if(is_corner(r, c, -DX[t], -DY[t])){
//					printf("for %d -> %d %d %d\n", t, r, c, val[t][r][c]);
					T = min(T, val[t][r][c] / 2);
				}
			}
		}
	}

	printf("%d\n", T);
	for(int i = 1; i <= n; ++i) {
		for(int j = 1; j <= m; ++j){
			bool ok = input[i][j] == 'X';
			for(int t = 0; t < 4; ++t)
				ok &= val[t][i][j] >= T;

			if(ok)
				printf("X");
			else
				printf(".");
		}

		puts("");
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int a,b,x,y,n;
		cin>>a>>b>>x>>y>>n;
		int n2=n;
		int a1,a2,b1,b2;
		if(n>b-y)
		{
			b1=y;
			n=n-b+y;
			if(n>a-x)
			{
				a1=x;
			}
			else
			{
				a1=a-n;
			}
		}
		else
		{
			b1=b-n;
			a1=a;
		}
		if(n2>a-x)
		{
			a2=x;
			n2=n2-a+x;
			if(n2>b-y)
			{
				b2=y;
			}
			else
			{
				b2=b-n2;;
			}
		}
		else
		{
			a2=a-n;
			b2=b;
		}

		if(a1*b1>a2*b2)
		{
			cout<<ll*a2*b2;
		}
		else
		{
			cout<<ll*a1*b1;
		}
		cout<<"\n";

    }

	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int a,b,x,y,n;
		cin>>a>>b>>x>>y>>n;
		int n2=n;
		int a1,a2,b1,b2;
		if(n>b-y)
		{
			b1=y;
			n=n-b+y;
			if(n>a-x)
			{
				a1=x;
			}
			else
			{
				a1=a-n;
			}
		}
		else
		{
			b1=b-n;
			a1=a;
		}
		if(n2>a-x)
		{
			a2=x;
			n2=n2-a+x;
			if(n2>b-y)
			{
				b2=y;
			}
			else
			{
				b2=b-n2;;
			}
		}
		else
		{
			a2=a-n;
			b2=b;
		}

		if(a1*b1>a2*b2)
		{
			cout<<(long int)a2*b2;
		}
		else
		{
			cout<<(long int)a1*b1;
		}

    }

	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		long long a,b,x,y,n;
		cin>>a>>b>>x>>y>>n;
		long long n2=n;
		long long a1,a2,b1,b2;
		if(n>b-y)
		{
			b1=y;
			n=n-b+y;
			if(n>a-x)
			{
				a1=x;
			}
			else
			{
				a1=a-n;
			}
		}
		else
		{
			b1=b-n;
			a1=a;
		}
		if(n2>a-x)
		{
			a2=x;
			n2=n2-a+x;
			if(n2>b-y)
			{
				b2=y;
			}
			else
			{
				b2=b-n2;;
			}
		}
		else
		{
			a2=a-n;
			b2=b;
		}
		long long ans=min(a1*b1*1ll,a2*b2*1ll);

		cout<<ans<<"\n";

    }

	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int a,b,x,y,n;
		cin>>a>>b>>x>>y>>n;
		int n2=n;
		int a1,a2,b1,b2;
		if(n>b-y)
		{
			b1=y;
			n=n-b+y;
			if(n>a-x)
			{
				a1=x;
			}
			else
			{
				a1=a-n;
			}
		}
		else
		{
			b1=b-n;
			a1=a;
		}
		if(n2>a-x)
		{
			a2=x;
			n2=n2-a+x;
			if(n2>b-y)
			{
				b2=y;
			}
			else
			{
				b2=b-n2;;
			}
		}
		else
		{
			a2=a-n;
			b2=b;
		}

		if(a1*b1>a2*b2)
		{
			cout<<1ll*a2*b2;
		}
		else
		{
			cout<<1ll*a1*b1;
		}
		cout<<"\n";

    }

	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int a,b,x,y,n;
		cin>>a>>b>>x>>y>>n;
		int n2=n;
		pair<int,int>p1(a,x);
		pair<int,int>p2(b,y);
		int a1,b1,a2,b2;
			if(n>(p2.first-p2.second))
			{
				b1=p2.second;
				n=n-(p2.first-p2.second);
				if(n>(p1.first-p1.second))
				{
					a1=p1.second;
				}
				else
				{
					a1=p1.first-n;
				}

			}
			else
			{
				b1=p2.first-n;
				a1=p1.first;
			}

			if(n2>(p1.first-p1.second))
			{
				a2=p1.second;
				n2=n2-(p1.first-p1.second);
				if(n2>(p2.first-p2.second))
				{
					b2=p2.second;
				}
				else
				{
					b2=p2.first-n2;
				}

			}
			else
			{
				a2=p1.first-n2;
				b2=p2.first;
			}

        if((long int)(a1*b1)>(long int)(a2*b2))
        {
        	cout<<(long int)a2*b2<<"\n";

        }
        else
        {
        	cout<<(long int)a1*b1<<"\n";
        }

    }

	return 0;
}


#include<iostream>
#include<string.h>

using namespace std;

void call(long int a,long int b, long int x,long int y,long int n)
{
	long int temp;
	if(a>b)
	{
		long int c=b-y;
		long int p=n-c;
		if(p>=0)
		{
			long int m=a-x;
			a=a-p;
			if(m>a)
			{
				temp=x;
				a=max(a,temp);
			}
		}
		else
		{
			b=b-n;
			unsigned long long int d=a*b;
			cout<<d;
			return;
		}
		b=b-c;
	}

	else if(a<b)
	{
		long int c=a-x;
		long int p=n-c;
		if(p>=0)
		{
			long int m=b-y;
			b=b-p;
			if(m>b)
			{
				temp=y;
				b=max(b,temp);
			}
		}
		else
		{
			a=a-n;
			unsigned long long int d=a*b;
			cout<<d;
			return;
		}
		a=a-c;
	}
	else
	{
		long int c=a-min(x,y);
		if(x<y)
		{
			long int temp =a;
			a=b;
			b=temp;
		}

		long int p=n-c;
		if(p>=0)
		{
			b=b-p;
		}
		a=a-n;
	}
	unsigned long long int d=a*b;
	cout<<d;
	return;

}

int main()
{
	int t;
	cin>>t;
	long int a,b,x,y,n;
	for(int i=0;i<t;i++)
	{
		cin>>a>>b>>x>>y>>n;
		call(a,b,x,y,n);
		cout<<"\n";
	}
	return 0;
}


#include<iostream>
#include<string.h>

using namespace std;

void call(long int a,long int b, long int x,long int y,long int n)
{
	long int temp;
	if(a>b)
	{
		long int c=b-y;
		long int p=n-c;
		if(p>=0)
		{
			long int m=a-x;
			a=a-p;
			if(m>a)
			{
				temp=x;
				a=max(a,temp);
			}
		}
		else
		{
			b=b-n;
			unsigned long long d=a*b;
			cout<<d;
			return;
		}
		b=b-c;
	}

	else if(a<b)
	{
		long int c=a-x;
		long int p=n-c;
		if(p>=0)
		{
			long int m=b-y;
			b=b-p;
			if(m>b)
			{
				temp=y;
				b=max(b,temp);
			}
		}
		else
		{
			a=a-n;
			unsigned long long d=a*b;
			cout<<d;
			return;
		}
		a=a-c;
	}
	else
	{
		int c=a-min(x,y);
		if(x<y)
		{
			long int temp =a;
			a=b;
			b=temp;
		}

		long int p=n-c;
		if(p>=0)
		{
			b=b-p;
		}
		a=a-n;
	}
	unsigned long long d=a*b;
	cout<<d;
	return;

}

int main()
{
	int t;
	cin>>t;
	long int a,b,x,y,n;
	for(int i=0;i<t;i++)
	{
		cin>>a>>b>>x>>y>>n;
		call(a,b,x,y,n);
		cout<<"\n";
	}
	return 0;
}


#include<iostream>
#include<string.h>

using namespace std;

void call(long int a,long int b, long int x,long int y,long int n)
{
	long int temp;
	if(a>b)
	{
		long int c=b-y;
		long int p=n-c;
		if(p>=0)
		{
			long int m=a-x;
			a=a-p;
			if(m>a)
			{
				temp=x;
				a=max(a,temp);
			}
		}
		else
		{
			b=b-n;
			cout<<a*b;
			return;
		}
		b=b-c;
	}

	else if(a<b)
	{
		long int c=a-x;
		long int p=n-c;
		if(p>=0)
		{
			long int m=b-y;
			b=b-p;
			if(m>b)
			{
				temp=y;
				b=max(b,temp);
			}
		}
		else
		{
			a=a-n;
			cout<<a*b;
			return;
		}
		a=a-c;
	}
	else
	{
		int c=a-min(x,y);
		if(x<y)
		{
			long int temp =a;
			a=b;
			b=temp;
		}

		long int p=n-c;
		if(p>=0)
		{
			b=b-p;
		}
		a=a-n;
	}
	cout<<a*b;
	return;

}

int main()
{
	int t;
	cin>>t;
	long int a,b,x,y,n;
	for(int i=0;i<t;i++)
	{
		cin>>a>>b>>x>>y>>n;
		call(a,b,x,y,n);
		cout<<"\n";
	}
	return 0;
}


#include<iostream>
#include<string.h>

using namespace std;

void call(long int a,long int b, long int x,long int y,long int n)
{
	long int temp;
	if(a>b)
	{
		long int c=b-y;
		long int p=n-c;
		if(p>=0)
		{
			long int m=a-x;
			a=a-p;
			if(m>a)
			{
				temp=x;
				a=max(a,temp);
			}
		}
		else
		{
			b=b-n;
			unsigned long long int d=a*b;
			cout<<d;
			return;
		}
		b=b-c;
	}

	else if(a<b)
	{
		long int c=a-x;
		long int p=n-c;
		if(p>=0)
		{
			long int m=b-y;
			b=b-p;
			if(m>b)
			{
				temp=y;
				b=max(b,temp);
			}
		}
		else
		{
			a=a-n;
			unsigned long long int d=a*b;
			cout<<d;
			return;
		}
		a=a-c;
	}
	else
	{
		int c=a-min(x,y);
		if(x<y)
		{
			long int temp =a;
			a=b;
			b=temp;
		}

		long int p=n-c;
		if(p>=0)
		{
			b=b-p;
		}
		a=a-n;
	}
	unsigned long long int d=a*b;
	cout<<d;
	return;

}

int main()
{
	int t;
	cin>>t;
	long int a,b,x,y,n;
	for(int i=0;i<t;i++)
	{
		cin>>a>>b>>x>>y>>n;
		call(a,b,x,y,n);
		cout<<"\n";
	}
	return 0;
}


#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
#pragma GCC optimize("unroll-loops")

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;

/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template <typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
*/
#define fbo find_by_order
#define ook order_of_key

typedef pair<int,int> pi;
typedef pair<ll,ll> pl;
typedef vector<int> vl;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<double> vd;

typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<vl> vi;
typedef vector<vb> vbi;

template<class T> using V = vector<T>;
template<class T, size_t SZ> using AR = array<T,SZ>;
template<class T> using pq = priority_queue<T, vector<T>, greater<T>>;

#define ts to_string
#define fe first
#define se second
#define all(x) (x).begin(), (x).end()
#define sz(v) ((int)(v).size())
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
#define mt make_tuple
#define eb emplace_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)

struct fhash {
    static uint64_t splitmix64(uint64_t x) {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
	}
    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
	}
};

vl split(string s){ vl v; stringstream ss(s); for (int i; ss >> i;) {
	v.pb(i); if (ss.peek() == ',') ss.ignore(); } return v; }

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll randint(ll a, ll b) {
	return uniform_int_distribution<ll>(a, b)(rng);}

template<class T> int fd(const T& n){  return (int)(n/pow(10,(int)log10(n))); } //first digit
bool pow2(int i){ return i&&(i&-i)==i;}
constexpr int pct(int x) { return __builtin_popcount(x); }
constexpr int bits(int x) { return 31-__builtin_clz(x); } // floor(log2(x))
ll gcd(ll a,ll b) {  return b?gcd(b,a%b):a;}

#define cbit(n, b) (((n) >> (b)) & 1)
#define sbit(n, b) ((n) | (static_cast<decay<decltype(n)>::type>(1) << (b)))
#define rbit(n, b) ((n) & ~(static_cast<decay<decltype(n)>::type>(1) << (b)))
#define fbit(n, b) ((n) ^ (static_cast<decay<decltype(n)>::type>(1) << (b)))

template<class T> bool chmin(T& a, const T& b) {
	return b < a? a = b, 1 : 0; }
template<class T> bool chmax(T& a, const T& b) {
	return a < b ? a = b, 1 : 0; }

template<class T> void remdup(vector<T>& v) {
	sort(all(v)); v.erase(unique(all(v)),end(v)); }
bool inc(int a,int b,int c){return a<=b&&b<=c;}

// TO_STRING
template <class A, class B, class C> string ts(tuple<A, B, C> p);
template <class A, class B, class C, class D> string ts(tuple<A, B, C, D> p);
string ts(char c) { return string(1,c); }
string ts(bool b) {
    #ifdef __APPLE__
		return b ? "true" : "false";
	#else
		return ts((int)b);
	#endif
}
string ts(const char* s) { return (string)s; }
string ts(string s) { return s; }
template<class A> string ts(complex<A> c) {
	stringstream ss; ss << c; return ss.str(); }
string ts(vector<bool> v) {
	string res = "{"; F0R(i,sz(v)) res += char('0'+v[i]);
	res += "}"; return res; }
template<size_t SZ> string ts(bitset<SZ> b) {
	string res = ""; F0R(i,sz(b)) res += char('0'+b[i]);
	return res; }
template<class A, class B> string ts(pair<A,B> p);
template<class T> string ts(T v) { // containers with begin(), end()
	#ifdef __APPLE__
		bool fst = 1; string res = "{";
		for (const auto& x: v) {
			if (!fst) res += ", ";
			fst = 0; res += ts(x);
		}
		res += "}"; return res;
	#else
		bool fst = 1; string res = "";
		for (const auto& x: v) {
			if (!fst) res += " ";
			fst = 0; res += ts(x);
		}
		return res;
	#endif
}
template<class A, class B> string ts(pair<A,B> p) {
	#ifdef __APPLE__
		return "("+ts(p.fe)+", "+ts(p.se)+")";
	#else
		return ts(p.fe)+" "+ts(p.se);
	#endif
}
template<class A, class B, class C> string ts(tuple<A, B, C> p) {
	#ifdef __APPLE__
	    return "("+ts(get<0>(p))+","+ts(get<1>(p))+","+ts(get<2>(p))+")";
	#else
	    return ts(get<0>(p))+" "+ts(get<1>(p))+" "+ts(get<2>(p));
	#endif
}
template <class A,class B, class C, class D> string ts(tuple<A, B, C, D> p) {
	#ifdef __APPLE__
	    return "("+ts(get<0>(p)) + ","+ts(get<1>(p))+","+ts(get<2>(p)) + ","+ts(get<3>(p)) + ")";
	#else
	    return ts(get<0>(p))+" "+ts(get<1>(p))+" "+ts(get<2>(p))+" "+ts(get<3>(p));
	#endif
}
//INPUT
//void re(string& d){getline(cin,d) ;getline(cin,d); }
template <class A, class B, class C> void re(tuple<A,B,C>& p);
template <class A, class B, class C, class D> void re(tuple<A,B,C,D>& p);
template<class A> void re(complex<A>& c);
template<class A, class B> void re(pair<A,B>& p);
template<class A> void re(vector<A>& v);
template<class A, size_t SZ> void re(array<A,SZ>& a);

template<class T> void re(T& x) { cin >> x; }
void re(double& d) { string t; re(t); d = stod(t); }
void re(ld& d) { string t; re(t); d = stold(t); }
template<class H, class... T> void re(H& h, T&... t) { re(h); re(t...); }
template<class A> void re(complex<A>& c) { A a,b; re(a,b); c = {a,b}; }
template<class A, class B> void re(pair<A,B>& p) { re(p.fe,p.se); }
template<class A> void re(vector<A>& x) { trav(a,x) re(a);}
template<class A, size_t SZ> void re(array<A,SZ>& x) { trav(a,x) re(a); }
template <class A, class B, class C> void re(tuple<A,B,C>& p){re(get<0>(p),get<1>(p),get<2>(p));}
template <class A, class B, class C, class D> void re(tuple<A,B,C,D>& p){re(get<0>(p),get<1>(p),get<2>(p),get<3>(p));}
//OUTPUT
template<class A> void pr(A x) { cout << ts(x); }
template<class H, class... T> void pr(const H& h, const T&... t) {
	pr(h); pr(t...); }
void ps() { pr("\n"); } // print w/ spaces
template<class H, class... T> void ps(const H& h, const T&... t) {
	pr(h); if (sizeof...(t)) pr(" "); ps(t...); }
//DEBUG
void DBG() { cerr << "]" << endl; }
template<class H, class... T> void DBG(H h, T... t) {
	cerr << ts(h); if (sizeof...(t)) cerr << ", ";
	DBG(t...); }
#ifdef __APPLE__ //chk -> fake assert
	#define dbg(...) cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
	#define chk(...) if (!(__VA_ARGS__)) cerr << "Line(" << __LINE__ << ") -> function(" \
		 << __FUNCTION__  << ") -> CHK FAILED: (" << #__VA_ARGS__ << ")" << "\n", exit(0);
#else
	#define dbg(...) 0
	#define chk(...) 0
#endif

ll fstTrue(function<bool(ll)> f, ll lb, ll rb) { //find first such that the condition is true
	while(lb<rb) {
		ll mb=(lb+rb)/2;
		f(mb)?rb=mb:lb=mb+1;
	}
	return lb;
}
ll lstTrue(function<bool(ll)> f, ll lb, ll rb) {  //find last such that the condition is true
	while(lb<rb) {
		ll mb=(lb+rb+1)/2;
		f(mb)?lb=mb:rb=mb-1;
	}
	return lb;
}

const int di[4]={-1, 0, 1, 0}, dj[4]={0, 1, 0, -1};
const int di8[8]={-1, -1, 0, 1, 1, 1, 0, -1}, dj8[8]={0, 1, 1, 1, 0, -1, -1, -1};
//const int MOD = 1e9+7, MOD2 =998244353;
const ld PI = acos((ld)-1);

void setIn(string s) { freopen(s.c_str(),"r",stdin); }
void setOut(string s) { freopen(s.c_str(),"w",stdout); }

void IOS(int n=10, string s=""){
  ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  cout.precision(n); cerr.precision(n); cerr << fixed; cout << fixed;
  //cin.exceptions(cin.failbit);
  if (sz(s)) { setIn(s+".in"), setOut(s+".out"); }
}

//const int mxN =2e5;

void solve(){
	ll a,b,x,y,n; re(a,b,x,y,n);
	if(max(a-n,x)>max(b-n,y)){
		swap(a,b);
		swap(x,y);
	}
	if(n>=(a-x)+(b-y)){
		ps((ll)x*y);
	}
	else if(a-n>=x){
		ps((ll)(a-n)*b);
	}else{
		ll d= a-x;
		n-=d;
		ps((ll)(a-n)*(b-d));
	}
}
int main(){
	IOS();
	int t=1; re(t);
	F0R(i,t){
		//pr("Case #",i+1,": ");
		solve();
	}
#ifdef __APPLE__
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
}