#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <cstdint>
using namespace std;
typedef long long ll;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define mod 1000000007
#define rep(i,n) for(int i = 0; i < (int)(n); ++i)
int n,m;

bool ok(int i,int j){
    if(i<0||i>=n)return false;
    if(j<0||j>=m)return false;
    return true;
}
class UnionFind {
private:
    int sz;
    vector<int> par, size_;
public:
    UnionFind(){}
    UnionFind(int node_size) : sz(node_size), par(sz), size_(sz, 1){
        iota(par.begin(), par.end(), 0);
    }
    int find(int x){
        if(par[x] == x) return x;
        else return par[x] = find(par[x]);
    }
    void unite(int x,int y){
        x = find(x), y = find(y);
        if(x == y) return;
        if(size_[x] < size_[y]) swap(x,y);
        par[y] = x;
        size_[x] += size_[y];
    }
    int size(int x){
        x = find(x);
        return size_[x];
    }
    bool same(int x,int y){
        return find(x) == find(y);
    }
};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> n >> m;
    vector<string>s(n);
    vector<vector<int> >d(n,vector<int>(m));
    rep(i,n)cin >> s[i];
    vector<vector<bool>> flag(n,vector<bool>(m));
    queue<pair<pair<int,int>,int> > q;
    UnionFind uf(n*m);
    rep(i,n){
        q.push(MP(MP(i,-1),0));
        q.push(MP(MP(i,m),0));
    }
    rep(j,m){
        q.push(MP(MP(-1,j),0));
        q.push(MP(MP(n,j),0));
    }
    rep(i,n){
        rep(j,m){
            if(s[i][j]=='.'){
                flag[i][j] = true;
                q.push(MP(MP(i,j),0));
            }
        }
    }
    while(!q.empty()){
        auto x = q.front();
        q.pop();
        int i = x.first.first;
        int j = x.first.second;
        int t = x.second;
        if(ok(i,j))d[i][j] = t;
        for(int di=-1;di<=1;di++){
            for(int dj=-1;dj<=1;dj++){
                if(ok(i+di,j+dj)){
                    if(ok(i,j)&&(di==0||dj==0)&&s[i][j]=='X'&&s[i+di][j+dj]=='X'){
                        uf.unite(i*m+j,(i+di)*m+j+dj);
                    }
                    if(!flag[i+di][j+dj]){
                        flag[i+di][j+dj] = true;
                        q.push(MP(MP(i+di,j+dj),t+1));
                    }
                }
            }
        }
    }
    vector<int> res(n*m,-1);
    rep(i,n){
        rep(j,m){
            if(s[i][j]=='X'){
                int K = uf.find(i*m+j);
                res[K] = max(d[i][j],res[K]);
            }
        }
    }
    int p = inf;
    rep(i,n*m){
        if(res[i]!=-1){
            p = min(res[i],p);
        }
    }
    //cout << p-1 << endl;
    int OK = 1;
    int NG = p+1;
    vector<string > t(n);
    while(NG-OK>1){
        int mid = (OK+NG)/2;
        string ppp;
        rep(j,m){
            ppp.push_back('.');
        }
        rep(i,n){
            t[i] = ppp;
        }
        rep(i,n){
            rep(j,m){
                if(s[i][j]=='X'&&d[i][j]>=mid){
                    t[i][j] = 'X';
                }else{
                    t[i][j] = '.';
                }
            }
        }
        rep(i,n){
            rep(j,m){
                flag[i][j] = false;
            }
        }
        rep(i,n){
            rep(j,m){
                if(t[i][j]=='X'){
                    flag[i][j] = true;
                    q.push(MP(MP(i,j),1));
                }
            }
        }
        while(!q.empty()){
            auto x = q.front();
            q.pop();
            int i = x.first.first;
            int j = x.first.second;
            int tm = x.second;
            t[i][j] = 'X';
            if(tm==mid)continue;
            for(int di=-1;di<=1;di++){
                for(int dj=-1;dj<=1;dj++){
                    if(ok(i+di,j+dj)){
                        if(!flag[i+di][j+dj]){
                            flag[i+di][j+dj] = true;
                            q.push(MP(MP(i+di,j+dj),tm+1));
                        }
                    }
                }
            }
        }
        bool flag = 1;
        rep(i,n){
            if(s[i]!=t[i]){
                flag = 0;
                break;
            }
        }
        if(flag){
            OK = mid;
        }else{
            NG = mid;
        }
    }
    cout << OK-1 << endl;
    rep(i,n){
        rep(j,m){
            if(s[i][j]=='X'&&d[i][j]>=OK){
                cout << 'X';
            }else{
                cout << '.';
            }
        }
        cout << "\n";
    }
    // rep(i,n){
    //     rep(j,m){
    //         if(s[i][j]=='X'){
    //             cout << uf.find(i*m+j);
    //         }else{
    //             cout << '.';
    //         }
    //     }
    //     cout << "\n";
    // }
    return 0;
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <cstdint>
using namespace std;
typedef long long ll;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define mod 1000000007
#define rep(i,n) for(int i = 0; i < (int)(n); ++i)
int n,m;

bool ok(int i,int j){
    if(i<0||i>=n)return false;
    if(j<0||j>=m)return false;
    return true;
}
class UnionFind {
private:
    int sz;
    vector<int> par, size_;
public:
    UnionFind(){}
    UnionFind(int node_size) : sz(node_size), par(sz), size_(sz, 1){
        iota(par.begin(), par.end(), 0);
    }
    int find(int x){
        if(par[x] == x) return x;
        else return par[x] = find(par[x]);
    }
    void unite(int x,int y){
        x = find(x), y = find(y);
        if(x == y) return;
        if(size_[x] < size_[y]) swap(x,y);
        par[y] = x;
        size_[x] += size_[y];
    }
    int size(int x){
        x = find(x);
        return size_[x];
    }
    bool same(int x,int y){
        return find(x) == find(y);
    }
};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> n >> m;
    vector<string>s(n);
    vector<vector<int> >d(n,vector<int>(m));
    rep(i,n)cin >> s[i];
    vector<vector<bool>> flag(n,vector<bool>(m));
    queue<pair<pair<int,int>,int> > q;
    UnionFind uf(n*m);
    rep(i,n){
        q.push(MP(MP(i,-1),0));
        q.push(MP(MP(i,m),0));
    }
    rep(j,m){
        q.push(MP(MP(-1,j),0));
        q.push(MP(MP(n,j),0));
    }
    rep(i,n){
        rep(j,m){
            if(s[i][j]=='.'){
                flag[i][j] = true;
                q.push(MP(MP(i,j),0));
            }
        }
    }
    while(!q.empty()){
        auto x = q.front();
        q.pop();
        int i = x.first.first;
        int j = x.first.second;
        int t = x.second;
        if(ok(i,j))d[i][j] = t;
        for(int di=-1;di<=1;di++){
            for(int dj=-1;dj<=1;dj++){
                if(ok(i+di,j+dj)){
                    if(ok(i,j)&&s[i][j]=='X'&&s[i+di][j+dj]=='X'){
                        uf.unite(i*m+j,(i+di)*m+j+dj);
                    }
                    if(!flag[i+di][j+dj]){
                        flag[i+di][j+dj] = true;
                        q.push(MP(MP(i+di,j+dj),t+1));
                    }
                }
            }
        }
    }
    vector<int> res(n*m,-1);
    rep(i,n){
        rep(j,m){
            if(s[i][j]=='X'){
                int K = uf.find(i*m+j);
                res[K] = max(d[i][j],res[K]);
            }
        }
    }
    int p = inf;
    rep(i,n*m){
        if(res[i]!=-1){
            p = min(res[i],p);
        }
    }
    cout << p-1 << endl;
    rep(i,n){
        rep(j,m){
            if(s[i][j]=='X'&&d[i][j]>=p){
                cout << 'X';
            }else{
                cout << '.';
            }
        }
        cout << "\n";
    }
    return 0;
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <cstdint>
using namespace std;
typedef long long ll;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define mod 1000000007
#define rep(i,n) for(int i = 0; i < (int)(n); ++i)
int n,m;

bool ok(int i,int j){
    if(i<0||i>=n)return false;
    if(j<0||j>=m)return false;
    return true;
}
class UnionFind {
private:
    int sz;
    vector<int> par, size_;
public:
    UnionFind(){}
    UnionFind(int node_size) : sz(node_size), par(sz), size_(sz, 1){
        iota(par.begin(), par.end(), 0);
    }
    int find(int x){
        if(par[x] == x) return x;
        else return par[x] = find(par[x]);
    }
    void unite(int x,int y){
        x = find(x), y = find(y);
        if(x == y) return;
        if(size_[x] < size_[y]) swap(x,y);
        par[y] = x;
        size_[x] += size_[y];
    }
    int size(int x){
        x = find(x);
        return size_[x];
    }
    bool same(int x,int y){
        return find(x) == find(y);
    }
};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> n >> m;
    vector<string>s(n);
    vector<vector<int> >d(n,vector<int>(m));
    rep(i,n)cin >> s[i];
    vector<vector<bool>> flag(n,vector<bool>(m));
    queue<pair<pair<int,int>,int> > q;
    UnionFind uf(n*m);
    rep(i,n){
        q.push(MP(MP(i,-1),0));
        q.push(MP(MP(i,m),0));
    }
    rep(j,m){
        q.push(MP(MP(-1,j),0));
        q.push(MP(MP(n,j),0));
    }
    rep(i,n){
        rep(j,m){
            if(s[i][j]=='.'){
                flag[i][j] = true;
                q.push(MP(MP(i,j),0));
            }
        }
    }
    while(!q.empty()){
        auto x = q.front();
        q.pop();
        int i = x.first.first;
        int j = x.first.second;
        int t = x.second;
        if(ok(i,j))d[i][j] = t;
        for(int di=-1;di<=1;di++){
            for(int dj=-1;dj<=1;dj++){
                if(ok(i+di,j+dj)){
                    if(ok(i,j)&&(di==0||dj==0)s[i][j]=='X'&&s[i+di][j+dj]=='X'){
                        uf.unite(i*m+j,(i+di)*m+j+dj);
                    }
                    if(!flag[i+di][j+dj]){
                        flag[i+di][j+dj] = true;
                        q.push(MP(MP(i+di,j+dj),t+1));
                    }
                }
            }
        }
    }
    vector<int> res(n*m,-1);
    rep(i,n){
        rep(j,m){
            if(s[i][j]=='X'){
                int K = uf.find(i*m+j);
                res[K] = max(d[i][j],res[K]);
            }
        }
    }
    int p = inf;
    rep(i,n*m){
        if(res[i]!=-1){
            p = min(res[i],p);
        }
    }
    cout << p-1 << endl;
    rep(i,n){
        rep(j,m){
            if(s[i][j]=='X'&&d[i][j]>=p){
                cout << 'X';
            }else{
                cout << '.';
            }
        }
        cout << "\n";
    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define db long double
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define all(a) a.begin(), a.end()
#include <ext/pb_ds/assoc_container.hpp> // Общий файл.
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;

vector<pair<int, int> > dir = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, -1}, {-1, 1}};

int n, m;

bool check(vector<vector<int> > &d, vector<vector<char> > &matrix, int x, vector<vector<int> > &spec){

	queue<pair<int, int> > q;

	for (int i=0;i<n;++i) for (int j=0;j<m;++j){
		spec[i][j] = 1e9;
	}

	for (int i=0; i < n; ++i) for (int j=0;j<m;++j){
		if (matrix[i][j] != '.' && d[i][j] >= x){
			q.push({i, j});
			spec[i][j] = 0;
		}
	}

	//cout << "=== " << x << " " << q.size() << endl;

	while (q.size()){
		pair<int, int> W = q.front();
		q.pop();
		for (int e=0; e < 8; ++e){
			int X = W.first+dir[e].first, Y = W.second+dir[e].second;
			if (X<0 || X >= n || Y < 0 || Y >= m || matrix[X][Y] == '.'){
				continue;
			}
			if (spec[X][Y] <= spec[W.first][W.second]+1) continue;
			spec[X][Y] = spec[W.first][W.second]+1;
			q.push({X, Y});
		}
	}

	for (int i=0;i<n;++i) for (int j=0;j<m;++j){
		if (matrix[i][j] != '.' && spec[i][j] > x) {
			//cout << "==== " << x << " " << i << " " << j << " " << spec[i][j] << endl;
			return false;
		}
	}

	return true;

}

int main(){
#ifdef LOCAL
	freopen("M_input.txt", "r", stdin);
	//freopen("M_output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> n >> m;
	vector<vector<char> > matrix(n);
	vector<vector<bool> > used(n);
	vector<vector<int> > d(n);
	vector<vector<int> > spec(n);

	for (int i=0; i < n; ++i){
		for (int j=0; j < m; ++j){
			char ch;
			cin >> ch;
			matrix[i].push_back(ch);
			used[i].push_back(false);
			d[i].push_back(0);
			spec[i].push_back(0);
		}
	}

	queue<pair<int, int> > q;
	for (int i=0; i < n; ++i) for (int j=0; j < m; ++j){
		if (matrix[i][j] == '.') continue;
		bool nei = false;
		for (int e=0; e < 8; ++e){
			int X = i+dir[e].first, Y = j+dir[e].second;
			if (X<0 || X >= n || Y < 0 || Y >= m || matrix[X][Y] == '.'){
				nei = true;
			}
		}
		if (!nei) {
			d[i][j] = 1e9;
		}
		else q.push({i, j});
	}

	while (q.size()){
		pair<int, int> W = q.front();
		q.pop();
		for (int e=0; e < 8; ++e){
			int X = W.first+dir[e].first, Y = W.second+dir[e].second;
			if (X<0 || X >= n || Y < 0 || Y >= m || matrix[X][Y] == '.'){
				continue;
			}
			if (d[X][Y] <= d[W.first][W.second]+1) continue;
			d[X][Y] = d[W.first][W.second]+1;
			q.push({X, Y});
		}
	}

	int L = 0, R = max(n, m) + 1;
	while (R-L>1){
		int M = (L+R)/2;
		if (check(d, matrix, M, spec)) L = M;
		else R = M;
	}

	cout << L << endl;

	for (int i=0;i<n;++i){
		for (int j=0;j<m;++j){
			if (matrix[i][j] == '.') cout << '.';
			else if (d[i][j] >= L) cout << 'X';
			else cout << '.';
		}
		cout << "\n";
	}

}


#include <bits/stdc++.h>
#define ll long long
#define db long double
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define all(a) a.begin(), a.end()
#include <ext/pb_ds/assoc_container.hpp> // Общий файл.
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;

vector<pair<int, int> > dir = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, -1}, {-1, 1}};

int n, m;

bool check(vector<vector<int> > &d, vector<vector<char> > &matrix, int x, vector<vector<int> > &spec){

	queue<pair<int, int> > q;

	for (int i=0;i<n;++i) for (int j=0;j<m;++j){
		spec[i][j] = 1e9;
	}

	for (int i=0; i < n; ++i) for (int j=0;j<m;++j){
		if (matrix[i][j] != '.' && d[i][j] >= x){
			q.push({i, j});
			spec[i][j] = 0;
		}
	}

	//cout << "=== " << x << " " << q.size() << endl;

	while (q.size()){
		pair<int, int> W = q.front();
		q.pop();
		for (int e=0; e < 8; ++e){
			int X = W.first+dir[e].first, Y = W.second+dir[e].second;
			if (X<0 || X >= n || Y < 0 || Y >= m || matrix[X][Y] == '.'){
				continue;
			}
			if (spec[X][Y] <= spec[W.first][W.second]+1) continue;
			spec[X][Y] = spec[W.first][W.second]+1;
			q.push({X, Y});
		}
	}

	for (int i=0;i<n;++i) for (int j=0;j<m;++j){
		if (matrix[i][j] != '.' && spec[i][j] > x) {
			//cout << "==== " << x << " " << i << " " << j << " " << spec[i][j] << endl;
			return false;
		}
	}

	return true;

}

int main(){
#ifdef LOCAL
	freopen("M_input.txt", "r", stdin);
	//freopen("M_output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> n >> m;
	vector<vector<char> > matrix(n);
	vector<vector<bool> > used(n);
	vector<vector<int> > d(n);
	vector<vector<int> > spec(n);

	for (int i=0; i < n; ++i){
		for (int j=0; j < m; ++j){
			char ch;
			cin >> ch;
			matrix[i].push_back(ch);
			used[i].push_back(false);
			d[i].push_back(0);
			spec[i].push_back(0);
		}
	}

	queue<pair<int, int> > q;
	for (int i=0; i < n; ++i) for (int j=0; j < m; ++j){
		if (matrix[i][j] == '.') continue;
		bool nei = false;
		for (int e=0; e < 8; ++e){
			int X = i+dir[e].first, Y = j+dir[e].second;
			if (X<0 || X >= n || Y < 0 || Y >= m || matrix[X][Y] == '.'){
				nei = true;
			}
		}
		if (!nei) {
			d[i][j] = 1e9;
		}
		else q.push({i, j});
	}

	while (q.size()){
		pair<int, int> W = q.front();
		q.pop();
		for (int e=0; e < 8; ++e){
			int X = W.first+dir[e].first, Y = W.second+dir[e].second;
			if (X<0 || X >= n || Y < 0 || Y >= m || matrix[X][Y] == '.'){
				continue;
			}
			if (d[X][Y] <= d[W.first][W.second]+1) continue;
			d[X][Y] = d[W.first][W.second]+1;
			q.push({X, Y});
		}
	}

	int L = 0, R = max(n, m) + 1;
	while (R-L>1){
		int M = (L+R)/2;
		if (check(d, matrix, M, spec)) L = M;
		else R = M;
	}

	cout << L << endl;

	for (int i=0;i<n;++i){
		for (int j=0;j<m;++j){
			if (matrix[i][j] == '.') cout << '.';
			else if (d[i][j] >= L) cout << 'X';
			else cout << '.';
		}
		cout << endl;
	}

}


#include <bits/stdc++.h>
#define ll long long
#define db long double
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define all(a) a.begin(), a.end()
#include <ext/pb_ds/assoc_container.hpp> // Общий файл.
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;

vector<pair<int, int> > dir = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, -1}, {-1, 1}};

int n, m;

bool check(vector<vector<int> > &d, vector<vector<char> > &matrix, int x, vector<vector<int> > &spec){

	queue<pair<int, int> > q;

	for (int i=0;i<n;++i) for (int j=0;j<m;++j){
		spec[i][j] = 1e9;
	}

	for (int i=0; i < n; ++i) for (int j=0;j<m;++j){
		if (matrix[i][j] != '.' && d[i][j] >= x){
			q.push({i, j});
			spec[i][j] = 0;
		}
	}

	//cout << "=== " << x << " " << q.size() << endl;

	while (q.size()){
		pair<int, int> W = q.front();
		q.pop();
		for (int e=0; e < 8; ++e){
			int X = W.first+dir[e].first, Y = W.second+dir[e].second;
			if (X<0 || X >= n || Y < 0 || Y >= m || matrix[X][Y] == '.'){
				continue;
			}
			if (spec[X][Y] <= spec[W.first][W.second]+1) continue;
			spec[X][Y] = spec[W.first][W.second]+1;
			q.push({X, Y});
		}
	}

	for (int i=0;i<n;++i) for (int j=0;j<m;++j){
		if (matrix[i][j] != '.' && spec[i][j] > x) {
			//cout << "==== " << x << " " << i << " " << j << " " << spec[i][j] << endl;
			return false;
		}
	}

	return true;

}

int main(){
#ifdef LOCAL
	freopen("M_input.txt", "r", stdin);
	//freopen("M_output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> n >> m;
	vector<vector<char> > matrix(n);
	vector<vector<bool> > used(n);
	vector<vector<int> > d(n);
	vector<vector<int> > spec(n);

	for (int i=0; i < n; ++i){
		for (int j=0; j < m; ++j){
			char ch;
			cin >> ch;
			matrix[i].push_back(ch);
			used[i].push_back(false);
			d[i].push_back(0);
			spec[i].push_back(0);
		}
	}

	queue<pair<int, int> > q;
	for (int i=0; i < n; ++i) for (int j=0; j < m; ++j){
		if (matrix[i][j] == '.') continue;
		bool nei = false;
		for (int e=0; e < 8; ++e){
			int X = i+dir[e].first, Y = j+dir[e].second;
			if (X<0 || X >= n || Y < 0 || Y >= m || matrix[X][Y] == '.'){
				nei = true;
			}
		}
		if (!nei) {
			d[i][j] = 1e9;
		}
		else q.push({i, j});
	}

	while (q.size()){
		pair<int, int> W = q.front();
		q.pop();
		for (int e=0; e < 8; ++e){
			int X = W.first+dir[e].first, Y = W.second+dir[e].second;
			if (X<0 || X >= n || Y < 0 || Y >= m || matrix[X][Y] == '.'){
				continue;
			}
			if (d[X][Y] <= d[W.first][W.second]+1) continue;
			d[X][Y] = d[W.first][W.second]+1;
			q.push({X, Y});
		}
	}

	int L = 0, R = min(n, m) + 1;
	while (R-L>1){
		int M = (L+R)/2;
		if (check(d, matrix, M, spec)) L = M;
		else R = M;
	}

	cout << L << endl;

	for (int i=0;i<n;++i){
		for (int j=0;j<m;++j){
			if (matrix[i][j] == '.') cout << '.';
			else if (d[i][j] >= L) cout << 'X';
			else cout << '.';
		}
		cout << "\n";
	}

}


#include <bits/stdc++.h>
#define ll long long
#define db long double
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define all(a) a.begin(), a.end()

using namespace std;

vector<pair<int, int> > dir = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, -1}, {-1, 1}};

int n, m;

void dfs(int x, int y, vector<vector<int> > &d, vector<vector<char> > &matrix, vector<vector<bool> > &used, vector<vector<bool> > &larger) {
	used[x][y] = true;
	larger[x][y] = true;
	for (int e=0; e < 8; ++e){
		int X = x+dir[e].first, Y = y+dir[e].second;
		if (X<0 || X >= n || Y < 0 || Y >= m || matrix[X][Y] == '.'){
			continue;
		}
		if (d[X][Y] != d[x][y]){
			return;
		}
		if (!used[X][Y]){
			dfs(X, Y, d, matrix, used, larger);
		}
	}
}

int main(){
#ifdef LOCAL
	freopen("M_input.txt", "r", stdin);
	//freopen("M_output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> n >> m;
	vector<vector<char> > matrix(n);
	vector<vector<bool> > used(n);
	vector<vector<int> > d(n);
	vector<vector<bool> > larger(n);

	for (int i=0; i < n; ++i){
		for (int j=0; j < m; ++j){
			char ch;
			cin >> ch;
			matrix[i].push_back(ch);
			used[i].push_back(false);
			d[i].push_back(0);
			larger[i].push_back(false);
		}
	}

	queue<pair<int, int> > q;
	for (int i=0; i < n; ++i) for (int j=0; j < m; ++j){
		if (matrix[i][j] == '.') continue;
		bool nei = false;
		for (int e=0; e < 8; ++e){
			int X = i+dir[e].first, Y = j+dir[e].second;
			if (X<0 || X >= n || Y < 0 || Y >= m || matrix[X][Y] == '.'){
				nei = true;
			}
		}
		if (!nei) {
			d[i][j] = 1e9;
		}
		else q.push({i, j});
	}

	while (q.size()){
		pair<int, int> W = q.front();
		q.pop();
		for (int e=0; e < 8; ++e){
			int X = W.first+dir[e].first, Y = W.second+dir[e].second;
			if (X<0 || X >= n || Y < 0 || Y >= m || matrix[X][Y] == '.'){
				continue;
			}
			if (d[X][Y] <= d[W.first][W.second]+1) continue;
			d[X][Y] = d[W.first][W.second]+1;
			q.push({X, Y});
		}
	}

	int ans = max(n, m) + 1;

	for (int i=0;i<n;++i) for (int j=0;j<m;++j) used[i][j] = false;
	for (int i=0;i<n;++i){
		for (int j=0;j<m;++j){
			for (int e=0; e < 8; ++e){
				int X = i+dir[e].first, Y = j+dir[e].second;
				if (X<0 || X >= n || Y < 0 || Y >= m || matrix[X][Y] == '.'){
					continue;
				}
				if (d[X][Y] <= d[i][j]) continue;
				dfs(i, j, d, matrix, used, larger);
			}
		}
	}

	for (int i=0;i<n;++i){
		for (int j=0;j<m;++j){
			if (matrix[i][j] != 'X') continue;
			if (!larger[i][j]) ans = min(ans, d[i][j]);
		}
	}

	cout << ans << endl;

	for (int i=0;i<n;++i){
		for (int j=0;j<m;++j){
			if (matrix[i][j] == '.') cout << '.';
			else if (d[i][j] >= ans) cout << 'X';
			else cout << '.';
		}
		cout << endl;
	}

}


#include <bits/stdc++.h>
#define ll long long
#define db long double
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define all(a) a.begin(), a.end()
#include <ext/pb_ds/assoc_container.hpp> // Общий файл.
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;

vector<pair<int, int> > dir = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, -1}, {-1, 1}};

int n, m;

bool check(vector<vector<int> > &d, vector<vector<char> > &matrix, int x, vector<vector<int> > &spec){

	queue<pair<int, int> > q;

	for (int i=0;i<n;++i) for (int j=0;j<m;++j){
		spec[i][j] = 1e9;
	}

	for (int i=0; i < n; ++i) for (int j=0;j<m;++j){
		if (matrix[i][j] != '.' && d[i][j] >= x){
			q.push({i, j});
			spec[i][j] = 0;
		}
	}

	//cout << "=== " << x << " " << q.size() << endl;

	while (q.size()){
		pair<int, int> W = q.front();
		q.pop();
		for (int e=0; e < 8; ++e){
			int X = W.first+dir[e].first, Y = W.second+dir[e].second;
			if (X<0 || X >= n || Y < 0 || Y >= m || matrix[X][Y] == '.'){
				continue;
			}
			if (spec[X][Y] <= spec[W.first][W.second]+1) continue;
			spec[X][Y] = spec[W.first][W.second]+1;
			q.push({X, Y});
		}
	}

	for (int i=0;i<n;++i) for (int j=0;j<m;++j){
		if (matrix[i][j] != '.' && spec[i][j] > x) {
			//cout << "==== " << x << " " << i << " " << j << " " << spec[i][j] << endl;
			return false;
		}
	}

	return true;

}

int main(){
#ifdef LOCAL
	freopen("M_input.txt", "r", stdin);
	//freopen("M_output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> n >> m;
	vector<vector<char> > matrix(n);
	vector<vector<bool> > used(n);
	vector<vector<int> > d(n);
	vector<vector<int> > spec(n);

	for (int i=0; i < n; ++i){
		for (int j=0; j < m; ++j){
			char ch;
			cin >> ch;
			matrix[i].push_back(ch);
			used[i].push_back(false);
			d[i].push_back(0);
			spec[i].push_back(0);
		}
	}

	queue<pair<int, int> > q;
	for (int i=0; i < n; ++i) for (int j=0; j < m; ++j){
		if (matrix[i][j] == '.') continue;
		bool nei = false;
		for (int e=0; e < 8; ++e){
			int X = i+dir[e].first, Y = j+dir[e].second;
			if (X<0 || X >= n || Y < 0 || Y >= m || matrix[X][Y] == '.'){
				nei = true;
			}
		}
		if (!nei) {
			d[i][j] = 1e9;
		}
		else q.push({i, j});
	}

	while (q.size()){
		pair<int, int> W = q.front();
		q.pop();
		for (int e=0; e < 8; ++e){
			int X = W.first+dir[e].first, Y = W.second+dir[e].second;
			if (X<0 || X >= n || Y < 0 || Y >= m || matrix[X][Y] == '.'){
				continue;
			}
			if (d[X][Y] <= d[W.first][W.second]+1) continue;
			d[X][Y] = d[W.first][W.second]+1;
			q.push({X, Y});
		}
	}

	int L = 0, R = min(n, m) + 1;
	while (R-L>1){
		int M = (L+R)/2;
		if (check(d, matrix, M, spec)) L = M;
		else R = M;
	}

	cout << L << endl;

	for (int i=0;i<n;++i){
		for (int j=0;j<m;++j){
			if (matrix[i][j] == '.') cout << '.';
			else if (d[i][j] >= L) cout << 'X';
			else cout << '.';
		}
		cout << endl;
	}

}


#include <bits/stdc++.h>
using namespace std;
#define fst first
#define snd second
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
#define pb push_back
#define for_tests(t, tt) int t; scanf("%d", &t); for(int tt = 1; tt <= t; tt++)
#ifndef ONLINE_JUDGE
#define debug(args...) fprintf(stderr,args)
#else
#define debug(args...)
#endif //ONLINE_JUDGE
template<typename T> inline T abs(T t) { return t < 0? -t : t; }
const ll modn = 1000000007;
inline ll mod(ll x) { return x % modn; }

const int MAXN = 212345;

int n, m, k;

bool valid(int i, int j){
    return (i>= 0 && j>=0 && i < n && j < m);
}

bool full(int i, int j, int ii, int jj, vector<vector<int>> &acc){
    if(!valid(i, j) || !valid(ii, jj)) return false;
    int sum = acc[i][j];
    if(ii > 0)
        sum -= acc[ii-1][j];
    if(jj > 0)
        sum -= acc[i][jj-1];
    if(ii > 0 && jj > 0)
        sum += acc[ii-1][jj-1];
    //printf("full? %d %d %d %d sum %d mult %d\n", i, j, ii, jj, sum , (i - ii + 1)*(j - jj + 1));
    return (sum == (i - ii + 1)*(j - jj + 1));
}

vector<vector<char>> ans;

bool go(int k, vector<vector<int> > &M, vector<vector<int>> &acc){
    int cnt = 0;
    ans.clear();
    for(int a=0;a<n;a++)
        ans.pb(vector<char>(m, '.'));
    vector<vector<int>> aux(n, vector<int>(m, 0));
    //printf("test k %d\n", k);
    for(int a=0;a<n;a++){
        for(int b=0;b<m;b++){
            if(full(a+k, b+k, a-k, b-k, acc)){
                //printf("full %d %d %d %d\n", a+k, b+k, a-k, b-k);
                ans[a][b] = 'X';
                aux[a-k][b-k]++;
                if(valid(a-k, b+k+1))
                    aux[a-k][b+k+1]--;
                if(valid(a+k+1, b-k))
                    aux[a+k+1][b-k]--;
                if(valid(a+k+1, b+k+1))
                    aux[a+k+1][b+k+1]++;
            }
        }
    }
    for(int a=0;a<n;a++){
        for(int b=0;b<m;b++){
            if(a > 0)
                aux[a][b] += aux[a-1][b];
            if(b > 0)
                aux[a][b] += aux[a][b-1];
            if(a > 0 && b > 0)
                aux[a][b] -= aux[a-1][b-1];
            if((aux[a][b] > 0) != M[a][b]){
                //printf("a %d  b %d diff\n", a, b);
                return false;
            }
        }
    }
    return true;
}

int main (){
	scanf("%d%d", &n, &m);
    vector<vector<int>> M(n, vector<int>(m, 0));
    vector<vector<int>> acc(n, vector<int>(m, 0));
	for(int a=0;a<n;a++){
        for(int b=0;b<m;b++){
            char c;
            scanf(" %c", &c);
            M[a][b] = (c == 'X');
            acc[a][b] = M[a][b];
            if(a > 0 && b > 0)
                acc[a][b] -= acc[a-1][b-1];
            if(a > 0)
                acc[a][b] += acc[a-1][b];
            if(b > 0)
                acc[a][b] += acc[a][b-1];
        }
	}
    int i = 0, j = min(n, m);
    while(i < j){
        int m = (i + j +1)/2;
        if(go(m, M, acc))
            i = m;
        else
            j = m - 1;
    }
    go(i, M, acc);
    printf("%d\n", i);
    for(int a=0;a<n;a++){
        for(int b=0;b<m;b++){
            printf("%c", ans[a][b]);
        }
        puts("");
    }
}


#include <bits/stdc++.h>
using namespace std;
#define fst first
#define snd second
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
#define pb push_back
#define for_tests(t, tt) int t; scanf("%d", &t); for(int tt = 1; tt <= t; tt++)
#ifndef ONLINE_JUDGE
#define debug(args...) fprintf(stderr,args)
#else
#define debug(args...)
#endif //ONLINE_JUDGE
template<typename T> inline T abs(T t) { return t < 0? -t : t; }
const ll modn = 1000000007;
inline ll mod(ll x) { return x % modn; }

const int MAXN = 212345;

int n, m, k;

bool valid(int i, int j){
    return (i>= 0 && j>=0 && i < n && j < m);
}

bool full(int i, int j, int ii, int jj, vector<vector<int>> &acc){
    if(!valid(i, j) || !valid(ii, jj)) return false;
    int sum = acc[i][j];
    if(ii > 0)
        sum -= acc[ii-1][j];
    if(jj > 0)
        sum -= acc[i][jj-1];
    if(ii > 0 && j > 0)
        sum += acc[ii-1][jj-1];
    //printf("full? %d %d %d %d sum %d mult %d\n", i, j, ii, jj, sum , (i - ii + 1)*(j - jj + 1));
    return (sum == (i - ii + 1)*(j - jj + 1));
}

vector<vector<char>> ans;

bool go(int k, vector<vector<int> > &M, vector<vector<int>> &acc){
    int cnt = 0;
    ans.clear();
    for(int a=0;a<n;a++)
        ans.pb(vector<char>(m, '.'));
    vector<vector<int>> aux(n, vector<int>(m, 0));
    //printf("test k %d\n", k);
    for(int a=0;a<n;a++){
        for(int b=0;b<m;b++){
            if(full(a+k, b+k, a-k, b-k, acc)){
                //printf("full %d %d %d %d\n", a+k, b+k, a-k, b-k);
                ans[a][b] = 'X';
                aux[a-k][b-k]++;
                if(valid(a-k, b+k+1))
                    aux[a-k][b+k+1]--;
                if(valid(a+k+1, b-k))
                    aux[a+k+1][b-k]--;
                if(valid(a+k+1, b+k+1))
                    aux[a+k+1][b+k+1]++;
            }
        }
    }
    for(int a=0;a<n;a++){
        for(int b=0;b<m;b++){
            if(a > 0)
                aux[a][b] += aux[a-1][b];
            if(b > 0)
                aux[a][b] += aux[a][b-1];
            if(a > 0 && b > 0)
                aux[a][b] -= aux[a-1][b-1];
            if((aux[a][b] > 0) != M[a][b]){
                //printf("a %d  b %d diff\n", a, b);
                return false;
            }
        }
    }
    return true;
}

int main (){
	scanf("%d%d", &n, &m);
    vector<vector<int>> M(n, vector<int>(m, 0));
    vector<vector<int>> acc(n, vector<int>(m, 0));
	for(int a=0;a<n;a++){
        for(int b=0;b<m;b++){
            char c;
            scanf(" %c", &c);
            M[a][b] = (c == 'X');
            acc[a][b] = M[a][b];
            if(a > 0 && b > 0)
                acc[a][b] -= acc[a-1][b-1];
            if(a > 0)
                acc[a][b] += acc[a-1][b];
            if(b > 0)
                acc[a][b] += acc[a][b-1];
        }
	}
    int i = 0, j = min(n, m);
    while(i < j){
        int m = (i + j +1)/2;
        if(go(m, M, acc))
            i = m;
        else
            j = m - 1;
    }
    go(i, M, acc);
    printf("%d\n", i);
    for(int a=0;a<n;a++){
        for(int b=0;b<m;b++){
            printf("%c", ans[a][b]);
        }
        puts("");
    }
}


#include <bits/stdc++.h>
using namespace std;
#define fst first
#define snd second
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
#define pb push_back
#define for_tests(t, tt) int t; scanf("%d", &t); for(int tt = 1; tt <= t; tt++)
#ifndef ONLINE_JUDGE
#define debug(args...) fprintf(stderr,args)
#else
#define debug(args...)
#endif //ONLINE_JUDGE
template<typename T> inline T abs(T t) { return t < 0? -t : t; }
const ll modn = 1000000007;
inline ll mod(ll x) { return x % modn; }

const int MAXN = 212345;

int n, m, k;

bool valid(int i, int j){
    return (i>= 0 && j>=0 && i < n && j < m);
}

bool full(int i, int j, int ii, int jj, vector<vector<int>> &acc){
    if(!valid(i, j) || !valid(ii, jj)) return false;
    int sum = acc[i][j];
    if(ii > 0)
        sum -= acc[ii-1][j];
    if(jj > 0)
        sum -= acc[i][jj-1];
    if(ii > 0 && jj > 0)
        sum += acc[ii-1][jj-1];
    //printf("full? %d %d %d %d sum %d mult %d\n", i, j, ii, jj, sum , (i - ii + 1)*(j - jj + 1));
    return (sum == (i - ii + 1)*(j - jj + 1));
}

vector<vector<char>> ans;

bool go(int k, vector<vector<int> > &M, vector<vector<int>> &acc){
    int cnt = 0;
    ans.clear();
    for(int a=0;a<n;a++)
        ans.pb(vector<char>(m, '.'));
    vector<vector<int>> aux(n, vector<int>(m, 0));
    //printf("test k %d\n", k);
    for(int a=0;a<n;a++){
        for(int b=0;b<m;b++){
            if(full(a+k, b+k, a-k, b-k, acc)){
                //printf("full %d %d %d %d\n", a+k, b+k, a-k, b-k);
                ans[a][b] = 'X';
                aux[a-k][b-k]++;
                if(valid(a-k, b+k+1))
                    aux[a-k][b+k+1]--;
                if(valid(a+k+1, b-k))
                    aux[a+k+1][b-k]--;
                if(valid(a+k+1, b+k+1))
                    aux[a+k+1][b+k+1]++;
            }
        }
    }
    for(int a=0;a<n;a++){
        for(int b=0;b<m;b++){
            if(a > 0)
                aux[a][b] += aux[a-1][b];
            if(b > 0)
                aux[a][b] += aux[a][b-1];
            if(a > 0 && b > 0)
                aux[a][b] -= aux[a-1][b-1];
            if((aux[a][b] > 0) != M[a][b]){
                printf("a %d  b %d diff\n", a, b);
                return false;
            }
        }
    }
    return true;
}

int main (){
	scanf("%d%d", &n, &m);
    vector<vector<int>> M(n, vector<int>(m, 0));
    vector<vector<int>> acc(n, vector<int>(m, 0));
	for(int a=0;a<n;a++){
        for(int b=0;b<m;b++){
            char c;
            scanf(" %c", &c);
            M[a][b] = (c == 'X');
            acc[a][b] = M[a][b];
            if(a > 0 && b > 0)
                acc[a][b] -= acc[a-1][b-1];
            if(a > 0)
                acc[a][b] += acc[a-1][b];
            if(b > 0)
                acc[a][b] += acc[a][b-1];
        }
	}
    int i = 0, j = min(n, m);
    while(i < j){
        int m = (i + j +1)/2;
        if(go(m, M, acc))
            i = m;
        else
            j = m - 1;
    }
    go(i, M, acc);
    printf("%d\n", i);
    for(int a=0;a<n;a++){
        for(int b=0;b<m;b++){
            printf("%c", ans[a][b]);
        }
        puts("");
    }
}


#include <bits/stdc++.h>
using namespace std;
#define fst first
#define snd second
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
#define pb push_back
#define for_tests(t, tt) int t; scanf("%d", &t); for(int tt = 1; tt <= t; tt++)
#ifndef ONLINE_JUDGE
#define debug(args...) fprintf(stderr,args)
#else
#define debug(args...)
#endif //ONLINE_JUDGE
template<typename T> inline T abs(T t) { return t < 0? -t : t; }
const ll modn = 1000000007;
inline ll mod(ll x) { return x % modn; }

const int MAXN = 212345;

int n, m, k;
int t;

int vi[] = {-1, 1, 0, 0, -1, 1, -1, 1};
int vj[] = {0, 0, 1, -1, -1, 1, 1, -1};

vector<vector<int>> M;

queue<pii> q[2];

bool valid(int i, int j){
    return (i >= 0 && j >= 0 && i < n && j < m);
}

void rev(queue<pii> &fila){
    while(!fila.empty()){
        pii p = fila.front();
        M[p.fst][p.snd] = 1;
        fila.pop();
    }
}

bool inv(int i, int j){
    for(int d=0;d < 8;d++){
        int ii = i + vi[d], jj = j + vj[d];
        if(valid(ii, jj)){
            if(M[ii][jj]) return false;
        }
    }
    return true;
}

int go(){
    for(int t=0;;t++){
       int u = (t&1);
        while(!q[u].empty()){
            pii p = q[u].front();
            q[u].pop();
            int i = p.fst, j = p.snd;
            for(int d=0;d<8;d++){
                int ii = i + vi[d], jj = j + vj[d];
                if(valid(ii, jj) && M[ii][jj]){
                    M[ii][jj] = 0;
                    q[u^1].push({ii, jj});
                    if(inv(ii, jj)){
                        rev(q[u^1]);
                        return t;
                    }
                }
            }
        }
    }
}

int main (){
    scanf("%d%d", &n, &m);
    for(int a=0;a<n;a++){
        M.pb(vector<int>(m, 0));
    }
    for(int a=0;a<n;a++){
        q[0].push({a, -1});
        q[0].push({a, m});
    }
    for(int b=0;b<m;b++){
        q[0].push({-1, b});
        q[0].push({n, b});
    }
    for(int a=0;a<n;a++){
        for(int b=0;b<m;b++){
            char c;
            scanf(" %c", &c);
            M[a][b] = (c == 'X');
            if(!M[a][b])
                q[0].push({a, b});
        }
    }
    int ans = go();
    printf("%d\n", ans);
    for(int a=0;a<n;a++){
        for(int b=0;b<m;b++){
            if(M[a][b])
                printf("X");
            else
                printf(".");
        }
        puts("");
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef pair<int,int> pii;
typedef pair<lli,lli> pll;
void sc(){}
template<typename T,typename... Args>
void sc(T& a, Args&... args) {cin>>a,sc(args...);}
template<typename T>
void pr(T a){cout<<a;}
template<typename T,typename... Args>
void pr(T a, Args... args) {cout<<a<<" ",pr(args...);}
template<typename T>
void prl(T a){cout<<a<<"\n";}
template<typename T,typename... Args>
void prl(T a, Args... args) {cout<<a<<" ",prl(args...);}
template<typename T>
int sz(const T &a){return (int)a.size();}
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
int xc[8]={0,0,1,-1,1,-1,1,-1};
int yc[8]={1,-1,0,0,1,-1,-1,1};
int main(){
    cin.tie(NULL);
    ios_base::sync_with_stdio(false);
	int n,m;
	sc(n,m);
	vector<vector<char>> arr;
	vector<vector<int>> dist;
	vector<vector<int>> gone;
	arr.resize(n+2);
	dist.resize(n+2);
	gone.resize(n+2);
	rep(i,0,n+2)arr[i].resize(m+2,'.'),dist[i].resize(m+2,-1),gone[i].resize(m+2,false);
	rep(i,1,n+1){
		rep(j,1,m+1)sc(arr[i][j]);
	}
	queue<pair<int,pii>> q;
	rep(i,0,n+2)rep(j,0,m+2){
		if(arr[i][j]=='.'){
			dist[i][j]=0;
			q.push({0,{i,j}});
		}
	}
	while(sz(q)){
		auto cur=q.front();
		q.pop();
		rep(i,0,8){
			pii ne={cur.second.first+xc[i],cur.second.second+yc[i]};
			if(ne.first>=0&&ne.first<=n+1&&ne.second>=0&&ne.second<=m+1){
				if(dist[ne.first][ne.second]==-1){
					dist[ne.first][ne.second]=cur.first+1;
					q.push({cur.first+1,ne});
				}
			}
		}
	}
	queue<pii> qu;
	int time=INT_MAX;
	rep(i,0,n+2)rep(j,0,m+2){
		if(arr[i][j]=='X'&&!gone[i][j]){
			qu.push({i,j});
			int ma=0;
			while(sz(qu)){
				auto cur=qu.front();
				qu.pop();
				ma=max(ma,dist[cur.first][cur.second]);
				rep(i,0,8) {
					pii ne = {cur.first + xc[i], cur.second + yc[i]};
					if(ne.first>=0&&ne.first<=n+1&&ne.second>=0&&ne.second<=m+1) {
						if (arr[ne.first][ne.second] == 'X' && !gone[ne.first][ne.second]) {
							gone[ne.first][ne.second] = true;
							qu.push(ne);
						}
					}
				}
			}
			time=min(time,ma);
		}
	}
	prl(time-1);
	rep(i,1,n+1){
		rep(j,1,m+1){
			if(dist[i][j]>=time){
				pr('X');
			}
			else pr('.');
		}
		prl("");
	}
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef pair<int,int> pii;
typedef pair<lli,lli> pll;
void sc(){}
template<typename T,typename... Args>
void sc(T& a, Args&... args) {cin>>a,sc(args...);}
template<typename T>
void pr(T a){cout<<a;}
template<typename T,typename... Args>
void pr(T a, Args... args) {cout<<a<<" ",pr(args...);}
template<typename T>
void prl(T a){cout<<a<<"\n";}
template<typename T,typename... Args>
void prl(T a, Args... args) {cout<<a<<" ",prl(args...);}
template<typename T>
int sz(const T &a){return (int)a.size();}
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
int xc[4]={0,0,1,-1};
int yc[4]={1,-1,0,0};
int main(){
    cin.tie(NULL);
    ios_base::sync_with_stdio(false);
	int n,m;
	sc(n,m);
	vector<vector<char>> arr;
	vector<vector<int>> dist;
	vector<vector<int>> gone;
	arr.resize(n+2);
	dist.resize(n+2);
	gone.resize(n+2);
	rep(i,0,n+2)arr[i].resize(m+2,'.'),dist[i].resize(m+2,-1),gone[i].resize(m+2,false);
	rep(i,1,n+1){
		rep(j,1,m+1)sc(arr[i][j]);
	}
	queue<pair<int,pii>> q;
	rep(i,0,n+2)rep(j,0,m+2){
		if(arr[i][j]=='.'){
			dist[i][j]=0;
			q.push({0,{i,j}});
		}
	}
	while(sz(q)){
		auto cur=q.front();
		q.pop();
		rep(i,0,4){
			pii ne={cur.second.first+xc[i],cur.second.second+yc[i]};
			if(ne.first>=0&&ne.first<=n+1&&ne.second>=0&&ne.second<=m+1){
				if(dist[ne.first][ne.second]==-1){
					dist[ne.first][ne.second]=cur.first+1;
					q.push({cur.first+1,ne});
				}
			}
		}
	}
	queue<pii> qu;
	int time=INT_MAX;
	rep(i,0,n+2)rep(j,0,m+2){
		if(arr[i][j]=='X'&&!gone[i][j]){
			qu.push({i,j});
			int ma=0;
			while(sz(qu)){
				auto cur=qu.front();
				qu.pop();
				ma=max(ma,dist[cur.first][cur.second]);
				rep(i,0,4) {
					pii ne = {cur.first + xc[i], cur.second + yc[i]};
					if(arr[ne.first][ne.second]=='X'&&!gone[ne.first][ne.second]){
						gone[ne.first][ne.second]=true;
						qu.push(ne);
					}
				}
			}
			time=min(time,ma);
		}
	}
	prl(time-1);
	rep(i,1,n+1){
		rep(j,1,m+1){
			if(dist[i][j]>=time){
				pr('X');
			}
			else pr('.');
		}
		prl("");
	}
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef pair<int,int> pii;
typedef pair<lli,lli> pll;
void sc(){}
template<typename T,typename... Args>
void sc(T& a, Args&... args) {cin>>a,sc(args...);}
template<typename T>
void pr(T a){cout<<a;}
template<typename T,typename... Args>
void pr(T a, Args... args) {cout<<a<<" ",pr(args...);}
template<typename T>
void prl(T a){cout<<a<<"\n";}
template<typename T,typename... Args>
void prl(T a, Args... args) {cout<<a<<" ",prl(args...);}
template<typename T>
int sz(const T &a){return (int)a.size();}
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
int xc[8]={0,0,1,-1,1,-1,1,-1};
int yc[8]={1,-1,0,0,1,-1,-1,1};
vector<vector<char>> arr;
vector<vector<int>> dist;
vector<vector<bool>> gone;
vector<vector<bool>> gone2;
int n,m;
queue<pair<int,pii>> q;
queue<pii> qu;
vector<pair<int,pii>> th;
bool work(int dist){
	int loc=0;
	int goneto=0;
	for(auto x:th)gone2[x.second.first][x.second.second]=false;
	while(loc<sz(th)&&th[loc].first>=dist){
		q.push({0,th[loc].second});
		gone2[th[loc].second.first][th[loc].second.second]=true;
		loc++;
	}
	while(sz(q)){
		auto cur=q.front();
		q.pop();
		goneto+=1;
		if(cur.first+1>=dist)continue;
		rep(i,0,8) {
			pii ne = {cur.second.first + xc[i], cur.second.second + yc[i]};
			if(ne.first>=0&&ne.first<=n+1&&ne.second>=0&&ne.second<=m+1) {
				if (arr[ne.first][ne.second] == 'X' && !gone2[ne.first][ne.second]) {
					gone2[ne.first][ne.second] = true;
					q.push({cur.first+1,ne});
				}
			}
		}
	}
	if(goneto==sz(th))return true;
	return false;
}
int main(){
    cin.tie(NULL);
    ios_base::sync_with_stdio(false);
    setbuf(stdout,NULL);
	sc(n,m);
	arr.resize(n+2);
	dist.resize(n+2);
	gone.resize(n+2);
	gone2.resize(n+2);
	rep(i,0,n+2)arr[i].resize(m+2,'.'),dist[i].resize(m+2,-1),gone2[i].resize(m+2,false),gone[i].resize(m+2,false);
	rep(i,1,n+1){
		rep(j,1,m+1)sc(arr[i][j]);
	}
	rep(i,0,n+2)rep(j,0,m+2){
		if(arr[i][j]=='.'){
			dist[i][j]=0;
			q.push({0,{i,j}});
		}
	}
	while(sz(q)){
		auto cur=q.front();
		q.pop();
		rep(i,0,8){
			pii ne={cur.second.first+xc[i],cur.second.second+yc[i]};
			if(ne.first>=0&&ne.first<=n+1&&ne.second>=0&&ne.second<=m+1){
				if(dist[ne.first][ne.second]==-1){
					dist[ne.first][ne.second]=cur.first+1;
					q.push({cur.first+1,ne});
				}
			}
		}
	}
	int time=INT_MAX;
	rep(i,0,n+2)rep(j,0,m+2){
		if(arr[i][j]=='X'&&!gone[i][j]){
			qu.push({i,j});
			th.clear();
			gone[i][j]=true;
			while(sz(qu)){
				auto cur=qu.front();
				qu.pop();
				th.push_back({dist[cur.first][cur.second],cur});
				rep(i,0,8) {
					pii ne = {cur.first + xc[i], cur.second + yc[i]};
					if(ne.first>=0&&ne.first<=n+1&&ne.second>=0&&ne.second<=m+1) {
						if (arr[ne.first][ne.second] == 'X' && !gone[ne.first][ne.second]) {
							gone[ne.first][ne.second] = true;
							qu.push(ne);
						}
					}
				}
			}
			sort(th.begin(),th.end(),greater<pair<int,pii>>());
			int lo=1;
			int hi=max(m,n);
			while(lo!=hi){
				int mid=(lo+hi)/2+1;
				if(work(mid))lo=mid;
				else hi=mid-1;
			}
			time=min(time,lo);
		}
	}
	prl(time-1);
	rep(i,1,n+1){
		rep(j,1,m+1){
			if(dist[i][j]>=time){
				pr('X');
			}
			else pr('.');
		}
		prl("");
	}
    return 0;
}
/*
7 5
XXX..
XXX..
XXX..
..X..
..XXX
..XXX
..XXX
 */


#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef pair<int,int> pii;
typedef pair<lli,lli> pll;
void sc(){}
template<typename T,typename... Args>
void sc(T& a, Args&... args) {cin>>a,sc(args...);}
template<typename T>
void pr(T a){cout<<a;}
template<typename T,typename... Args>
void pr(T a, Args... args) {cout<<a<<" ",pr(args...);}
template<typename T>
void prl(T a){cout<<a<<"\n";}
template<typename T,typename... Args>
void prl(T a, Args... args) {cout<<a<<" ",prl(args...);}
template<typename T>
int sz(const T &a){return (int)a.size();}
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
int xc[8]={0,0,1,-1,1,-1,1,-1};
int yc[8]={1,-1,0,0,1,-1,-1,1};
int main(){
    cin.tie(NULL);
    ios_base::sync_with_stdio(false);
	int n,m;
	sc(n,m);
	vector<vector<char>> arr;
	vector<vector<int>> dist;
	vector<vector<int>> gone;
	arr.resize(n+2);
	dist.resize(n+2);
	gone.resize(n+2);
	rep(i,0,n+2)arr[i].resize(m+2,'.'),dist[i].resize(m+2,-1),gone[i].resize(m+2,false);
	rep(i,1,n+1){
		rep(j,1,m+1)sc(arr[i][j]);
	}
	queue<pair<int,pii>> q;
	rep(i,0,n+2)rep(j,0,m+2){
		if(arr[i][j]=='.'){
			dist[i][j]=0;
			q.push({0,{i,j}});
		}
	}
	while(sz(q)){
		auto cur=q.front();
		q.pop();
		rep(i,0,8){
			pii ne={cur.second.first+xc[i],cur.second.second+yc[i]};
			if(ne.first>=0&&ne.first<=n+1&&ne.second>=0&&ne.second<=m+1){
				if(dist[ne.first][ne.second]==-1){
					dist[ne.first][ne.second]=cur.first+1;
					q.push({cur.first+1,ne});
				}
			}
		}
	}
	queue<pii> qu;
	int time=INT_MAX;
	rep(i,0,n+2)rep(j,0,m+2){
		if(arr[i][j]=='X'&&!gone[i][j]){
			qu.push({i,j});
			int ma=0;
			while(sz(qu)){
				auto cur=qu.front();
				qu.pop();
				ma=max(ma,dist[cur.first][cur.second]);
				rep(i,0,8) {
					pii ne = {cur.first + xc[i], cur.second + yc[i]};
					if(arr[ne.first][ne.second]=='X'&&!gone[ne.first][ne.second]){
						gone[ne.first][ne.second]=true;
						qu.push(ne);
					}
				}
			}
			time=min(time,ma);
		}
	}
	prl(time-1);
	rep(i,1,n+1){
		rep(j,1,m+1){
			if(dist[i][j]>=time){
				pr('X');
			}
			else pr('.');
		}
		prl("");
	}
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

template<class T, class U>
void ckmin(T &a, U b)
{
	if (a > b) a = b;
}
template<class T, class U>
void ckmax(T &a, U b)
{
	if (a < b) a = b;
}

#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
#define SZ(x) ((int) ((x).size()))
#define ALL(x) (x).begin(), (x).end()
#define MAXN 1000013

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;

int N, M;
vector<bool> grid[MAXN];
vi sum[MAXN];
vi rt[MAXN], dn[MAXN];
int ans = 998244353;

int32_t main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	cout << fixed << setprecision(12);
	cerr << fixed << setprecision(4);
	cin >> N >> M;
	FOR(i, 0, N)
	{
		string temps; cin >> temps;
		grid[i].resize(M);
		rt[i].resize(M);
		dn[i].resize(M);
		sum[i].resize(M + 1);
		FOR(j, 0, M)
		{
			grid[i][j] = (temps[j] == 'X');
		}
	}
	sum[N].resize(M + 1);
	FOR(i, 0, N)
	{
		FOR(j, 0, M)
		{
			sum[i + 1][j + 1] = grid[i][j] + sum[i][j + 1] + sum[i + 1][j] - sum[i][j];
		}
	}
	FORD(i, N, 0)
	{
		FORD(j, M, 0)
		{
			rt[i][j] = (grid[i][j] ? (1 + (j == M - 1 ? 0 : rt[i][j + 1])) : 0);
			dn[i][j] = (grid[i][j] ? 1 + (i == N - 1 ? 0 : dn[i + 1][j]) : 0);
		}
	}
	FOR(i, 0, N)
	{
		FOR(j, 0, M)
		{
			if (!grid[i][j]) continue;
			if (j == 0 || !grid[i][j - 1])
			{
				ckmin(ans, (rt[i][j] - 1) / 2);
			}
			if (i == 0 || !grid[i - 1][j])
			{
				ckmin(ans, (dn[i][j] - 1) / 2);
			}
		}
	}
	cout << ans << '\n';
	int sz = (2 * ans + 1); sz *= sz;
	FOR(i, 0, N)
	{
		FOR(j, 0, M)
		{
			if (i - ans < 0 || j - ans < 0 || i + ans >= N || j + ans >= M)
			{
				cout << '.';
				continue;
			}
			int cur = sum[i + ans + 1][j + ans + 1] - sum[i - ans][j + ans + 1] - sum[i + ans + 1][j - ans] + sum[i - ans][j - ans];
			cout << (cur == sz ? 'X' : '.');
		}
		cout << '\n';
	}
	return 0;
	//bleh now just do prefix sums.
	//you overlayed a bunch of squares of size 2x+1 to get the picture. what's maximum value of x?
}


#include <bits/stdc++.h>

using namespace std;

template<class T, class U>
void ckmin(T &a, U b)
{
	if (a > b) a = b;
}
template<class T, class U>
void ckmax(T &a, U b)
{
	if (a < b) a = b;
}

#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
#define SZ(x) ((int) ((x).size()))
#define ALL(x) (x).begin(), (x).end()
#define MAXN 1000013

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;

int N, M;
vector<bool> grid[MAXN], res[MAXN];
vi sum[MAXN], chk[MAXN];

bool check(int x)
{
	// cerr << "CHECK " << x << endl;
	int sz = (2 * x + 1) * (2 * x + 1);
	FOR(i, 0, N)
	{
		FOR(j, 0, M)
		{
			chk[i][j] = 0;
			res[i][j] = false;
		}
	}
	FOR(i, 0, N)
	{
		FOR(j, 0, M)
		{
			// cerr << "HEY " << i << ' ' << j << endl;
			// cerr << i - x << ' ' << j - x << ' ' << i + x << ' ' << j + x << endl;
			if (i - x < 0 || j - x < 0 || i + x >= N || j + x >= M)
			{
				res[i][j] = 0;
				continue;
			}
			// cerr << "HEY " << i << ' ' << j << endl;
			int cur = sum[i + x + 1][j + x + 1] - sum[i - x][j + x + 1] - sum[i + x + 1][j - x] + sum[i - x][j - x];
			if (cur == sz)
			{
				res[i][j] = true;
				chk[i - x][j - x]++; chk[i - x][j + x + 1]--; chk[i + x + 1][j - x]--; chk[i + x + 1][j + x + 1]++;
			}
		}
	}
	// FOR(i, 0, N)
	// {
	// 	FOR(j, 0, M)
	// 	{
	// 		cerr << res[i][j];
	// 	}
	// 	cerr << endl;
	// }
	FOR(i, 0, N)
	{
		FOR(j, 0, M)
		{
			int sum = chk[i][j];
			if (i != 0) sum += chk[i - 1][j];
			if (j != 0) sum += chk[i][j - 1];
			if (i != 0 && j != 0) sum -= chk[i - 1][j - 1];
			chk[i][j] = sum;
			// cerr << sum << ' ';
			if (sum > 0 && !grid[i][j]) return false;
			if (sum == 0 && grid[i][j]) return false;
		}
		// cerr << endl;
	}
	return true;
}

int32_t main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	cout << fixed << setprecision(12);
	cerr << fixed << setprecision(4);
	cin >> N >> M;
	FOR(i, 0, N)
	{
		string temps; cin >> temps;
		grid[i].resize(M);
		sum[i].resize(M + 1);
		chk[i].resize(M + 1);
		res[i].resize(M);
		FOR(j, 0, M)
		{
			grid[i][j] = (temps[j] == 'X');
		}
	}
	sum[N].resize(M + 1);
	chk[N].resize(M + 1);
	FOR(i, 0, N)
	{
		FOR(j, 0, M)
		{
			sum[i + 1][j + 1] = grid[i][j] + sum[i][j + 1] + sum[i + 1][j] - sum[i][j];
		}
	}
	int lo = 0, hi = (min(N, M) - 1) / 2;
	while(hi > lo)
	{
		int mid = (lo + hi + 1) >> 1;
		if (check(mid))
		{
			lo = mid;
		}
		else
		{
			hi = mid - 1;
		}
	}
	check(lo);
	cout << lo << '\n';
	FOR(i, 0, N)
	{
		FOR(j, 0, M)
		{
			cout << (res[i][j] ? 'X' : '.');
		}
		cout << '\n';
	}
	return 0;
	//bleh now just do prefix sums.
	//you overlayed a bunch of squares of size 2x+1 to get the picture. what's maximum value of x?
}


#include <bits/stdc++.h>
using namespace std;
int di[] = {1,1,1,0,0,-1,-1,-1}, dj[] = {0,1,-1,1,-1,0,1,-1}, n, m;
bool valid(int i, int j) {
	return 0 <= i && i < n && 0 <= j && j < m;
}
int main() {
	ios::sync_with_stdio(0);cin.tie(0);
	cin >> n >> m;
	vector<string> arr(n);
	vector<vector<int>> dist(n, vector<int>(m, 0x3f3f3f3f));
	vector<vector<char>> seen(n, vector<char>(m, 0));
	queue<pair<int, int>> que;
	auto push = [&](int i, int j, int d) {
		dist[i][j] = min(dist[i][j], d);
		if(!seen[i][j]) {
			//printf("%d,%d pushed with dist %d\n", i, j, d);
			seen[i][j] = 1;
			que.push({i,j});
		}
	};
	for(int i=0; i<n; i++) {
		cin >> arr[i];
	}
	for(int i=0; i<n; i++) {
		for(int j=0; j<m; j++) {
			if(arr[i][j] == '.') {
				seen[i][j] = 1;
				dist[i][j] = 0;
				for(int k=0; k<8; k++) {
					int ti = i+di[k], tj = j+dj[k];
					if(valid(ti,tj) &&  arr[ti][tj] == 'X') {
						push(ti, tj, 1);
					}
				}
			}
			if((!i || !j || i == n-1 || j == m-1) && arr[i][j] == 'X') {
				push(i, j, 1);
			}
		}

	}
	//printf("after queue\n");
	while(!que.empty()) {
		int i, j;
		tie(i, j) = que.front();
		que.pop();
		for(int k=0; k<8; k++) {
			int ti = i+di[k], tj = j+dj[k];
			if(valid(ti,tj) &&  arr[ti][tj] == 'X') {
				push(ti, tj, dist[i][j]+1);
			}
		}
	}
	// seen = vector<vector<char>>(n, vector<char>(m, 0));
	// function<int(int,int)> dfs = [&](int i, int j) {
	// 	int ret = dist[i][j];
	// 	for(int k=0; k<8; k++) {
	// 		int ti = i+di[k], tj = j+dj[k];
	// 		if(valid(ti, tj) && arr[ti][tj] == 'X' && !seen[ti][tj]) {
	// 			seen[ti][tj] = 1;
	// 			ret = max(ret, dfs(ti, tj));
	// 		}
	// 	}
	// 	return ret;
	// };
	// int ans = INT_MAX;
	// for(int i=0; i<n; i++) {
	// 	for(int j=0; j<m; j++) {
	// 		if(arr[i][j] == 'X' && !seen[i][j]) {
	// 			ans = min(ans, dfs(i, j));
	// 		}
	// 	}
	// }
	int ans = INT_MAX;
	for(int i=0; i<n; i++) {
		int k = 0;
		for(int j=0; j<m; j=k) {
			if(arr[i][j] == '.') {
				k++;
				continue;
			}
			while(k < m && arr[i][k] == 'X')
				k++;
			ans = min(ans, k-j);
		}
	}
	for(int j=0; j<m; j++) {
		int k = 0;
		for(int i=0; i<n; i=k) {
			if(arr[i][j] == '.') {
				k++;
				continue;
			}
			while(k < n && arr[k][j] == 'X')
				k++;
			ans = min(ans, k-i);
		}
	}
	ans = (ans-1)/2;
	cout << ans << '\n';
	for(int i=0; i<n; i++) {
		for(int j=0; j<m; j++) {
			if(dist[i][j] <= ans) {
				arr[i][j] = '.';
			}
		}
		cout << arr[i] << '\n';
	}
	// for(int i=0; i<n; i++) {
	// 	for(int j=0; j<m; j++) {
	// 		cout << dist[i][j] << ' ';
	// 	}
	// 	cout << '\n';
	// }
}


#include <bits/stdc++.h>
using namespace std;
int di[] = {1,1,1,0,0,-1,-1,-1}, dj[] = {0,1,-1,1,-1,0,1,-1}, n, m;
bool valid(int i, int j) {
	return 0 <= i && i < n && 0 <= j && j < m;
}
int main() {
	ios::sync_with_stdio(0);cin.tie(0);
	cin >> n >> m;
	vector<string> arr(n);
	vector<vector<int>> dist(n, vector<int>(m, 0x3f3f3f3f));
	vector<vector<char>> seen(n, vector<char>(m, 0));
	queue<pair<int, int>> que;
	auto push = [&](int i, int j, int d) {
		dist[i][j] = min(dist[i][j], d);
		if(!seen[i][j]) {
			//printf("%d,%d pushed with dist %d\n", i, j, d);
			seen[i][j] = 1;
			que.push({i,j});
		}
	};
	for(int i=0; i<n; i++) {
		cin >> arr[i];
	}
	for(int i=0; i<n; i++) {
		for(int j=0; j<m; j++) {
			if(arr[i][j] == '.') {
				seen[i][j] = 1;
				dist[i][j] = 0;
				for(int k=0; k<8; k++) {
					int ti = i+di[k], tj = j+dj[k];
					if(valid(ti,tj) &&  arr[ti][tj] == 'X') {
						push(ti, tj, 1);
					}
				}
			}
			if((!i || !j || i == n-1 || j == m-1) && arr[i][j] == 'X') {
				push(i, j, 1);
			}
		}

	}
	//printf("after queue\n");
	while(!que.empty()) {
		int i, j;
		tie(i, j) = que.front();
		que.pop();
		for(int k=0; k<8; k++) {
			int ti = i+di[k], tj = j+dj[k];
			if(valid(ti,tj) &&  arr[ti][tj] == 'X') {
				push(ti, tj, dist[i][j]+1);
			}
		}
	}
	seen = vector<vector<char>>(n, vector<char>(m, 0));
	function<int(int,int)> dfs = [&](int i, int j) {
		int ret = dist[i][j];
		for(int k=0; k<8; k++) {
			int ti = i+di[k], tj = j+dj[k];
			if(valid(ti, tj) && arr[ti][tj] == 'X' && !seen[ti][tj]) {
				seen[ti][tj] = 1;
				ret = max(ret, dfs(ti, tj));
			}
		}
		return ret;
	};
	int ans = INT_MAX;
	for(int i=0; i<n; i++) {
		for(int j=0; j<m; j++) {
			if(arr[i][j] == 'X' && !seen[i][j]) {
				ans = min(ans, dfs(i, j));
			}
		}
	}
	ans--;
	cout << ans << '\n';
	for(int i=0; i<n; i++) {
		for(int j=0; j<m; j++) {
			if(dist[i][j] <= ans) {
				arr[i][j] = '.';
			}
		}
		cout << arr[i] << '\n';
	}
	// for(int i=0; i<n; i++) {
	// 	for(int j=0; j<m; j++) {
	// 		cout << dist[i][j] << ' ';
	// 	}
	// 	cout << '\n';
	// }
}


#include <bits/stdc++.h>
using namespace std;
#define ii pair<int, int>
#define iii pair<int, ii>
#define fi first
#define se second

int n, m;
vector<vector<char>> C;
int dx[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dy[] = {0, 1, 1, 1, 0, -1, -1, -1};

inline bool check(int x, int y)
{
	return x >= 0 and y >= 0 and x < n and y < m;
}

pair<bool, vector<vector<char>>> bfs(int t)
{
	vector<vector<int>> dist(n, vector<int>(m, -1));
	queue<iii> q;

	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++)
			if(C[i][j] == '.')
			{
				q.push({0, {i, j}});
				dist[i][j] = 0;
			}

	while(!q.empty())
	{
		int d = q.front().fi;
		int l = q.front().se.fi, r = q.front().se.se;

		q.pop();

		for(int i = 0; i < 8; i++)
		{
			int x = l + dx[i], y = r + dy[i];
			if(!check(x, y)or dist[x][y] != -1) continue;
			dist[x][y] = d + 1;
			q.push({d + 1, {x, y}});
		}
	}

	vector<vector<char>> cor(n, vector<char>(m, '.'));

	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++)
			if(C[i][j] == 'X' and dist[i][j] > t)
			{
				q.push({0, {i, j}});
				cor[i][j] = 'X';
			}

	while(!q.empty())
	{
		int d = q.front().fi;
		int l = q.front().se.fi, r = q.front().se.se;

		q.pop();

		if(d >= t) continue;

		for(int i = 0; i < 8; i++)
		{
			int x = l + dx[i], y = r + dy[i];
			if(!check(x, y) or cor[x][y] == 'X') continue;
			cor[x][y] = 'X';
			q.push({d + 1, {x, y}});
		}
	}

	bool fl = true;
	vector<vector<char>> ANS(n, vector<char>(m, '.'));

	for(int i = 0; i < n and fl; i++)
		for(int j = 0; j < m and fl; j++)
			if(C[i][j] != cor[i][j])
				fl = false;

	if(!fl) return {fl, ANS};

	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++)
			if(C[i][j] == 'X' and dist[i][j] > t)
				ANS[i][j] = 'X';

	return {fl, ANS};
}

int main()
{
	cin >> n >> m;
	n+=2; m+=2;
	C.resize(n);
	fill(C.begin(), C.end(), vector<char>(m, '.'));
	for(int i = 1; i < n - 1; i++)
		for(int j = 1; j < m - 1; j++)
			scanf(" %c", &C[i][j]);

	int b = 0, e = min(n, m), T = -1;
	vector<vector<char>> ANS;

	while(b <= e)
	{
		int t = (b + e) / 2;
		auto it = bfs(t);
		if(it.fi)
		{
			ANS = it.se;
			T = t;
			b = t + 1;
		}
		else e = t - 1;
	}
	assert(T != -1);
	cout << T << '\n';
	for(int i = 1; i < n - 1; i++)
	{
		for(int j = 1; j < m - 1; j++)
			printf("%c", ANS[i][j]);
		puts("");
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ii pair<int, int>
#define iii pair<int, ii>
#define fi first
#define se second

int n, m;
vector<vector<char>> C;
int dx[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dy[] = {0, 1, 1, 1, 0, -1, -1, -1};

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

pair<bool, vector<vector<char>>> bfs(int t)
{
	vector<vector<int>> dist(n, vector<int>(m, -1));
	queue<iii> q;

	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++)
			if(C[i][j] == '.')
			{
				q.push({0, {i, j}});
				dist[i][j] = 0;
			}

	while(!q.empty())
	{
		int d = q.front().fi;
		int l = q.front().se.fi, r = q.front().se.se;

		q.pop();

		for(int i = 0; i < 8; i++)
		{
			int x = l + dx[i], y = r + dy[i];
			if(!check(x, y)or dist[x][y] != -1) continue;
			dist[x][y] = d + 1;
			q.push({d + 1, {x, y}});
		}
	}

	vector<vector<char>> cor(n, vector<char>(m, '.'));

	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++)
			if(C[i][j] == 'X' and dist[i][j] > t)
			{
				q.push({0, {i, j}});
				cor[i][j] = 'X';
			}

	while(!q.empty())
	{
		int d = q.front().fi;
		int l = q.front().se.fi, r = q.front().se.se;

		q.pop();

		if(d >= t) continue;

		for(int i = 0; i < 8; i++)
		{
			int x = l + dx[i], y = r + dy[i];
			if(!check(x, y) or cor[x][y] == 'X') continue;
			cor[x][y] = 'X';
			q.push({d + 1, {x, y}});
		}
	}

	bool fl = true;
	vector<vector<char>> ANS(n, vector<char>(m, '.'));

	for(int i = 0; i < n and fl; i++)
		for(int j = 0; j < m and fl; j++)
			if(C[i][j] != cor[i][j])
				fl = false;

	if(!fl) return {fl, ANS};

	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++)
			if(C[i][j] == 'X' and dist[i][j] > t)
				ANS[i][j] = 'X';

	return {fl, ANS};
}

int main()
{
	cin >> n >> m;
	n+=2; m+=2;
	C.resize(n);
	fill(C.begin(), C.end(), vector<char>(m, '.'));
	for(int i = 1; i < n - 1; i++)
		for(int j = 1; j < m - 1; j++)
			cin >> C[i][j];

	int b = 0, e = n * m, T = -1;
	vector<vector<char>> ANS;

	while(b <= e)
	{
		int t = (b + e) / 2;
		auto it = bfs(t);
		if(it.fi)
		{
			ANS = it.se;
			T = t;
			b = t + 1;
		}
		else e = t - 1;
	}
	assert(T != -1);
	cout << T << '\n';
	for(int i = 1; i < n - 1; i++)
	{
		for(int j = 1; j < m - 1; j++)
			cout << ANS[i][j];
		puts("");
	}

	return 0;
}


#include<bits/stdc++.h>
#define long long long
using namespace std;
const int N=1e6;
int n,m;
string s[N+2];
vector<vector<int> >d,tmp;
vector<pair<int,int> >pos[N+2];
int dx[]={0,0,1,1,1,-1,-1,-1};
int dy[]={1,-1,0,1,-1,0,1,-1};
bool ok(int c)
{
  for(int i=1;i<=n;i++)
  {
    for(int j=1;j<=m;j++)
      tmp[i][j]=1e9;
  }
  queue<pair<int,int> >q;
  for(int i=1;i<=n;i++)
  {
    for(int j=1;j<=m;j++)
      if(d[i][j]>=c)q.push({i,j}),tmp[i][j]=0;
  }
  while(q.size())
  {
    pair<int,int>p=q.front();q.pop();
    int x,y;
    for(int i=0;i<8;i++)
    {
      x=p.first+dx[i],y=p.second+dy[i];
      if(x>=1&&x<=n&&y>=1&&y<=m)
      {
        if(tmp[p.first][p.second]+1<tmp[x][y])
          tmp[x][y]=tmp[p.first][p.second]+1,q.push({x,y});
      }
    }
  }
  for(int i=1;i<=n;i++)
  {
    for(int j=1;j<=m;j++)
      if(d[i][j]<c&&d[i][j]>0&&tmp[i][j]>=c)return 0;
  }
  return 1;
}
int main()
{
  ios_base::sync_with_stdio(0);cin.tie(0);
  cin>>n>>m;

  d.resize(n+2),tmp.resize(n+2);
  for(int i=0;i<n+2;i++)d[i].resize(m+2),tmp[i].resize(m+2);

  for(int i=0;i<n;i++)cin>>s[i];

  for(int i=0;i<=n+1;i++)for(int j=0;j<=m+1;j++)d[i][j]=1e9;

  queue<pair<int,int> >q;
  for(int i=0;i<=n+1;i++)
  {
    q.push({i,0});q.push({i,m+1});
    d[i][0]=0;d[i][m+1]=0;
  }
  for(int j=0;j<=m+1;j++)
  {
    q.push({0,j});q.push({n+1,j});
    d[0][j]=0;d[n+1][j]=0;
  }
  for(int i=1;i<=n;i++)
  {
    for(int j=1;j<=m;j++)
    {
      if(s[i-1][j-1]=='.')
        q.push({i,j}),d[i][j]=0;
    }
  }
  while(q.size())
  {
    pair<int,int>p=q.front();q.pop();
    int x,y;
    for(int i=0;i<8;i++)
    {
      x=p.first+dx[i],y=p.second+dy[i];
      if(x>=1&&x<=n&&y>=1&&y<=m)
      {
        if(d[p.first][p.second]+1<d[x][y])
          d[x][y]=d[p.first][p.second]+1,q.push({x,y});
      }
    }
  }
  for(int i=1;i<=n;i++)
  {
    for(int j=1;j<=m;j++)
      pos[d[i][j]].push_back({i,j});
  }
  // for(int i=1;i<=n;i++)
  // {
  //   for(int j=1;j<=m;j++)
  //     cout<<d[i][j];
  //   cout<<endl;
  // }
  int x;
  for(x=1; ;x++)
    if(!ok(x))break;
  x--;
  cout<<x-1<<endl;
  for(int i=1;i<=n;i++)
  {
    for(int j=1;j<=m;j++)
    {
      if(d[i][j]>=x)cout<<'X';
      else cout<<'.';
    }
    cout<<endl;
  }
  return 0;
}


#include<bits/stdc++.h>
#define long long long
using namespace std;
const int N=1e6;
int n,m;
string s[N+2];
vector<vector<int> >d,tmp;
vector<pair<int,int> >pos[N+2];
int dx[]={0,0,1,1,1,-1,-1,-1};
int dy[]={1,-1,0,1,-1,0,1,-1};
bool ok(int c)
{
  for(int i=1;i<=n;i++)
  {
    for(int j=1;j<=m;j++)
      tmp[i][j]=1e9;
  }
  queue<pair<int,int> >q;
  for(int i=1;i<=n;i++)
  {
    for(int j=1;j<=m;j++)
      if(d[i][j]>=c)q.push({i,j}),tmp[i][j]=0;
  }
  while(q.size())
  {
    pair<int,int>p=q.front();q.pop();
    int x,y;
    for(int i=0;i<8;i++)
    {
      x=p.first+dx[i],y=p.second+dy[i];
      if(x>=1&&x<=n&&y>=1&&y<=m)
      {
        if(tmp[p.first][p.second]+1<tmp[x][y])
          tmp[x][y]=tmp[p.first][p.second]+1,q.push({x,y});
      }
    }
  }
  for(int i=1;i<=n;i++)
  {
    for(int j=1;j<=m;j++)
      if(d[i][j]<c&&d[i][j]>0&&tmp[i][j]>=c)return 0;
  }
  return 1;
}
int main()
{
  ios_base::sync_with_stdio(0);cin.tie(0);
  cin>>n>>m;

  d.resize(n+2),tmp.resize(n+2);
  for(int i=0;i<n+2;i++)d[i].resize(m+2),tmp[i].resize(m+2);

  for(int i=0;i<n;i++)cin>>s[i];

  for(int i=0;i<=n+1;i++)for(int j=0;j<=m+1;j++)d[i][j]=1e9;

  queue<pair<int,int> >q;
  for(int i=0;i<=n+1;i++)
  {
    q.push({i,0});q.push({i,m+1});
    d[i][0]=0;d[i][m+1]=0;
  }
  for(int j=0;j<=m+1;j++)
  {
    q.push({0,j});q.push({n+1,j});
    d[0][j]=0;d[n+1][j]=0;
  }
  for(int i=1;i<=n;i++)
  {
    for(int j=1;j<=m;j++)
    {
      if(s[i-1][j-1]=='.')
        q.push({i,j}),d[i][j]=0;
    }
  }
  while(q.size())
  {
    pair<int,int>p=q.front();q.pop();
    int x,y;
    for(int i=0;i<8;i++)
    {
      x=p.first+dx[i],y=p.second+dy[i];
      if(x>=1&&x<=n&&y>=1&&y<=m)
      {
        if(d[p.first][p.second]+1<d[x][y])
          d[x][y]=d[p.first][p.second]+1,q.push({x,y});
      }
    }
  }
  int lo=1,hi=0,md;
  for(int i=1;i<=n;i++)
  {
    for(int j=1;j<=m;j++)
      hi=max(hi,d[i][j]);
  }
  hi++;
  while(hi-lo>2)
  {
    md=(lo+hi)/2;
    if(ok(md))lo=md;else hi=md;
  }
  for(md=lo;md<=hi;md++)
    if(!ok(md))break;
  md--;
  cout<<md-1<<"\n";
  for(int i=1;i<=n;i++)
  {
    for(int j=1;j<=m;j++)
    {
      if(d[i][j]>=md)cout<<'X';
      else cout<<'.';
    }
    cout<<"\n";
  }
  return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <complex>
#include <string>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#ifdef LOCAL
#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;
#else
#define debug(x) ;
#endif

#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 2000010

int H, W;
char S[SIZE];
char* mm[SIZE];
int sum_[SIZE];
int* sum[SIZE];
bool burn[SIZE];
bool ans[SIZE];

vector<int> vecH, vecW;

bool check(int x) {
  memset(burn, 0, sizeof(burn));
  memset(ans, 0, sizeof(ans));

  for (int diffY = 1; diffY >= -1; diffY -= 2) {
    for (int diffX = 1; diffX >= -1; diffX -= 2) {
      for (int i : vecH) {
        for (int j : vecW) {
          if (mm[i][j] == '.') continue;
          if (burn[i*W+j]) continue;

          int w, h;
          w = h = x * 2 + 1;

          int l = j, r = j + w * diffX - diffX;
          int u = i, d = i + h * diffY - diffY;

          if (l > r) swap(l, r);
          if (u > d) swap(d, u);

          if (d >= H || u < 0) continue;
          if (r >= W || l < 0) continue;

          d++; r++;

          int range_sum = sum[u][l] + sum[d][r] - sum[u][r] - sum[d][l];

          if (range_sum == (r - l) * (d - u)) {
            ans[(u+x)*W+l+x] = true;
            for (int y=u; y<d; y++) {
              for (int x=l; x<r; x++) {
                burn[y*W+x] = true;
              }
            }
          }
        }
      }
      reverse(vecW.begin(), vecW.end());
    }
    reverse(vecH.begin(), vecH.end());
  }

  for (int i=0; i<H; i++)
    for (int j=0; j<W; j++)
      if (mm[i][j] == 'X' && !burn[i*W+j])
        return false;

  return true;
}

int main(){

  scanf("%d%d", &H, &W);

  for (int i=0; i<H; i++) {
    mm[i] = S + i * W;
    sum[i] = sum_ + i * (W+1);
    scanf("%s", S + i * W);
  }
  sum[H] = sum_ + H * (W+1);

  for (int i=0; i<H; i++)
    for (int j=0; j<W; j++)
      sum[i+1][j+1] = mm[i][j] == 'X';

  for (int i=0; i<=H; i++)
    for (int j=0; j<W; j++)
      sum[i][j+1] += sum[i][j];

  for (int i=0; i<H; i++)
    for (int j=0; j<=W; j++)
      sum[i+1][j] += sum[i][j];

  for (int i=0; i<H; i++) vecH.push_back(i);
  for (int j=0; j<W; j++) vecW.push_back(j);

  int l = 0, r = min(H, W) / 2;

  while (l < r) {
    int mid = (l + r + 1) / 2;

    if (check(mid)) {
      l = mid;
    } else {
      r = mid - 1;
    }
  }

  check(l);

  cout << l << endl;

  for (int i=0; i<H; i++) {
    for (int j=0; j<W; j++) {
      printf(ans[i*W+j] ? "X" : ".");
    }
    puts("");
  }

  return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <complex>
#include <string>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#ifdef LOCAL
#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;
#else
#define debug(x) ;
#endif

#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 2000010

int H, W;
char S[SIZE];
char* mm[SIZE];
int sum_[SIZE];
int* sum[SIZE];
int sum2_[SIZE];
int* sum2[SIZE];
bool burn[SIZE];
bool ans[SIZE];

vector<int> vecH, vecW;

bool check(int x) {
  memset(burn, 0, sizeof(burn));
  memset(ans, 0, sizeof(ans));
  memset(sum2_, 0, sizeof(sum2_));

  for (int i : vecH) {
    for (int j : vecW) {
      if (mm[i][j] == '.') continue;

      int w, h;
      w = h = x * 2 + 1;

      int l = j, r = j + w;
      int u = i, d = i + h;

      if (d > H || u < 0) continue;
      if (r > W || l < 0) continue;

      int range_sum = sum[u][l] + sum[d][r] - sum[u][r] - sum[d][l];

      if (range_sum == (r - l) * (d - u)) {
        ans[(u+x)*W+(l+x)] = true;
        sum2[u][l]++;
        sum2[u][r]--;
        sum2[d][l]--;
        sum2[d][r]++;
      }
    }
  }

  for (int i=0; i<H; i++)
    for (int j=0; j<W-1; j++) {
      sum2[i][j+1] += sum2[i][j];
    }

  for (int i=0; i<H-1; i++)
    for (int j=0; j<W; j++)
      sum2[i+1][j] += sum2[i][j];

  for (int i=0; i<H; i++)
    for (int j=0; j<W; j++)
      if (mm[i][j] == 'X' && !sum2[i][j])
        return false;

  return true;
}

int main(){

#ifndef TEST
  scanf("%d%d", &H, &W);

  for (int i=0; i<H; i++)
    scanf("%s", S + i * W);
#else
  srand(time(NULL));
  H = 1;
  W = 1;

  for (int i=0; i<H; i++)
    for (int j=0; j<W; j++)
      S[i*W+j] = rand() % 2 ? 'X' : '.';
#endif

  for (int i=0; i<H; i++) {
    mm[i] = S + i * W;
    sum[i] = sum_ + i * (W+1);
    sum2[i] = sum2_ + i * (W+1);
  }
  sum[H] = sum_ + H * (W+1);
  sum2[H] = sum2_ + H * (W+1);

  for (int i=0; i<H; i++)
    for (int j=0; j<W; j++)
      sum[i+1][j+1] = mm[i][j] == 'X';

  for (int i=0; i<=H; i++)
    for (int j=0; j<W; j++)
      sum[i][j+1] += sum[i][j];

  for (int i=0; i<H; i++)
    for (int j=0; j<=W; j++)
      sum[i+1][j] += sum[i][j];

  for (int i=0; i<H; i++) vecH.push_back(i);
  for (int j=0; j<W; j++) vecW.push_back(j);

  int l = 0, r = min(H, W) / 2;

  while (l < r) {
    int mid = (l + r + 1) / 2;

    if (check(mid)) {
      l = mid;
    } else {
      r = mid - 1;
    }
  }

  check(l);

  cout << l << endl;
  debug(l);

  for (int i=0; i<H; i++) {
    for (int j=0; j<W; j++) {
      printf(ans[i*W+j] ? "X" : ".");
    }
    puts("");
  }

  return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define ll long long
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()

using namespace std;
using namespace __gnu_pbds;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; ///find_by_order(),order_of_key()
template<class T1, class T2> ostream& operator<<(ostream& os, const pair<T1,T2>& a) { os << '{' << a.f << ", " << a.s << '}'; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T1,class T2> ostream& operator<<(ostream& os, const map<T1,T2>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T1,class T2> ostream& operator<<(ostream& os, const gp_hash_table<T1,T2>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}

vector<int> dx={0,0,1,-1,1,1,-1,-1},dy={1,-1,0,0,1,-1,1,-1};
int n,m;
const int N=1e6+1;
vector<string> mat(N),sol;
bool inside(int x,int y){
    return x>=0&&x<n&&y>=0&&y<m&&mat[x][y]=='X';
}
vector<vector<int> > dist(N);
vector<vector<bool> > visited(N);
int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	scanf("%i %i",&n,&m);
	string em1(m,'.');
	for(int i=0;i<n;i++)
        cin >> mat[i],dist[i].resize(m),visited[i].resize(m),sol.pb(em1);
    queue<pair<int,int> > q;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        {
            bool moze=true;
            for(int k=0;k<8;k++)
                if(!inside(i+dx[k],j+dy[k]))
                    moze=false;
            if(!moze)
                q.push({i,j}),dist[i][j]=1;
        }
    while(q.size())
    {
        int x=q.front().f,y=q.front().s;
        q.pop();
        for(int k=0;k<8;k++)
        {
            int xx=x+dx[k],yy=y+dy[k];
            if(inside(xx,yy)&&dist[xx][yy]==0)
                dist[xx][yy]=dist[x][y]+1,q.push({xx,yy});
        }
    }
    vector<pair<int,pair<int,int> > > em;
    vector<vector<pair<int,pair<int,int> > > > ko;
    int mn=INT_MAX;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        {
            if(visited[i][j]||!inside(i,j))
                continue;
            q.push({i,j});
            ko.pb(em);
            while(q.size())
            {
                int x=q.front().f,y=q.front().s;
                ko.back().pb({dist[x][y],{x,y}});
                q.pop();
                for(int k=0;k<8;k++)
                {
                    int xx=x+dx[k],yy=y+dy[k];
                    if(inside(xx,yy)&&visited[xx][yy]==0)
                        visited[xx][yy]=1,q.push({xx,yy});
                }
            }
            sort(all(ko.back()));
            mn=min(mn,ko.back().back().f);
        }
    printf("%i\n",mn-1);
    for(auto p:ko)
        for(auto d:p)
            if(d.f>=mn)
                sol[d.s.f][d.s.s]='X';
    for(int i=0;i<n;i++)
        cout << sol[i] << endl;
    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define ll long long
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()

using namespace std;
using namespace __gnu_pbds;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; ///find_by_order(),order_of_key()
template<class T1, class T2> ostream& operator<<(ostream& os, const pair<T1,T2>& a) { os << '{' << a.f << ", " << a.s << '}'; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T1,class T2> ostream& operator<<(ostream& os, const map<T1,T2>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T1,class T2> ostream& operator<<(ostream& os, const gp_hash_table<T1,T2>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}

vector<int> dx={0,0,1,-1,1,1,-1,-1},dy={1,-1,0,0,1,-1,1,-1};
int n,m;
const int N=1e6+1;
vector<string> mat(N),sol;
bool inside(int x,int y){
    return x>=0&&x<n&&y>=0&&y<m&&mat[x][y]=='X';
}
vector<vector<int> > dist(N);
int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	scanf("%i %i",&n,&m);
	string em1(m,'.');
	for(int i=0;i<n;i++)
        cin >> mat[i],dist[i].resize(m),sol.pb(em1);
    queue<pair<int,int> > q;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        {
            if(!inside(i,j))
                continue;
            bool moze=true;
            for(int k=0;k<8;k++)
                if(!inside(i+dx[k],j+dy[k]))
                    moze=false;
            if(!moze)
                q.push({i,j}),dist[i][j]=1;
        }
    while(q.size())
    {
        int x=q.front().f,y=q.front().s;
        q.pop();
        for(int k=0;k<8;k++)
        {
            int xx=x+dx[k],yy=y+dy[k];
            if(inside(xx,yy)&&dist[xx][yy]==0)
                dist[xx][yy]=dist[x][y]+1,q.push({xx,yy});
        }
    }
    int l=0,r=max(n,m);
    while(l<r)
    {
        int mi=(l+r+1)>>1;
        vector<vector<int> > d(n,vector<int>(m));
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
                if(dist[i][j]>mi)
                    q.push({i,j}),d[i][j]=1;
        while(q.size())
        {
            int x=q.front().f,y=q.front().s;
            q.pop();
            if(d[x][y]>mi)
                continue;
            for(int k=0;k<8;k++)
            {
                int xx=x+dx[k],yy=y+dy[k];
                if(inside(xx,yy)&&d[xx][yy]==0)
                    d[xx][yy]=d[x][y]+1,q.push({xx,yy});
            }
        }
        bool isto=true;
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
                if(inside(i,j)&&!d[i][j])
                    isto=false;
        if(!isto)
            r=mi-1;
        else
            l=mi;
    }
    printf("%i\n",l);
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++)
            if(dist[i][j]>l)
                printf("X");
            else
                printf(".");
        printf("\n");
    }
    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define ll long long
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()

using namespace std;
using namespace __gnu_pbds;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; ///find_by_order(),order_of_key()
template<class T1, class T2> ostream& operator<<(ostream& os, const pair<T1,T2>& a) { os << '{' << a.f << ", " << a.s << '}'; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T1,class T2> ostream& operator<<(ostream& os, const map<T1,T2>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T1,class T2> ostream& operator<<(ostream& os, const gp_hash_table<T1,T2>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}

vector<int> dx={0,0,1,-1,1,1,-1,-1},dy={1,-1,0,0,1,-1,1,-1};
int n,m;
const int N=1e6+1;
vector<string> mat(N),sol;
bool inside(int x,int y){
    return x>=0&&x<n&&y>=0&&y<m&&mat[x][y]=='X';
}
vector<vector<int> > dist(N);
int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin >> n >> m;
	string em1(m,'.');
	for(int i=0;i<n;i++)
        cin >> mat[i],dist[i].resize(m),sol.pb(em1);
    queue<pair<int,int> > q;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        {
            if(!inside(i,j))
                continue;
            bool moze=true;
            for(int k=0;k<8;k++)
                if(!inside(i+dx[k],j+dy[k]))
                    moze=false;
            if(!moze)
                q.push({i,j}),dist[i][j]=1;
        }
    while(q.size())
    {
        int x=q.front().f,y=q.front().s;
        q.pop();
        for(int k=0;k<8;k++)
        {
            int xx=x+dx[k],yy=y+dy[k];
            if(inside(xx,yy)&&dist[xx][yy]==0)
                dist[xx][yy]=dist[x][y]+1,q.push({xx,yy});
        }
    }
    int l=0,r=min(n,m);
    while(l<r)
    {
        int mi=(l+r+1)>>1;
        vector<vector<int> > d(n,vector<int>(m));
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
                if(dist[i][j]>mi)
                    q.push({i,j}),d[i][j]=1;
        while(q.size())
        {
            int x=q.front().f,y=q.front().s;
            q.pop();
            if(d[x][y]>mi)
                continue;
            for(int k=0;k<8;k++)
            {
                int xx=x+dx[k],yy=y+dy[k];
                if(inside(xx,yy)&&d[xx][yy]==0)
                    d[xx][yy]=d[x][y]+1,q.push({xx,yy});
            }
        }
        bool isto=true;
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
                if(inside(i,j)&&!d[i][j])
                    isto=false;
        if(!isto)
            r=mi-1;
        else
            l=mi;
    }
    cout << l << "\n";
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++)
            if(dist[i][j]>l)
                cout << "X";
            else
                cout << ".";
        cout << "\n";
    }
    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define ll long long
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()

using namespace std;
using namespace __gnu_pbds;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; ///find_by_order(),order_of_key()
template<class T1, class T2> ostream& operator<<(ostream& os, const pair<T1,T2>& a) { os << '{' << a.f << ", " << a.s << '}'; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T1,class T2> ostream& operator<<(ostream& os, const map<T1,T2>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T1,class T2> ostream& operator<<(ostream& os, const gp_hash_table<T1,T2>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}

vector<int> dx={0,0,1,-1,1,1,-1,-1},dy={1,-1,0,0,1,-1,1,-1};
int n,m;
const int N=1e6+1;
vector<string> mat(N),sol;
bool inside(int x,int y){
    return x>=0&&x<n&&y>=0&&y<m&&mat[x][y]=='X';
}
vector<vector<int> > dist(N);
int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin >> n >> m;
	string em1(m,'.');
	for(int i=0;i<n;i++)
        cin >> mat[i],dist[i].resize(m),sol.pb(em1);
    queue<pair<int,int> > q;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        {
            if(!inside(i,j))
                continue;
            bool moze=true;
            for(int k=0;k<8;k++)
                if(!inside(i+dx[k],j+dy[k]))
                    moze=false;
            if(!moze)
                q.push({i,j}),dist[i][j]=1;
        }
    while(q.size())
    {
        int x=q.front().f,y=q.front().s;
        q.pop();
        for(int k=0;k<8;k++)
        {
            int xx=x+dx[k],yy=y+dy[k];
            if(inside(xx,yy)&&dist[xx][yy]==0)
                dist[xx][yy]=dist[x][y]+1,q.push({xx,yy});
        }
    }
    int l=0,r=max(n,m);
    while(l<r)
    {
        int mi=(l+r+1)>>1;
        vector<vector<int> > d(n,vector<int>(m));
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
                if(dist[i][j]>mi)
                    q.push({i,j}),d[i][j]=1;
        while(q.size())
        {
            int x=q.front().f,y=q.front().s;
            q.pop();
            if(d[x][y]>mi)
                continue;
            for(int k=0;k<8;k++)
            {
                int xx=x+dx[k],yy=y+dy[k];
                if(inside(xx,yy)&&d[xx][yy]==0)
                    d[xx][yy]=d[x][y]+1,q.push({xx,yy});
            }
        }
        bool isto=true;
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
                if(inside(i,j)&&!d[i][j])
                    isto=false;
        if(!isto)
            r=mi-1;
        else
            l=mi;
    }
    cout << l << "\n";
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++)
            if(dist[i][j]>l)
                cout << "X";
            else
                cout << ".";
        cout << "\n";
    }
    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define pb push_back
#define mp make_pair
#define fi first
#define se second

using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> splay;

const int N = 1000010;
const int mod = 998244353;

inline int read()
{
  int p=0; int f=1; char ch=getchar();
  while(ch<'0' || ch>'9'){if(ch=='-') f=-1; ch=getchar();}
  while(ch>='0' && ch<='9'){p=p*10+ch-'0'; ch=getchar();}
  return p*f;
}

int n,m;

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  vector<string> str(n+1);
  for(int i=0;i<n;i++) cin >> str[i];
  vector<vector<int> > s(n+1 , vector<int> (m+1) );
  for(int i=0;i<n;i++) for(int j=0;j<m;j++)
  {
    s[i][j] = (str[i][j] == 'X');
    if(i && j) s[i][j] -= s[i-1][j-1];
    if(i) s[i][j] += s[i-1][j];
    if(j) s[i][j] += s[i][j-1];
  }

//  printf("%d %d\n",n,m);

  auto check = [&](int k)
  {
    vector<vector<int> >t(n+1 , vector<int> (m+1) );
    for(int i=0;i<n-2*k;i++) for(int j=0;j<m-2*k;j++) {
      int temp = s[i+2*k][j+2*k];
      if(i && j) temp += s[i-1][j-1];
      if(i) temp -= s[i-1][j+2*k];
      if(j) temp -= s[i+2*k][j-1];
//      printf("%d %d %d %d\n",k,i,j,temp);
      if(temp == (2*k+1)*(2*k+1)){
      	t[i][j] ++;
      	t[i+2*k+1][j] --; t[i][j+2*k+1] --;
      	t[i+2*k+1][j+2*k+1] ++;
      }
    }

    for(int i=0;i<n;i++) for(int j=0;j<m;j++)
	{
	  if(i) t[i][j] += t[i-1][j];
	  if(j) t[i][j] += t[i][j-1];
	  if(i&&j) t[i][j] -= t[i-1][j-1];
//	  printf("%d %d %d %d\n",k,i,j,t[i][j]);
      if(!t[i][j] && str[i][j] == 'X') return 0;
      if(t[i][j] && str[i][j] == '.') return 0;
    }return 1;
  };

  int l = 0; int r = min(n,m) / 2; int res;
  while(l<=r)
  {
    int mid = (l+r)>>1;
    if(check(mid)){res=mid; l = mid+1;}
    else r=mid-1;
  }
  cout << res << endl;
  vector<string> ss(n , string(m , '.'));
  for(int i=0;i<n;i++) for(int j=0;j<m;j++) ss[i][j] = '.';
  for(int i=0;i<n-2*res;i++) for(int j=0;j<m-2*res;j++)
  {
    int temp = s[i+2*res][j+2*res];
	if(i && j) temp += s[i-1][j-1];
	if(i) temp -= s[i-1][j+2*res];
	if(j) temp -= s[i+2*res][j-1];
	if(temp == (2*res+1) * (2*res+1)) ss[i+res][j+res] = 'X';
  }
  for(int i=0;i<n;i++) cout<<ss[i]<<endl;

  return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define pb push_back
#define mp make_pair
#define fi first
#define se second

using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> splay;

const int N = 1000010;
const int mod = 998244353;

inline int read()
{
  int p=0; int f=1; char ch=getchar();
  while(ch<'0' || ch>'9'){if(ch=='-') f=-1; ch=getchar();}
  while(ch>='0' && ch<='9'){p=p*10+ch-'0'; ch=getchar();}
  return p*f;
}

int n,m;

int main()
{
//  freopen("a.in","r",stdin);
//  freopen("a.out","w",stdout);
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  vector<string> str(n);
  for(int i=0;i<n;i++) cin >> str[i];
  vector<vector<int> > s(n , vector<int> (m) );
  for(int i=0;i<n;i++) for(int j=0;j<m;j++)
  {
    s[i][j] = (str[i][j] == 'X');
    if(i && j) s[i][j] -= s[i-1][j-1];
    if(i) s[i][j] += s[i-1][j];
    if(j) s[i][j] += s[i][j-1];
  }

//  printf("%d %d\n",n,m);

  auto check = [&](int k)
  {
  	k = k * 2;
    vector<vector<int> >t(n+1 , vector<int> (m+1) );
    for(int i=0;i<n-k;i++) for(int j=0;j<m-k;j++) {
      int temp = s[i+k][j+k];
      if(i && j) temp += s[i-1][j-1];
      if(i) temp -= s[i-1][j+k];
      if(j) temp -= s[i+k][j-1];
//      printf("%d %d %d %d\n",k,i,j,temp);
      if(temp == (k+1)*(k+1)){
      	t[i][j] ++;
      	t[i+k+1][j] --; t[i][j+k+1] --;
      	t[i+k+1][j+k+1] ++;
      }
    }

    for(int i=0;i<n;i++) for(int j=0;j<m;j++)
	{
	  if(i) t[i][j] += t[i-1][j];
	  if(j) t[i][j] += t[i][j-1];
	  if(i&&j) t[i][j] -= t[i-1][j-1];
//	  printf("%d %d %d %d\n",k,i,j,t[i][j]);
      if(!t[i][j] && str[i][j] == 'X') return 0;
      if(t[i][j] && str[i][j] == '.') return 0;
    }return 1;
  };

  int l = 0; int r = min(n,m) / 2; int res;
  while(l<=r)
  {
    int mid = (l+r)>>1;
    if(check(mid)){res=mid; l = mid+1;}
    else r=mid-1;
  }
  cout << res << endl; res = res * 2;
  vector<string> ss(n , string(m , '.'));
  for(int i=0;i<n;i++) for(int j=0;j<m;j++) ss[i][j] = '.';
  for(int i=0;i<n-res;i++) for(int j=0;j<m-res;j++)
  {
    int temp = s[i+res][j+res];
	if(i && j) temp += s[i-1][j-1];
	if(i) temp -= s[i-1][j+res];
	if(j) temp -= s[i+res][j-1];
	if(temp == (res+1) * (res+1)) ss[i+res/2][j+res/2] = 'X';
  }
  for(int i=0;i<n;i++) cout<<ss[i]<<endl;

  return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define pb push_back
#define mp make_pair
#define fi first
#define se second

using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> splay;

const int N = 1000010;
const int mod = 998244353;

inline int read()
{
  int p=0; int f=1; char ch=getchar();
  while(ch<'0' || ch>'9'){if(ch=='-') f=-1; ch=getchar();}
  while(ch>='0' && ch<='9'){p=p*10+ch-'0'; ch=getchar();}
  return p*f;
}

int n,m;

int main()
{
  n = read(); m = read();
  vector<string> str(n+1);
  for(int i=0;i<n;i++) cin >> str[i];
  vector<vector<int> > s(n+1 , vector<int> (m+1) );
  for(int i=0;i<n;i++) for(int j=0;j<m;j++)
  {
    s[i][j] = (str[i][j] == 'X');
    if(i && j) s[i][j] -= s[i-1][j-1];
    if(i) s[i][j] += s[i-1][j];
    if(j) s[i][j] += s[i][j-1];
  }

//  printf("%d %d\n",n,m);

  auto check = [&](int k)
  {
    vector<vector<int> >t(n+1 , vector<int> (m+1) );
    for(int i=0;i<n-2*k;i++) for(int j=0;j<m-2*k;j++) {
      int temp = s[i+2*k][j+2*k];
      if(i && j) temp += s[i-1][j-1];
      if(i) temp -= s[i-1][j+2*k];
      if(j) temp -= s[i+2*k][j-1];
//      printf("%d %d %d %d\n",k,i,j,temp);
      if(temp == (2*k+1)*(2*k+1)){
      	t[i][j] ++;
      	t[i+2*k+1][j] --; t[i][j+2*k+1] --;
      	t[i+2*k+1][j+2*k+1] ++;
      }
    }

    for(int i=0;i<n;i++) for(int j=0;j<m;j++)
	{
	  if(i) t[i][j] += t[i-1][j];
	  if(j) t[i][j] += t[i][j-1];
	  if(i&&j) t[i][j] -= t[i-1][j-1];
//	  printf("%d %d %d %d\n",k,i,j,t[i][j]);
      if(!t[i][j] && str[i][j] == 'X') return 0;
      else if(t[i][j] && str[i][j] == '.') return 0;
    }return 1;
  };

  int l = 0; int r = min(n,m) / 2; int res;
  while(l<=r)
  {
    int mid = (l+r)>>1;
    if(check(mid)){res=mid; l = mid+1;}
    else r=mid-1;
  }
  printf("%d\n",res);
  vector<string> ss(n , string(m , '.'));
  for(int i=0;i<n;i++) for(int j=0;j<m;j++) ss[i][j] = '.';
  for(int i=0;i<n-2*res;i++) for(int j=0;j<m-2*res;j++)
  {
    int temp = s[i+2*res][j+2*res];
	if(i && j) temp += s[i-1][j-1];
	if(i) temp -= s[i-1][j+2*res];
	if(j) temp -= s[i+2*res][j-1];
	if(temp == (2*res+1) * (2*res+1)) ss[i+res][j+res] = 'X';
  }
  for(int i=0;i<n;i++) cout<<ss[i]<<endl;

  return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define pb push_back
#define mp make_pair
#define fi first
#define se second

using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> splay;

const int N = 1000010;
const int mod = 998244353;

inline int read()
{
  int p=0; int f=1; char ch=getchar();
  while(ch<'0' || ch>'9'){if(ch=='-') f=-1; ch=getchar();}
  while(ch>='0' && ch<='9'){p=p*10+ch-'0'; ch=getchar();}
  return p*f;
}

int n,m;
char str[N],ans[N]; int s[N],t[N];

int id(int x,int y){if(x>=1 && x<=n && y>=1 && y<=m) return (x-1) * m + y; return 0;}

bool check(int k)
{
  k*=2;
  for(int i=1;i<=n-k;i++) for(int j=1;j<=m-k;j++) if(s[id(i+k,j+k)] + s[id(i-1,j-1)] - s[id(i+k,j-1)] - s[id(i-1,j+k)] == (k+1) * (k+1))
  {
    t[id(i,j)] ++; t[id(i+k+1,j+k+1)]++;
    t[id(i,j+k+1)] --; t[id(i+k+1,j)] --;
  }t[0] = 0;
  for(int i=1;i<=n;i++) for(int j=1;j<=m;j++)
  {
    t[id(i,j)] = t[id(i,j)] + t[id(i-1,j)] + t[id(i,j-1)] - t[id(i-1,j-1)];
    if(t[id(i,j)] && str[id(i,j)] == '.') return 0;
    if(!t[id(i,j)] && str[id(i,j)] == 'X') return 0;
  }return 1;
}

int main()
{
  n = read(); m = read();
  for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) scanf("\n%c",&str[id(i,j)]);
  for(int i=1;i<=n;i++) for(int j=1;j<=m;j++)
    s[id(i,j)] = (str[id(i,j)] == 'X') + s[id(i-1,j)] + s[id(i,j-1)] - s[id(i-1,j-1)];
  int l = 0; int r = min(n,m)/2; int res;
  while(l<=r)
  {
    int mid = (l+r)>>1;
    if(check(mid)) l = mid+1,res = mid;
    else r=mid-1;
  }
  printf("%d\n",res); res*=2;
  for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) ans[id(i,j)] = '.';
  for(int i=1;i<=n-res;i++) for(int j=1;j<=m-res;j++) if(s[id(i+res,j+res)] + s[id(i-1,j-1)] - s[id(i+res,j-1)] - s[id(i-1,j+res)] == (res+1) * (res+1)) ans[id(i+res/2,j+res/2)] = 'X';
  for(int i=1;i<=n;i++){for(int j=1;j<=m;j++) printf("%c",ans[id(i,j)]); puts("");}
  return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define pb push_back
#define mp make_pair
#define fi first
#define se second

using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> splay;

const int N = 1000010;
const int mod = 998244353;

inline int read()
{
  int p=0; int f=1; char ch=getchar();
  while(ch<'0' || ch>'9'){if(ch=='-') f=-1; ch=getchar();}
  while(ch>='0' && ch<='9'){p=p*10+ch-'0'; ch=getchar();}
  return p*f;
}

int n,m;
char str[N],ans[N]; int s[N],t[N];

int id(int x,int y){if(x>=1 && x<=n && y>=1 && y<=m) return (x-1) * m + y; return 0;}

bool check(int k)
{
  k*=2;
  for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) t[id(i,j)] = 0;
  for(int i=1;i<=n-k;i++) for(int j=1;j<=m-k;j++) if(s[id(i+k,j+k)] + s[id(i-1,j-1)] - s[id(i+k,j-1)] - s[id(i-1,j+k)] == (k+1) * (k+1))
  {
//    printf("%d %d %d\n",k,i,j);
    t[id(i,j)] ++; t[id(i+k+1,j+k+1)]++;
    t[id(i,j+k+1)] --; t[id(i+k+1,j)] --;
  }t[0] = 0;
  for(int i=1;i<=n;i++) for(int j=1;j<=m;j++)
  {
    t[id(i,j)] = t[id(i,j)] + t[id(i-1,j)] + t[id(i,j-1)] - t[id(i-1,j-1)];
//    printf("%d %d %d %d %c\n",k,i,j,t[id(i,j)],str[id(i,j)]);
    if(t[id(i,j)] && str[id(i,j)] == '.') return 0;
    if(!t[id(i,j)] && str[id(i,j)] == 'X') return 0;
  }return 1;
}

int main()
{
//  freopen("a.in","r",stdin);
//  freopen("a.out","w",stdout);
  n = read(); m = read();
  for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) scanf("\n%c",&str[id(i,j)]);
  for(int i=1;i<=n;i++) for(int j=1;j<=m;j++)
    s[id(i,j)] = (str[id(i,j)] == 'X') + s[id(i-1,j)] + s[id(i,j-1)] - s[id(i-1,j-1)];
  int l = 0; int r = min(n,m)/2; int res;
  while(l<=r)
  {
    int mid = (l+r)>>1;
    if(check(mid)) l = mid+1,res = mid;
    else r=mid-1;
  }
  printf("%d\n",res); res*=2;
  for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) ans[id(i,j)] = '.';
  for(int i=1;i<=n-res;i++) for(int j=1;j<=m-res;j++) if(s[id(i+res,j+res)] + s[id(i-1,j-1)] - s[id(i+res,j-1)] - s[id(i-1,j+res)] == (res+1) * (res+1)) ans[id(i+res/2,j+res/2)] = 'X';
  for(int i=1;i<=n;i++){for(int j=1;j<=m;j++) printf("%c",ans[id(i,j)]); puts("");}
  return 0;
}