#include <set>
#include <map>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 1000010;

int n, m;
char str[N];
string board[N];

int main() {

	scanf("%d %d", &n, &m);

	for (int i = 0; i < n; i++) {
		scanf("%s", str);
		board[i] = str;
	}

	vector < vector <int> > s(n + 1, vector <int> (m + 1, 0));

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			s[i + 1][j + 1] = s[i + 1][j] + s[i][j + 1] - s[i][j] + (board[i][j] == 'X');
		}
	}

	auto check = [&](int mid) {
		vector < vector <int> > t(n + 1, vector <int> (m + 1, 0));
		long long r = 2 * mid + 1;
		for (int i = 0; i <= n - r; i++) {
			for (int j = 0; j <= m - r; j++) {
				if (s[i + r][j + r] - s[i + r][j] - s[i][j + r] + s[i][j] == r * r) {
					t[i][j]++;
					t[i + r][j]--;
					t[i][j + r]--;
					t[i + r][j + r]++;
				}
			}
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (i != 0) {
					t[i][j] += t[i - 1][j];
				}
				if (j != 0) {
					t[i][j] += t[i][j - 1];
				}
				if (i != 0 && j != 0) {
					t[i][j] += t[i - 1][j - 1];
				}
				if (board[i][j] == 'X' && t[i][j] == 0) {
					return false;
				}
			}
		}
		return true;
	};

	int low = 0;
	int high = max(n, m);

	while (low <= high) {
		int mid = (low + high) / 2;
		if (check(mid)) {
			low = mid + 1;
		} else {
			high = mid - 1;
		}
	}

	long long r = 2 * high + 1;

	vector <string> ans(n, string(m, '.'));

	for (int i = 0; i <= n - r; i++) {
		for (int j = 0; j <= m - r; j++) {
			if (s[i + r][j + r] - s[i + r][j] - s[i][j + r] + s[i][j] == r * r) {
				ans[i + high][j + high] = 'X';
			}
		}
	}

	printf("%d\n", high);

	for (int i = 0; i < n; i++) {
		printf("%s\n", ans[i].c_str());
	}

	return 0;

}


#include <set>
#include <map>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 1000010;

int n, m;
char str[N];
string board[N];

int main() {

	scanf("%d %d", &n, &m);

	for (int i = 0; i < n; i++) {
		scanf("%s", str);
		board[i] = str;
	}

	vector < vector <int> > s(n + 1, vector <int> (m + 1, 0));

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			s[i + 1][j + 1] = s[i + 1][j] + s[i][j + 1] - s[i][j] + (board[i][j] == 'X');
		}
	}

	auto check = [&](int mid) {
		vector < vector <int> > t(n + 1, vector <int> (m + 1, 0));
		long long r = 2 * mid + 1;
		for (int i = 0; i <= n - r; i++) {
			for (int j = 0; j <= m - r; j++) {
				if (s[i + r][j + r] - s[i + r][j] - s[i][j + r] + s[i][j] == r * r) {
					t[i][j]++;
					t[i + r][j]--;
					t[i][j + r]--;
					t[i + r][j + r]++;
				}
			}
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (i != 0) {
					t[i][j] += t[i - 1][j];
				}
				if (j != 0) {
					t[i][j] += t[i][j - 1];
				}
				if (i != 0 && j != 0) {
					t[i][j] -= t[i - 1][j - 1];
				}
				if (board[i][j] == 'X' && t[i][j] == 0) {
					return false;
				}
			}
		}
		return true;
	};

	int low = 0;
	int high = max(n, m);

	while (low <= high) {
		int mid = (low + high) / 2;
		if (check(mid)) {
			low = mid + 1;
		} else {
			high = mid - 1;
		}
	}

	long long r = 2 * high + 1;

	vector <string> ans(n, string(m, '.'));

	for (int i = 0; i <= n - r; i++) {
		for (int j = 0; j <= m - r; j++) {
			if (s[i + r][j + r] - s[i + r][j] - s[i][j + r] + s[i][j] == r * r) {
				ans[i + high][j + high] = 'X';
			}
		}
	}

	printf("%d\n", high);

	for (int i = 0; i < n; i++) {
		printf("%s\n", ans[i].c_str());
	}

	return 0;

}


#include<bits/stdc++.h>

using namespace std;

#define range(i, m, n) for(int i = m; i < n; i++)
#define husk(i, m, n) for(int i = m; i > n; i--)

int n, m;

int main() {
  scanf("%d%d", &n, &m);
  vector<vector<char>> a(n, vector<char>(m));
  vector<vector<int>> b(n, vector<int>(m));
  range(i, 0, n) {
    range(j, 0, m) {
      scanf(" %c", &a[i][j]);
      b[i][j] = a[i][j] == 'X';
      if(i) b[i][j] += b[i - 1][j];
      if(j) b[i][j] += b[i][j - 1];
      if(i && j) b[i][j] -= b[i - 1][j - 1];
    }
  }
  auto solve = [&](int s) {
    vector<vector<int>> c(n, vector<int>(m));
    range(i, 0, n) {
      range(j, 0, m) {
        int ft = 0;
        if(i >= s && i < n - s && j >= s && j < m - s) {
          ft = b[i + s][j + s];
          if(i > s) ft -= b[i - s - 1][j + s];
          if(j > s) ft -= b[i + s][j - s - 1];
          if(i > s && j > s) ft += b[i - s - 1][j - s - 1];
        }
        c[i][j] = ft == (2 * s + 1) * (2 * s + 1);
        if(i) c[i][j] += c[i - 1][j];
        if(j) c[i][j] += c[i][j - 1];
      }
    }
    range(i, 0, n) {
      range(j, 0, m) {
        if(a[i][j] == '.') continue;
        int nx = i - s, mx = i + s;
        int ny = j - s, my = j + s;
        nx = max(nx, 0); mx = min(mx, n - 1); ny = max(ny, 0); my = min(my, m - 1);
        int ft = c[mx][my];
        if(nx) ft -= c[nx - 1][my];
        if(ny) ft -= c[mx][ny - 1];
        if(nx && ny) ft += c[nx - 1][ny - 1];
        if(!ft) return false;
      }
    }
    return true;
  };
  int lo = 0, hi = min(n, m) + 1;
  while(hi - lo > 1) {
    int mid = (hi + lo) >> 1;
    if(solve(mid)) lo = mid;
    else hi = mid;
  }
  int s = lo;
  printf("%d\n", s);
  vector<vector<char>> c(n, vector<char>(m, '.'));
  range(i, s, n - s) {
    range(j, s, m - s) {
      int ft = b[i + s][j + s];
      if(i > s) ft -= b[i - s - 1][j + s];
      if(j > s) ft -= b[i + s][j - s - 1];
      if(i > s && j > s) ft += b[i - s - 1][j - s - 1];
      c[i][j] = (ft == (2 * s + 1) * (2 * s + 1) ? 'X' : '.');
    }
  }
  if(s == 0) c = a;
  range(i, 0, n) {
    range(j, 0, m) {
      putchar(c[i][j]);
    }
    printf("\n");
  }
  return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define range(i, m, n) for(int i = m; i < n; i++)
#define husk(i, m, n) for(int i = m; i > n; i--)

int n, m;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  vector<vector<char>> a(n, vector<char>(m));
  vector<vector<int>> b(n, vector<int>(m));
  range(i, 0, n) {
    range(j, 0, m) {
      cin >> a[i][j];
      b[i][j] = a[i][j] == 'X';
      if(i) b[i][j] += b[i - 1][j];
      if(j) b[i][j] += b[i][j - 1];
      if(i && j) b[i][j] -= b[i - 1][j - 1];
    }
  }
  auto solve = [&](int s) {
    vector<vector<int>> c(n, vector<int>(m));
    range(i, 0, n) {
      range(j, 0, m) {
        int ft = 0;
        if(i >= s && i < n - s && j >= s && j < m - s) {
          ft = b[i + s][j + s];
          if(i > s) ft -= b[i - s - 1][j + s];
          if(j > s) ft -= b[i + s][j - s - 1];
          if(i > s && j > s) ft += b[i - s - 1][j - s - 1];
        }
        c[i][j] = ft == (2 * s + 1) * (2 * s + 1);
        if(i) c[i][j] += c[i - 1][j];
        if(j) c[i][j] += c[i][j - 1];
      }
    }
    range(i, 0, n) {
      range(j, 0, m) {
        if(a[i][j] == '.') continue;
        int nx = i - s, mx = i + s;
        int ny = j - s, my = j + s;
        nx = max(nx, 0); mx = min(mx, n - 1); ny = max(ny, 0); my = min(my, m - 1);
        int ft = c[mx][my];
        if(nx) ft -= c[nx - 1][my];
        if(ny) ft -= c[mx][ny - 1];
        if(nx && ny) ft += c[nx - 1][ny - 1];
        if(!ft) return false;
      }
    }
    return true;
  };
  int lo = 0, hi = min(n, m) + 1;
  while(hi - lo > 1) {
    int mid = (hi + lo) >> 1;
    if(solve(mid)) lo = mid;
    else hi = mid;
  }
  int s = lo;
  cout << s << endl;
  vector<vector<char>> c(n, vector<char>(m, '.'));
  range(i, s, n - s) {
    range(j, s, m - s) {
      int ft = b[i + s][j + s];
      if(i > s) ft -= b[i - s - 1][j + s];
      if(j > s) ft -= b[i + s][j - s - 1];
      if(i > s && j > s) ft += b[i - s - 1][j - s - 1];
      c[i][j] = (ft == (2 * s + 1) * (2 * s + 1) ? 'X' : '.');
    }
  }
  if(s == 0) c = a;
  range(i, 0, n) {
    range(j, 0, m) {
      cout << c[i][j];
    }
    cout << endl;
  }
  return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define range(i, m, n) for(int i = m; i < n; i++)
#define husk(i, m, n) for(int i = m; i > n; i--)

int n, m;

int main() {
  scanf("%d%d", &n, &m);
  vector<vector<char>> a(n, vector<char>(m));
  vector<vector<int>> b(n, vector<int>(m));
  range(i, 0, n) {
    range(j, 0, m) {
      scanf(" %c", &a[i][j]);
      b[i][j] = a[i][j] == 'X';
      if(i) b[i][j] += b[i - 1][j];
      if(j) b[i][j] += b[i][j - 1];
      if(i && j) b[i][j] -= b[i - 1][j - 1];
    }
  }
  auto solve = [&](int s) {
    vector<vector<int>> c(n, vector<int>(m));
    range(i, 0, n) {
      range(j, 0, m) {
        int ft = 0;
        if(i >= s && i < n - s && j >= s && j < m - s) {
          ft = b[i + s][j + s];
          if(i > s) ft -= b[i - s - 1][j + s];
          if(j > s) ft -= b[i + s][j - s - 1];
          if(i > s && j > s) ft += b[i - s - 1][j - s - 1];
        }
        c[i][j] = ft == (2 * s + 1) * (2 * s + 1);
        if(i) c[i][j] += c[i - 1][j];
        if(j) c[i][j] += c[i][j - 1];
        if(i && j) c[i][j] -= c[i - 1][j - 1]
      }
    }
    range(i, 0, n) {
      range(j, 0, m) {
        if(a[i][j] == '.') continue;
        int nx = i - s, mx = i + s;
        int ny = j - s, my = j + s;
        nx = max(nx, 0); mx = min(mx, n - 1); ny = max(ny, 0); my = min(my, m - 1);
        int ft = c[mx][my];
        if(nx) ft -= c[nx - 1][my];
        if(ny) ft -= c[mx][ny - 1];
        if(nx && ny) ft += c[nx - 1][ny - 1];
        if(!ft) return false;
      }
    }
    return true;
  };
  int lo = 0, hi = min(n, m) + 1;
  while(hi - lo > 1) {
    int mid = (hi + lo) >> 1;
    if(solve(mid)) lo = mid;
    else hi = mid;
  }
  int s = lo;
  printf("%d\n", s);
  vector<vector<char>> c(n, vector<char>(m, '.'));
  range(i, s, n - s) {
    range(j, s, m - s) {
      int ft = b[i + s][j + s];
      if(i > s) ft -= b[i - s - 1][j + s];
      if(j > s) ft -= b[i + s][j - s - 1];
      if(i > s && j > s) ft += b[i - s - 1][j - s - 1];
      c[i][j] = (ft == (2 * s + 1) * (2 * s + 1) ? 'X' : '.');
    }
  }
  if(s == 0) c = a;
  range(i, 0, n) {
    range(j, 0, m) {
      putchar(c[i][j]);
    }
    printf("\n");
  }
  return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define range(i, m, n) for(int i = m; i < n; i++)
#define husk(i, m, n) for(int i = m; i > n; i--)

int n, m;

int main() {
  scanf("%d%d", &n, &m);
  vector<vector<char>> a(n, vector<char>(m));
  vector<vector<int>> b(n, vector<int>(m));
  range(i, 0, n) {
    range(j, 0, m) {
      scanf(" %c", &a[i][j]);
      b[i][j] = a[i][j] == 'X';
      if(i) b[i][j] += b[i - 1][j];
      if(j) b[i][j] += b[i][j - 1];
      if(i && j) b[i][j] -= b[i - 1][j - 1];
    }
  }
  auto solve = [&](int s) {
    vector<vector<int>> c(n, vector<int>(m));
    range(i, 0, n) {
      range(j, 0, m) {
        int ft = 0;
        if(i >= s && i < n - s && j >= s && j < m - s) {
          ft = b[i + s][j + s];
          if(i > s) ft -= b[i - s - 1][j + s];
          if(j > s) ft -= b[i + s][j - s - 1];
          if(i > s && j > s) ft += b[i - s - 1][j - s - 1];
        }
        c[i][j] = ft == (2 * s + 1) * (2 * s + 1);
        if(i) c[i][j] += c[i - 1][j];
        if(j) c[i][j] += c[i][j - 1];
        if(i && j) c[i][j] -= c[i - 1][j - 1];
      }
    }
    range(i, 0, n) {
      range(j, 0, m) {
        if(a[i][j] == '.') continue;
        int nx = i - s, mx = i + s;
        int ny = j - s, my = j + s;
        nx = max(nx, 0); mx = min(mx, n - 1); ny = max(ny, 0); my = min(my, m - 1);
        int ft = c[mx][my];
        if(nx) ft -= c[nx - 1][my];
        if(ny) ft -= c[mx][ny - 1];
        if(nx && ny) ft += c[nx - 1][ny - 1];
        if(!ft) return false;
      }
    }
    return true;
  };
  int lo = 0, hi = min(n, m) + 1;
  while(hi - lo > 1) {
    int mid = (hi + lo) >> 1;
    if(solve(mid)) lo = mid;
    else hi = mid;
  }
  int s = lo;
  printf("%d\n", s);
  vector<vector<char>> c(n, vector<char>(m, '.'));
  range(i, s, n - s) {
    range(j, s, m - s) {
      int ft = b[i + s][j + s];
      if(i > s) ft -= b[i - s - 1][j + s];
      if(j > s) ft -= b[i + s][j - s - 1];
      if(i > s && j > s) ft += b[i - s - 1][j - s - 1];
      c[i][j] = (ft == (2 * s + 1) * (2 * s + 1) ? 'X' : '.');
    }
  }
  if(s == 0) c = a;
  range(i, 0, n) {
    range(j, 0, m) {
      putchar(c[i][j]);
    }
    printf("\n");
  }
  return 0;
}


#include<bits/stdc++.h>
#define MN 3000005
int n,m,sum1[MN],sum2[MN];
char mp[MN];
int go(int x,int y){return (x-1)*m+y;}
int val(int *sum,int x1,int y1,int x2,int y2){return sum[go(x2,y2)]-sum[go(x1-1,y2)]-sum[go(x2,y1-1)]+sum[go(x1-1,y1-1)];}
bool check(int x)
{
	memset(sum2,0,sizeof sum2);
	int dd=(x*2+1)*(x*2+1);
	for(int i=x+1;i<=n-x;i++)
		for(int j=x+1;j<=m-x;j++){
			if(dd==val(sum1,i-x,j-x,i+x,j+x)) sum2[go(i,j)]=1;
		}
	for(int i=1;i<=n*m;i++) sum2[i]+=sum2[i-1]+sum2[std::max(i-m,0)]-sum2[std::max(i-m-1,0)];
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if(mp[go(i,j)]=='X')
				if(!val(sum2,std::max(i-x,1),std::max(j-x,1),std::min(n,i+x),std::min(m,j+x)))
					return 0;
	return 1;
}
int main()
{
	scanf("%d%d",&n,&m);
	n+=2;m+=2;
	for(int i=2;i<n;i++)
		scanf("%s",mp+(i-1)*m+2);
	for(int i=1;i<=n*m;i++) sum1[i]=(mp[i]=='X');
	for(int i=1;i<=n*m;i++) sum1[i]+=(i%m==1?0:sum1[i-1])+sum1[std::max(i-m,0)]-sum1[std::max(i-m-1,0)];
	int l=0,r=1e3,ans;
	while(l<=r)
	{
		int mid=l+r>>1;
		if(check(mid)) ans=mid,l=mid+1;
		else r=mid-1;
	}
	printf("%d\n",ans);
	check(ans);
	for(int i=2;i<n;i++)
	{
		for(int j=2;j<m;j++)
		{
			if(val(sum2,i,j,i,j)) printf("X");
			else printf(".");
		}
		puts("");
	}
}


#include<bits/stdc++.h>
#define MN 3000005
int n,m,sum1[MN],sum2[MN];
char mp[MN];
int go(int x,int y){if(!x||!y) return 0;return (x-1)*m+y;}
int val(int *sum,int x1,int y1,int x2,int y2){return sum[go(x2,y2)]-sum[go(x1-1,y2)]-sum[go(x2,y1-1)]+sum[go(x1-1,y1-1)];}
bool check(int x)
{
	memset(sum2,0,sizeof sum2);
	int dd=(x*2+1)*(x*2+1);
	for(int i=x+1;i<=n-x;i++)
		for(int j=x+1;j<=m-x;j++){
			if(dd==val(sum1,i-x,j-x,i+x,j+x)) sum2[go(i,j)]=1;
		}
	for(int i=1;i<=n*m;i++) {
		if(i>m) sum2[i]+=sum2[i-m];
		if(i%m!=1) sum2[i]+=sum2[i-1];
		if(i%m!=1&&i>m) sum2[i]-=sum2[i-m-1];
	}
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if(mp[go(i,j)]=='X')
				if(!val(sum2,std::max(i-x,1),std::max(j-x,1),std::min(n,i+x),std::min(m,j+x)))
					return 0;
	return 1;
}
int main()
{
	scanf("%d%d",&n,&m);
	n+=2;m+=2;
	for(int i=2;i<n;i++)
		scanf("%s",mp+(i-1)*m+2);
	for(int i=1;i<=n*m;i++) sum1[i]=(mp[i]=='X');
	for(int i=1;i<=n*m;i++) {
		if(i>m) sum1[i]+=sum1[i-m];
		if(i%m!=1) sum1[i]+=sum1[i-1];
		if(i%m!=1&&i>m) sum1[i]-=sum1[i-m-1];
	}
	int l=0,r=1e3,ans;
	while(l<=r)
	{
		int mid=l+r>>1;
		if(check(mid)) ans=mid,l=mid+1;
		else r=mid-1;
	}
	printf("%d\n",ans);
	check(ans);
	for(int i=2;i<n;i++)
	{
		for(int j=2;j<m;j++)
		{
			if(val(sum2,i,j,i,j)) printf("X");
			else printf(".");
		}
		puts("");
	}
}


#include<bits/stdc++.h>
#define MN 3000005
int n,m,sum1[MN],sum2[MN];
char mp[MN];
int go(int x,int y){if(!x) return 0;return (x-1)*m+y;}
int val(int *sum,int x1,int y1,int x2,int y2){return sum[go(x2,y2)]-sum[go(x1-1,y2)]-sum[go(x2,y1-1)]+sum[go(x1-1,y1-1)];}
bool check(int x)
{
	memset(sum2,0,sizeof sum2);
	int dd=(x*2+1)*(x*2+1);
	for(int i=x+1;i<=n-x;i++)
		for(int j=x+1;j<=m-x;j++){
			if(dd==val(sum1,i-x,j-x,i+x,j+x)) sum2[go(i,j)]=1;
		}
	for(int i=1;i<=n*m;i++) sum2[i]+=(i%m==1?0:sum2[i-1])+sum2[std::max(i-m,0)]-sum2[std::max(i-m-1,0)];
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if(mp[go(i,j)]=='X')
				if(!val(sum2,std::max(i-x,1),std::max(j-x,1),std::min(n,i+x),std::min(m,j+x)))
					return 0;
	return 1;
}
int main()
{
	scanf("%d%d",&n,&m);
	n+=2;m+=2;
	for(int i=2;i<n;i++)
		scanf("%s",mp+(i-1)*m+2);
	for(int i=1;i<=n*m;i++) sum1[i]=(mp[i]=='X');
	for(int i=1;i<=n*m;i++) sum1[i]+=(i%m==1?0:sum1[i-1])+sum1[std::max(i-m,0)]-sum1[std::max(i-m-1,0)];
	int l=0,r=1e3,ans;
	while(l<=r)
	{
		int mid=l+r>>1;
		if(check(mid)) ans=mid,l=mid+1;
		else r=mid-1;
	}
	printf("%d\n",ans);
	check(ans);
	for(int i=2;i<n;i++)
	{
		for(int j=2;j<m;j++)
		{
			if(val(sum2,i,j,i,j)) printf("X");
			else printf(".");
		}
		puts("");
	}
}


#include<bits/stdc++.h>
#define MN 3000005
int n,m,sum1[MN],sum2[MN];
char mp[MN];
int go(int x,int y){if(!x) return 0;return (x-1)*m+y;}
int val(int *sum,int x1,int y1,int x2,int y2){return sum[go(x2,y2)]-sum[go(x1-1,y2)]-sum[go(x2,y1-1)]+sum[go(x1-1,y1-1)];}
bool check(int x)
{
	memset(sum2,0,sizeof sum2);
	int dd=(x*2+1)*(x*2+1);
	for(int i=x+1;i<=n-x;i++)
		for(int j=x+1;j<=m-x;j++){
			if(dd==val(sum1,i-x,j-x,i+x,j+x)) sum2[go(i,j)]=1;
		}
	for(int i=1;i<=n*m;i++) sum2[i]+=sum2[i-1]+sum2[std::max(i-m,0)]-sum2[std::max(i-m-1,0)];
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if(mp[go(i,j)]=='X')
				if(!val(sum2,std::max(i-x,1),std::max(j-x,1),std::min(n,i+x),std::min(m,j+x)))
					return 0;
	return 1;
}
int main()
{
	scanf("%d%d",&n,&m);
	n+=2;m+=2;
	for(int i=2;i<n;i++)
		scanf("%s",mp+(i-1)*m+2);
	for(int i=1;i<=n*m;i++) sum1[i]=(mp[i]=='X');
	for(int i=1;i<=n*m;i++) sum1[i]+=(i%m==1?0:sum1[i-1])+sum1[std::max(i-m,0)]-sum1[std::max(i-m-1,0)];
	int l=0,r=1e3,ans;
	while(l<=r)
	{
		int mid=l+r>>1;
		if(check(mid)) ans=mid,l=mid+1;
		else r=mid-1;
	}
	printf("%d\n",ans);
	check(ans);
	for(int i=2;i<n;i++)
	{
		for(int j=2;j<m;j++)
		{
			if(val(sum2,i,j,i,j)) printf("X");
			else printf(".");
		}
		puts("");
	}
}


#include<bits/stdc++.h>
#define MN 1000005
int n,m,sum1[MN],sum2[MN];
char mp[MN];
int go(int x,int y){return (x-1)*m+y;}
int val(int *sum,int x1,int y1,int x2,int y2){return sum[go(x2,y2)]-sum[go(x1-1,y2)]-sum[go(x2,y1-1)]+sum[go(x1-1,y1-1)];}
bool check(int x)
{
	memset(sum2,0,sizeof sum2);
	int dd=(x*2+1)*(x*2+1);
	for(int i=x+1;i<=n-x;i++)
		for(int j=x+1;j<=m-x;j++){
			if(dd==val(sum1,i-x,j-x,i+x,j+x)) sum2[go(i,j)]=1;
		}
	for(int i=1;i<=n*m;i++) sum2[i]+=sum2[i-1]+sum2[std::max(i-m,0)]-sum2[std::max(i-m-1,0)];
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if(mp[go(i,j)]=='X')
				if(!val(sum2,std::max(i-x,1),std::max(j-x,1),std::min(n,i+x),std::min(m,j+x)))
					return 0;
	return 1;
}
int main()
{
	scanf("%d%d",&n,&m);
	n+=2;m+=2;
	for(int i=2;i<n;i++)
		scanf("%s",mp+(i-1)*m+2);
	for(int i=1;i<=n*m;i++) sum1[i]=(mp[i]=='X');
	for(int i=1;i<=n*m;i++) sum1[i]+=(i%m==1?0:sum1[i-1])+sum1[std::max(i-m,0)]-sum1[std::max(i-m-1,0)];
	int l=0,r=1e3,ans;
	while(l<=r)
	{
		int mid=l+r>>1;
		if(check(mid)) ans=mid,l=mid+1;
		else r=mid-1;
	}
	printf("%d\n",ans);
	check(ans);
	for(int i=2;i<n;i++)
	{
		for(int j=2;j<m;j++)
		{
			if(val(sum2,i,j,i,j)) printf("X");
			else printf(".");
		}
		puts("");
	}
}


#include<bits/stdc++.h>
#define MN 3000005
int n,m,sum1[MN],sum2[MN];
char mp[MN];
int go(int x,int y){if(!x) return 0;return (x-1)*m+y;}
int val(int *sum,int x1,int y1,int x2,int y2){return sum[go(x2,y2)]-sum[go(x1-1,y2)]-sum[go(x2,y1-1)]+sum[go(x1-1,y1-1)];}
bool check(int x)
{
	memset(sum2,0,sizeof sum2);
	int dd=(x*2+1)*(x*2+1);
	for(int i=x+1;i<=n-x;i++)
		for(int j=x+1;j<=m-x;j++){
			if(dd==val(sum1,i-x,j-x,i+x,j+x)) sum2[go(i,j)]=1;
		}
	for(int i=1;i<=n*m;i++) sum2[i]+=(i%m==1?0:(sum2[i-1]-sum2[std::max(i-m-1,0)]))+sum2[std::max(i-m,0)];
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if(mp[go(i,j)]=='X')
				if(!val(sum2,std::max(i-x,1),std::max(j-x,1),std::min(n,i+x),std::min(m,j+x)))
					return 0;
	return 1;
}
int main()
{
	scanf("%d%d",&n,&m);
	n+=2;m+=2;
	for(int i=2;i<n;i++)
		scanf("%s",mp+(i-1)*m+2);
	for(int i=1;i<=n*m;i++) sum1[i]=(mp[i]=='X');
	for(int i=1;i<=n*m;i++) sum1[i]+=(i%m==1?0:(sum1[i-1]-sum1[std::max(i-m-1,0)]))+sum1[std::max(i-m,0)];
	int l=0,r=1e3,ans;
	while(l<=r)
	{
		int mid=l+r>>1;
		if(check(mid)) ans=mid,l=mid+1;
		else r=mid-1;
	}
	printf("%d\n",ans);
	check(ans);
	for(int i=2;i<n;i++)
	{
		for(int j=2;j<m;j++)
		{
			if(val(sum2,i,j,i,j)) printf("X");
			else printf(".");
		}
		puts("");
	}
}


#include<bits/stdc++.h>
#define MN 1005005
int n,m,sum1[MN],sum2[MN];
char mp[MN];
int go(int x,int y){return (x-1)*m+y;}
int val(int *sum,int x1,int y1,int x2,int y2){return sum[go(x2,y2)]-sum[go(x1-1,y2)]-sum[go(x2,y1-1)]+sum[go(x1-1,y1-1)];}
bool check(int x)
{
	memset(sum2,0,sizeof sum2);
	int dd=(x*2+1)*(x*2+1);
	for(int i=x+1;i<=n-x;i++)
		for(int j=x+1;j<=m-x;j++){
			if(dd==val(sum1,i-x,j-x,i+x,j+x)) sum2[go(i,j)]=1;
		}
	for(int i=1;i<=n*m;i++) sum2[i]+=sum2[i-1]+sum2[std::max(i-m,0)]-sum2[std::max(i-m-1,0)];
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if(mp[go(i,j)]=='X')
				if(!val(sum2,std::max(i-x,1),std::max(j-x,1),std::min(n,i+x),std::min(m,j+x)))
					return 0;
	return 1;
}
int main()
{
	scanf("%d%d",&n,&m);
	n+=2;m+=2;
	for(int i=2;i<n;i++)
		scanf("%s",mp+(i-1)*m+2);
	for(int i=1;i<=n*m;i++) sum1[i]=(mp[i]=='X');
	for(int i=1;i<=n*m;i++) sum1[i]+=(i%m==1?0:sum1[i-1])+sum1[std::max(i-m,0)]-sum1[std::max(i-m-1,0)];
	int l=0,r=1e3,ans;
	while(l<=r)
	{
		int mid=l+r>>1;
		if(check(mid)) ans=mid,l=mid+1;
		else r=mid-1;
	}
	printf("%d\n",ans);
	check(ans);
	for(int i=2;i<n;i++)
	{
		for(int j=2;j<m;j++)
		{
			if(val(sum2,i,j,i,j)) printf("X");
			else printf(".");
		}
		puts("");
	}
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <math.h>
#include <queue>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <ctime>
#include <iomanip>
#include <fstream>
using namespace std;

#define PB push_back
#define SPC << " " <<

#define ALL(c) (c).begin(),(c).end()
#define IN(x,c) (find(c.begin(),c.end(),x) != (c).end())
#define REP(i,n) for (int i=0;i<(int)(n);i++)
#define FOR(i,a,b) for (int i=(a);i<=(b);i++)
#define INIT(a,v) memset(a,v,sizeof(a))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
template<class A, class B> A cvt(B x) { stringstream ss; ss<<x; A y; ss>>y; return y; }

#define DEBUG(x) cerr << #x << " = "; cerr << x << endl;
#define DEBUG_ITER(x) cerr << #x << " = "; for (auto _ : x) cerr << _ << ' '; cerr << endl;

typedef pair<int,int> PII;
typedef long long int64;
typedef vector<int> VI;
typedef vector<string> VS;

int n,m;

int get(vector<VI> &f, int i, int j, int t) {
	int x1=max(j-t-1,0), x2=min(j+t,m);
	int y1=max(i-t-1,0), y2=min(i+t,n);
	return f[y2][x2]-f[y2][x1]-f[y1][x2]+f[y1][x1];
}

int main() {
	//freopen("test.in","r",stdin);
	//freopen("test.out","w",stdout);
	cin >> n >> m;
	VS s(n+1);
	s[0]=string(m+1,'?');
	FOR (i,1,n) {
		cin >> s[i];
		s[i]="?"+s[i];
	}
	vector<VI> f(n+1, VI(m+1));
	vector<PII> pts;
	FOR (i,0,n) {
		FOR (j,0,m) {
			if (s[i][j]=='X') {
				f[i][j]=1;
				pts.PB({i,j});
			}
			if (i-1>=0) f[i][j]+=f[i-1][j];
			if (j-1>=0) f[i][j]+=f[i][j-1];
			if (i-1>=0 && j-1>=0) f[i][j]-=f[i-1][j-1];
		}
	}
	sort(ALL(pts));
	int lo=0,hi=max(n,m)+1;
	while (lo+1<hi) {
		int t=(lo+hi)/2;
		vector<PII> tar;
		FOR (i,1,n) {
			FOR (j,1,m) {
				if (get(f,i,j,t)==(1LL+2*t)*(1LL+2*t)) {
					tar.PB({i,j});
				}
			}
		}
		sort(ALL(tar));
		set<PII> st;
		int i1=0,i2=0;
		int ok=1;
		REP (j,pts.size()) {
			int y=pts[j].first, x=pts[j].second;
			while (i1<tar.size() && tar[i1].first<pts[j].first-t) { st.erase({tar[i1].second, tar[i1].first}); i1++; }
			while (i2<tar.size() && tar[i2].first<=pts[j].first+t) { st.insert({tar[i2].second, tar[i2].first}); i2++; }
			auto it = st.lower_bound({pts[j].second-t, -1});
			if (it==st.end() || abs(it->first - pts[j].second)>t) {
				ok=0;
				break;
			}
		}
		if (ok) lo=t; else hi=t;
	}
	cout << lo << endl;
	int t=lo;
	FOR (i,1,n) {
		FOR (j,1,m) {
			if (get(f,i,j,t)==(1LL+2*t)*(1LL+2*t)) cout << "X";
			else cout << ".";
		}
		cout << "\n";
	}
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <math.h>
#include <queue>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <ctime>
#include <iomanip>
#include <fstream>
using namespace std;

#define PB push_back
#define SPC << " " <<

#define ALL(c) (c).begin(),(c).end()
#define IN(x,c) (find(c.begin(),c.end(),x) != (c).end())
#define REP(i,n) for (int i=0;i<(int)(n);i++)
#define FOR(i,a,b) for (int i=(a);i<=(b);i++)
#define INIT(a,v) memset(a,v,sizeof(a))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
template<class A, class B> A cvt(B x) { stringstream ss; ss<<x; A y; ss>>y; return y; }

#define DEBUG(x) cerr << #x << " = "; cerr << x << endl;
#define DEBUG_ITER(x) cerr << #x << " = "; for (auto _ : x) cerr << _ << ' '; cerr << endl;

typedef pair<int,int> PII;
typedef long long int64;
typedef vector<int> VI;
typedef vector<string> VS;

int n,m;

int get(vector<VI> &f, int i, int j, int t) {
	int x1=max(j-t-1,0), x2=min(j+t,m);
	int y1=max(i-t-1,0), y2=min(i+t,n);
	return f[y2][x2]-f[y2][x1]-f[y1][x2]+f[y1][x1];
}

int main() {
	//freopen("test.in","r",stdin);
	cin >> n >> m;
	VS s(n+1);
	s[0]=string(n+1,'?');
	FOR (i,1,n) {
		cin >> s[i];
		s[i]="?"+s[i];
	}
	vector<VI> f(n+1, VI(m+1));
	vector<PII> pts;
	FOR (i,0,n) {
		FOR (j,0,m) {
			if (s[i][j]=='X') {
				f[i][j]=1;
				pts.PB({i,j});
			}
			if (i-1>=0) f[i][j]+=f[i-1][j];
			if (j-1>=0) f[i][j]+=f[i][j-1];
			if (i-1>=0 && j-1>=0) f[i][j]-=f[i-1][j-1];
		}
	}
	sort(ALL(pts));
	int lo=0,hi=max(n,m)+1;
	while (lo+1<hi) {
		int t=(lo+hi)/2;
		vector<VI> g(n+1, VI(m+1));
		vector<PII> tar;
		FOR (i,1,n) {
			FOR (j,1,m) {
				if (get(f,i,j,t)==(1LL+2*t)*(1LL+2*t)) {
					g[i][j]=1;
					tar.PB({i,j});
				}
			}
		}
		sort(ALL(tar));
		set<PII> st;
		int i1=0,i2=0;
		int ok=1;
		REP (j,pts.size()) {
			int y=pts[j].first, x=pts[j].second;
			while (i1<tar.size() && tar[i1].first<pts[j].first-t) { st.erase({tar[i1].second, pts[i1].first}); i1++; }
			while (i2<tar.size() && tar[i2].first<=pts[j].first+t) { st.insert({tar[i2].second, tar[i2].first}); i2++; }
			auto it = st.lower_bound({pts[j].second-t, -1});
			if (it==st.end() || abs(it->first - pts[j].second)>t) {
				ok=0;
				break;
			}
		}
		if (ok) lo=t; else hi=t;
	}
	cout << lo << endl;
	int t=lo;
	FOR (i,1,n) {
		FOR (j,1,m) {
			if (get(f,i,j,t)==(1LL+2*t)*(1LL+2*t)) cerr << "X";
			else cerr << ".";
		}
		cerr << "\n";
	}
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <math.h>
#include <queue>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <ctime>
#include <iomanip>
#include <fstream>
using namespace std;

#define PB push_back
#define SPC << " " <<

#define ALL(c) (c).begin(),(c).end()
#define IN(x,c) (find(c.begin(),c.end(),x) != (c).end())
#define REP(i,n) for (int i=0;i<(int)(n);i++)
#define FOR(i,a,b) for (int i=(a);i<=(b);i++)
#define INIT(a,v) memset(a,v,sizeof(a))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
template<class A, class B> A cvt(B x) { stringstream ss; ss<<x; A y; ss>>y; return y; }

#define DEBUG(x) cerr << #x << " = "; cerr << x << endl;
#define DEBUG_ITER(x) cerr << #x << " = "; for (auto _ : x) cerr << _ << ' '; cerr << endl;

typedef pair<int,int> PII;
typedef long long int64;
typedef vector<int> VI;
typedef vector<string> VS;

int n,m;

int get(vector<VI> &f, int i, int j, int t) {
	int x1=max(j-t-1,0), x2=min(j+t,m);
	int y1=max(i-t-1,0), y2=min(i+t,n);
	return f[y2][x2]-f[y2][x1]-f[y1][x2]+f[y1][x1];
}

int main() {
	//freopen("test.in","r",stdin);
	//freopen("test.out","w",stdout);
	cin >> n >> m;
	VS s(n+1);
	s[0]=string(n+1,'?');
	FOR (i,1,n) {
		cin >> s[i];
		s[i]="?"+s[i];
	}
	vector<VI> f(n+1, VI(m+1));
	vector<PII> pts;
	FOR (i,0,n) {
		FOR (j,0,m) {
			if (s[i][j]=='X') {
				f[i][j]=1;
				pts.PB({i,j});
			}
			if (i-1>=0) f[i][j]+=f[i-1][j];
			if (j-1>=0) f[i][j]+=f[i][j-1];
			if (i-1>=0 && j-1>=0) f[i][j]-=f[i-1][j-1];
		}
	}
	sort(ALL(pts));
	int lo=0,hi=max(n,m)+1;
	while (lo+1<hi) {
		int t=(lo+hi)/2;
		vector<VI> g(n+1, VI(m+1));
		vector<PII> tar;
		FOR (i,1,n) {
			FOR (j,1,m) {
				if (get(f,i,j,t)==(1LL+2*t)*(1LL+2*t)) {
					g[i][j]=1;
					tar.PB({i,j});
				}
			}
		}
		sort(ALL(tar));
		set<PII> st;
		int i1=0,i2=0;
		int ok=1;
		REP (j,pts.size()) {
			int y=pts[j].first, x=pts[j].second;
			while (i1<tar.size() && tar[i1].first<pts[j].first-t) { st.erase({tar[i1].second, pts[i1].first}); i1++; }
			while (i2<tar.size() && tar[i2].first<=pts[j].first+t) { st.insert({tar[i2].second, tar[i2].first}); i2++; }
			auto it = st.lower_bound({pts[j].second-t, -1});
			if (it==st.end() || abs(it->first - pts[j].second)>t) {
				ok=0;
				break;
			}
		}
		if (ok) lo=t; else hi=t;
	}
	cout << lo << endl;
	int t=lo;
	FOR (i,1,n) {
		FOR (j,1,m) {
			if (get(f,i,j,t)==(1LL+2*t)*(1LL+2*t)) cout << "X";
			else cout << ".";
		}
		cout << "\n";
	}
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <math.h>
#include <queue>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <ctime>
#include <iomanip>
#include <fstream>
using namespace std;

#define PB push_back
#define SPC << " " <<

#define ALL(c) (c).begin(),(c).end()
#define IN(x,c) (find(c.begin(),c.end(),x) != (c).end())
#define REP(i,n) for (int i=0;i<(int)(n);i++)
#define FOR(i,a,b) for (int i=(a);i<=(b);i++)
#define INIT(a,v) memset(a,v,sizeof(a))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
template<class A, class B> A cvt(B x) { stringstream ss; ss<<x; A y; ss>>y; return y; }

#define DEBUG(x) cerr << #x << " = "; cerr << x << endl;
#define DEBUG_ITER(x) cerr << #x << " = "; for (auto _ : x) cerr << _ << ' '; cerr << endl;

typedef pair<int,int> PII;
typedef long long int64;
typedef vector<int> VI;
typedef vector<string> VS;

int n,m;

int get(vector<VI> &f, int i, int j, int t) {
	int x1=max(j-t-1,0), x2=min(j+t,m);
	int y1=max(i-t-1,0), y2=min(i+t,n);
	return f[y2][x2]-f[y2][x1]-f[y1][x2]+f[y1][x1];
}

int main() {
	//freopen("test.in","r",stdin);
	//freopen("test.out","w",stdout);
	cin >> n >> m;
	VS s(n+1);
	s[0]=string(m+1,'?');
	FOR (i,1,n) {
		cin >> s[i];
		s[i]="?"+s[i];
	}
	vector<VI> f(n+1, VI(m+1));
	vector<PII> pts;
	FOR (i,0,n) {
		FOR (j,0,m) {
			if (s[i][j]=='X') {
				f[i][j]=1;
				pts.PB({i,j});
			}
			if (i-1>=0) f[i][j]+=f[i-1][j];
			if (j-1>=0) f[i][j]+=f[i][j-1];
			if (i-1>=0 && j-1>=0) f[i][j]-=f[i-1][j-1];
		}
	}
	sort(ALL(pts));
	int lo=0,hi=max(n,m)+1;
	while (lo+1<hi) {
		int t=(lo+hi)/2;
		vector<PII> tar;
		FOR (i,1,n) {
			FOR (j,1,m) {
				if (get(f,i,j,t)==(1LL+2*t)*(1LL+2*t)) {
					tar.PB({i,j});
				}
			}
		}
		sort(ALL(tar));
		set<PII> st;
		int i1=0,i2=0;
		int ok=1;
		REP (j,pts.size()) {
			int y=pts[j].first, x=pts[j].second;
			while (i1<tar.size() && tar[i1].first<pts[j].first-t) { st.erase({tar[i1].second, pts[i1].first}); i1++; }
			while (i2<tar.size() && tar[i2].first<=pts[j].first+t) { st.insert({tar[i2].second, tar[i2].first}); i2++; }
			auto it = st.lower_bound({pts[j].second-t, -1});
			if (it==st.end() || abs(it->first - pts[j].second)>t) {
				ok=0;
				break;
			}
		}
		if (ok) lo=t; else hi=t;
	}
	cout << lo << endl;
	int t=lo;
	FOR (i,1,n) {
		FOR (j,1,m) {
			if (get(f,i,j,t)==(1LL+2*t)*(1LL+2*t)) cout << "X";
			else cout << ".";
		}
		cout << "\n";
	}
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <math.h>
#include <queue>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <ctime>
#include <iomanip>
#include <fstream>
using namespace std;

#define PB push_back
#define SPC << " " <<

#define ALL(c) (c).begin(),(c).end()
#define IN(x,c) (find(c.begin(),c.end(),x) != (c).end())
#define REP(i,n) for (int i=0;i<(int)(n);i++)
#define FOR(i,a,b) for (int i=(a);i<=(b);i++)
#define INIT(a,v) memset(a,v,sizeof(a))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
template<class A, class B> A cvt(B x) { stringstream ss; ss<<x; A y; ss>>y; return y; }

#define DEBUG(x) cerr << #x << " = "; cerr << x << endl;
#define DEBUG_ITER(x) cerr << #x << " = "; for (auto _ : x) cerr << _ << ' '; cerr << endl;

typedef pair<int,int> PII;
typedef long long int64;
typedef vector<int> VI;
typedef vector<string> VS;

int n,m;

int get(vector<VI> &f, int i, int j, int t) {
	int x1=max(j-t-1,0), x2=min(j+t,m);
	int y1=max(i-t-1,0), y2=min(i+t,n);
	return f[y2][x2]-f[y2][x1]-f[y1][x2]+f[y1][x1];
}

int main() {
	//freopen("test.in","r",stdin);
	//freopen("test.out","w",stdout);
	cin >> n >> m;
	VS s(n+1);
	s[0]=string(m+1,'?');
	FOR (i,1,n) {
		cin >> s[i];
		s[i]="?"+s[i];
	}
	vector<VI> f(n+1, VI(m+1));
	vector<PII> pts;
	FOR (i,0,n) {
		FOR (j,0,m) {
			if (s[i][j]=='X') {
				f[i][j]=1;
				pts.PB({i,j});
			}
			if (i-1>=0) f[i][j]+=f[i-1][j];
			if (j-1>=0) f[i][j]+=f[i][j-1];
			if (i-1>=0 && j-1>=0) f[i][j]-=f[i-1][j-1];
		}
	}
	sort(ALL(pts));
	int lo=0,hi=max(n,m)+1;
	while (lo+1<hi) {
		int t=(lo+hi)/2;
		vector<VI> g(n+1, VI(m+1));
		vector<PII> tar;
		FOR (i,1,n) {
			FOR (j,1,m) {
				if (get(f,i,j,t)==(1LL+2*t)*(1LL+2*t)) {
					g[i][j]=1;
					tar.PB({i,j});
				}
			}
		}
		sort(ALL(tar));
		set<PII> st;
		int i1=0,i2=0;
		int ok=1;
		REP (j,pts.size()) {
			int y=pts[j].first, x=pts[j].second;
			while (i1<tar.size() && tar[i1].first<pts[j].first-t) { st.erase({tar[i1].second, pts[i1].first}); i1++; }
			while (i2<tar.size() && tar[i2].first<=pts[j].first+t) { st.insert({tar[i2].second, tar[i2].first}); i2++; }
			auto it = st.lower_bound({pts[j].second-t, -1});
			if (it==st.end() || abs(it->first - pts[j].second)>t) {
				ok=0;
				break;
			}
		}
		if (ok) lo=t; else hi=t;
	}
	cout << lo << endl;
	int t=lo;
	FOR (i,1,n) {
		FOR (j,1,m) {
			if (get(f,i,j,t)==(1LL+2*t)*(1LL+2*t)) cout << "X";
			else cout << ".";
		}
		cout << "\n";
	}
	return 0;
}


#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
#include <deque>
#include <cmath>
#include <math.h>
#include <string>
#include <time.h>
#include <random>
#include <memory.h>
#include <bitset>
using namespace std;

#define FILE0
#define MAGIC0
mt19937 rnd(time(0)); //rnd()

#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mii map<int, int>
#define mll map<ll, ll>
#define msl map<string, ll>
#define mls map<ll, string>
#define MOD (1000000007ll)
#define MOD2 (1000000009ll)
#define BIGMOD (1000000000000000007ll)
#define RMOD int(5e8 + rnd() % (int) 5e8)
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

#ifdef FILE1
ifstream in("input.txt");
ofstream out("output.txt");
#endif
#ifdef MAGIC1
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#endif

bool isPrime(ll a)
{
	ll i;
	for (i = 2; i*i <= a; i++)
	{
		if (a%i == 0)
		{
			return 0;
		}
	}
	return (a != 1);
}

bool isPalindrom(const string &s, int l, int r)
{
	for (int i = 0; i < (r - l + 1) / 2; i++)
	{
		if (s[l + i] != s[r - i])
		{
			return 0;
		}
	}
	return 1;
}

template <class type1> type1 gcd(type1 a, type1 b)
{
	type1 t;
	while (b)
	{
		t = b;
		b = a%b;
		a = t;
	}
	return a;
}

ll positiveMod(ll v, ll md)
{
	v = v%md;
	if (v < 0)
	{
		v += md;
	}
	return v;
}

//-----------------------------------------------------end--------------------------------------//

int n, m;
vector<vector<char> > dat;
vector<vector<int> > pref;
vector<vector<int> > inf;

vector<pii> res;

int check(int k)
{
	if (k > n || k > m)
	{
		return 0;
	}

	int i, j, sz = 1 + 2 * k, a, b, sz2 = sz*sz;
	vector<pii> tr;
	tr.reserve(1000005);

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

	for (i = 1; i <= n - sz + 1; i++)
	{
		for (j = 1; j <= m - sz + 1; j++)
		{
			a = pref[i + sz - 1][j + sz - 1] - pref[i - 1][j + sz - 1] - pref[i + sz - 1][j - 1] + pref[i - 1][j - 1];

			if (a == sz2)
			{
				inf[i][j] += 1;
				inf[i][j+sz] += -1;

				inf[i + sz][j] += -1;
				inf[i + sz][j + sz] += 1;

				tr.push_back({ i, j });
			}
		}
	}

	for (i = 1; i <= n; i++)
	{
		for (j = 1; j <= m; j++)
		{
			inf[i][j] += inf[i][j - 1];
		}
	}

	for (i = 1; i <= n; i++)
	{
		for (j = 1; j <= m; j++)
		{
			inf[i][j] += inf[i - 1][j];
		}
	}

	for (i = 1; i <= n; i++)
	{
		for (j = 1; j <= m; j++)
		{
			if (dat[i][j] == 'X' && inf[i][j] == 0)
			{
				return 0;
			}
		}
	}

	res = tr;

	return 1;
}

int bin(int l, int r)
{
	if (l == r)
	{
		return l;
	}

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

	if (check(mid) == 0)
	{
		return bin(l, mid - 1);
	}
	else
	{
		return bin(mid, r);
	}
}

int main()
{
	fast;

	res.reserve(1000001);

	int i, j;
	char c;
	cin >> n >> m;

	string s;

	dat.resize(n + 2, vector<char>(m + 2, '.'));
	pref.resize(n + 2, vector<int>(m + 2, 0));
	inf.resize(n + 2, vector<int>(m + 2, 0));

	for (i = 1; i <= n; i++)
	{
		cin >> s;
		for (j = 1; j <= m; j++)
		{
			dat[i][j] = s[j - 1];
		}
	}

	for (i = 1; i <= n; i++)
	{
		for (j = 1; j <= m; j++)
		{
			pref[i][j] = pref[i - 1][j] + pref[i][j - 1] - pref[i - 1][j - 1];
			if (dat[i][j] == 'X')
			{
				pref[i][j]++;
			}
		}
	}

	int res_ = bin(0, 501);

	if (res_ != 0)
	{
		for (i = 1; i <= n; i++)
		{
			for (j = 1; j <= m; j++)
			{
				dat[i][j] = '.';
			}
		}

		for (auto it : res)
		{
			dat[it.first + res_][it.second + res_] = 'X';
		}
	}

	cout << res_ << "\n";
	for (i = 1; i <= n; i++)
	{
		for (j = 1; j <= m; j++)
		{
			cout << dat[i][j];
		}
		cout << "\n";
	}

	return 0;
}


#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
#include <deque>
#include <cmath>
#include <math.h>
#include <string>
#include <time.h>
#include <random>
#include <memory.h>
#include <bitset>
using namespace std;

#define FILE0
#define MAGIC0
mt19937 rnd(time(0)); //rnd()

#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mii map<int, int>
#define mll map<ll, ll>
#define msl map<string, ll>
#define mls map<ll, string>
#define MOD (1000000007ll)
#define MOD2 (1000000009ll)
#define BIGMOD (1000000000000000007ll)
#define RMOD int(5e8 + rnd() % (int) 5e8)
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

#ifdef FILE1
ifstream in("input.txt");
ofstream out("output.txt");
#endif
#ifdef MAGIC1
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#endif

bool isPrime(ll a)
{
	ll i;
	for (i = 2; i*i <= a; i++)
	{
		if (a%i == 0)
		{
			return 0;
		}
	}
	return (a != 1);
}

bool isPalindrom(const string &s, int l, int r)
{
	for (int i = 0; i < (r - l + 1) / 2; i++)
	{
		if (s[l + i] != s[r - i])
		{
			return 0;
		}
	}
	return 1;
}

template <class type1> type1 gcd(type1 a, type1 b)
{
	type1 t;
	while (b)
	{
		t = b;
		b = a%b;
		a = t;
	}
	return a;
}

ll positiveMod(ll v, ll md)
{
	v = v%md;
	if (v < 0)
	{
		v += md;
	}
	return v;
}

//-----------------------------------------------------end--------------------------------------//

int n, m;
vector<vector<char> > dat;
vector<vector<int> > pref;
vector<vector<int> > inf;

vector<pii> res;

int check(int k)
{
	if (k > n || k > m)
	{
		return 0;
	}

	int i, j, sz = 1 + 2 * k, a, b, sz2 = sz*sz;
	vector<pii> tr;
	tr.reserve(1000005);

	for (i = 1; i <= n; i++)
	{
		for (j = 1; j <= n; j++)
		{
			inf[i][j] = 0;
		}
	}

	for (i = 1; i <= n - sz + 1; i++)
	{
		for (j = 1; j <= m - sz + 1; j++)
		{
			a = pref[i + sz - 1][j + sz - 1] - pref[i - 1][j + sz - 1] - pref[i + sz - 1][j - 1] + pref[i - 1][j - 1];

			if (a == sz2)
			{
				inf[i][j] += 1;
				inf[i][j+sz] += -1;

				inf[i + sz][j] += -1;
				inf[i + sz][j + sz] += 1;

				tr.push_back({ i, j });
			}
		}
	}

	for (i = 1; i <= n; i++)
	{
		for (j = 1; j <= m; j++)
		{
			inf[i][j] += inf[i][j - 1];
		}
	}

	for (i = 1; i <= n; i++)
	{
		for (j = 1; j <= m; j++)
		{
			inf[i][j] += inf[i - 1][j];
		}
	}

	for (i = 1; i <= n; i++)
	{
		for (j = 1; j <= m; j++)
		{
			if (dat[i][j] == 'X' && inf[i][j] == 0)
			{
				return 0;
			}
		}
	}

	res = tr;

	return 1;
}

int bin(int l, int r)
{
	if (l == r)
	{
		return l;
	}

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

	if (check(mid) == 0)
	{
		return bin(l, mid - 1);
	}
	else
	{
		return bin(mid, r);
	}
}

int main()
{
	fast;

	res.reserve(1000001);

	int i, j;
	char c;
	cin >> n >> m;

	dat.resize(n + 2, vector<char>(m + 2, '.'));
	pref.resize(n + 2, vector<int>(m + 2, 0));
	inf.resize(n + 2, vector<int>(m + 2, 0));

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

	for (i = 1; i <= n; i++)
	{
		for (j = 1; j <= m; j++)
		{
			pref[i][j] = pref[i - 1][j] + pref[i][j - 1] - pref[i - 1][j - 1];
			if (dat[i][j] == 'X')
			{
				pref[i][j]++;
			}
		}
	}

	int res_ = bin(0, 501);

	if (res_ != 0)
	{
		for (i = 1; i <= n; i++)
		{
			for (j = 1; j <= m; j++)
			{
				dat[i][j] = '.';
			}
		}

		for (auto it : res)
		{
			dat[it.first + res_][it.second + res_] = 'X';
		}
	}

	cout << res_ << "\n";
	for (i = 1; i <= n; i++)
	{
		for (j = 1; j <= m; j++)
		{
			cout << dat[i][j];
		}
		cout << "\n";
	}

	return 0;
}


#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
#include <deque>
#include <cmath>
#include <math.h>
#include <string>
#include <time.h>
#include <random>
#include <memory.h>
#include <bitset>
using namespace std;

#define FILE0
#define MAGIC0
mt19937 rnd(time(0)); //rnd()

#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mii map<int, int>
#define mll map<ll, ll>
#define msl map<string, ll>
#define mls map<ll, string>
#define MOD (1000000007ll)
#define MOD2 (1000000009ll)
#define BIGMOD (1000000000000000007ll)
#define RMOD int(5e8 + rnd() % (int) 5e8)
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

#ifdef FILE1
ifstream in("input.txt");
ofstream out("output.txt");
#endif
#ifdef MAGIC1
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#endif

bool isPrime(ll a)
{
	ll i;
	for (i = 2; i*i <= a; i++)
	{
		if (a%i == 0)
		{
			return 0;
		}
	}
	return (a != 1);
}

bool isPalindrom(const string &s, int l, int r)
{
	for (int i = 0; i < (r - l + 1) / 2; i++)
	{
		if (s[l + i] != s[r - i])
		{
			return 0;
		}
	}
	return 1;
}

template <class type1> type1 gcd(type1 a, type1 b)
{
	type1 t;
	while (b)
	{
		t = b;
		b = a%b;
		a = t;
	}
	return a;
}

ll positiveMod(ll v, ll md)
{
	v = v%md;
	if (v < 0)
	{
		v += md;
	}
	return v;
}

//-----------------------------------------------------end--------------------------------------//

int n, m;
vector<vector<char> > dat;
vector<vector<int> > pref;
vector<vector<int> > inf;

vector<pii> res;

int check(int k)
{
	if (k > n || k > m)
	{
		return 0;
	}

	int i, j, sz = 1 + 2 * k, a, b, sz2 = sz*sz;
	vector<pii> tr;
	tr.reserve(1000005);

	for (i = 1; i <= n; i++)
	{
		for (j = 1; j <= n; j++)
		{
			inf[i][j] = 0;
		}
	}

	for (i = 1; i <= n - sz + 1; i++)
	{
		for (j = 1; j <= m - sz + 1; j++)
		{
			a = pref[i + sz - 1][j + sz - 1] - pref[i - 1][j + sz - 1] - pref[i + sz - 1][j - 1] + pref[i - 1][j - 1];

			if (a == sz2)
			{
				inf[i][j] += 1;
				inf[i][j+sz] += -1;

				inf[i + sz][j] += -1;
				inf[i + sz][j + sz] += 1;

				tr.push_back({ i, j });
			}
		}
	}

	for (i = 1; i <= n; i++)
	{
		for (j = 1; j <= m; j++)
		{
			inf[i][j] += inf[i][j - 1];
		}
	}

	for (i = 1; i <= n; i++)
	{
		for (j = 1; j <= m; j++)
		{
			inf[i][j] += inf[i - 1][j];
		}
	}

	for (i = 1; i <= n; i++)
	{
		for (j = 1; j <= m; j++)
		{
			if (dat[i][j] == 'X' && inf[i][j] == 0)
			{
				return 0;
			}
		}
	}

	res = tr;

	return 1;
}

int bin(int l, int r)
{
	if (l == r)
	{
		return l;
	}

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

	if (check(mid) == 0)
	{
		return bin(l, mid - 1);
	}
	else
	{
		return bin(mid, r);
	}
}

int main()
{
	fast;

	res.reserve(1000001);

	int i, j;
	char c;
	cin >> n >> m;

	string s;

	dat.resize(n + 2, vector<char>(m + 2, '.'));
	pref.resize(n + 2, vector<int>(m + 2, 0));
	inf.resize(n + 2, vector<int>(m + 2, 0));

	for (i = 1; i <= n; i++)
	{
		cin >> s;
		for (j = 1; j <= m; j++)
		{
			dat[i][j] = s[j - 1];
		}
	}

	for (i = 1; i <= n; i++)
	{
		for (j = 1; j <= m; j++)
		{
			pref[i][j] = pref[i - 1][j] + pref[i][j - 1] - pref[i - 1][j - 1];
			if (dat[i][j] == 'X')
			{
				pref[i][j]++;
			}
		}
	}

	int res_ = bin(0, 501);

	if (res_ != 0)
	{
		for (i = 1; i <= n; i++)
		{
			for (j = 1; j <= m; j++)
			{
				dat[i][j] = '.';
			}
		}

		for (auto it : res)
		{
			dat[it.first + res_][it.second + res_] = 'X';
		}
	}

	cout << res_ << "\n";
	for (i = 1; i <= n; i++)
	{
		for (j = 1; j <= m; j++)
		{
			cout << dat[i][j];
		}
		cout << "\n";
	}

	return 0;
}


#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
#include <deque>
#include <cmath>
#include <math.h>
#include <string>
#include <time.h>
#include <random>
#include <memory.h>
#include <bitset>
using namespace std;

#define FILE0
#define MAGIC0
mt19937 rnd(time(0)); //rnd()

#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mii map<int, int>
#define mll map<ll, ll>
#define msl map<string, ll>
#define mls map<ll, string>
#define MOD (1000000007ll)
#define MOD2 (1000000009ll)
#define BIGMOD (1000000000000000007ll)
#define RMOD int(5e8 + rnd() % (int) 5e8)
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

#ifdef FILE1
ifstream in("input.txt");
ofstream out("output.txt");
#endif
#ifdef MAGIC1
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#endif

bool isPrime(ll a)
{
	ll i;
	for (i = 2; i*i <= a; i++)
	{
		if (a%i == 0)
		{
			return 0;
		}
	}
	return (a != 1);
}

bool isPalindrom(const string &s, int l, int r)
{
	for (int i = 0; i < (r - l + 1) / 2; i++)
	{
		if (s[l + i] != s[r - i])
		{
			return 0;
		}
	}
	return 1;
}

template <class type1> type1 gcd(type1 a, type1 b)
{
	type1 t;
	while (b)
	{
		t = b;
		b = a%b;
		a = t;
	}
	return a;
}

ll positiveMod(ll v, ll md)
{
	v = v%md;
	if (v < 0)
	{
		v += md;
	}
	return v;
}

//-----------------------------------------------------end--------------------------------------//

int n, m;
vector<vector<char> > dat;
vector<vector<int> > pref;
vector<vector<int> > inf;

vector<pii> res;

int check(int k)
{
	if (k > n || k > m)
	{
		return 0;
	}

	int i, j, sz = 1 + 2 * k, a, b, sz2 = sz*sz;
	vector<pii> tr;
	tr.reserve(10000);

	for (i = 1; i <= n; i++)
	{
		for (j = 1; j <= n; j++)
		{
			inf[i][j] = 0;
		}
	}

	for (i = 1; i <= n - sz + 1; i++)
	{
		for (j = 1; j <= m - sz + 1; j++)
		{
			a = pref[i + sz - 1][j + sz - 1] - pref[i - 1][j + sz - 1] - pref[i + sz - 1][j - 1] + pref[i - 1][j - 1];

			if (a == sz2)
			{
				inf[i][j] += 1;
				inf[i][j+sz] += -1;

				inf[i + sz][j] += -1;
				inf[i + sz][j + sz] += 1;

				tr.push_back({ i, j });
			}
		}
	}

	for (i = 1; i <= n; i++)
	{
		for (j = 1; j <= m; j++)
		{
			inf[i][j] += inf[i][j - 1];
		}
	}

	for (i = 1; i <= n; i++)
	{
		for (j = 1; j <= m; j++)
		{
			inf[i][j] += inf[i - 1][j];
		}
	}

	for (i = 1; i <= n; i++)
	{
		for (j = 1; j <= m; j++)
		{
			if (dat[i][j] == 'X' && inf[i][j] == 0)
			{
				return 0;
			}
		}
	}

	res = tr;

	return 1;
}

int bin(int l, int r)
{
	if (l == r)
	{
		return l;
	}

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

	if (check(mid) == 0)
	{
		return bin(l, mid - 1);
	}
	else
	{
		return bin(mid, r);
	}
}

int main()
{
	fast;

	res.reserve(1000001);

	int i, j;
	char c;
	cin >> n >> m;

	dat.resize(n + 2, vector<char>(m + 2, '.'));
	pref.resize(n + 2, vector<int>(m + 2, 0));
	inf.resize(n + 2, vector<int>(m + 2, 0));

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

	for (i = 1; i <= n; i++)
	{
		for (j = 1; j <= m; j++)
		{
			pref[i][j] = pref[i - 1][j] + pref[i][j - 1] - pref[i - 1][j - 1];
			if (dat[i][j] == 'X')
			{
				pref[i][j]++;
			}
		}
	}

	int res_ = bin(0, 505);

	if (res_ != 0)
	{
		for (i = 1; i <= n; i++)
		{
			for (j = 1; j <= m; j++)
			{
				dat[i][j] = '.';
			}
		}

		for (auto it : res)
		{
			dat[it.first + res_][it.second + res_] = 'X';
		}
	}

	cout << res_ << "\n";
	for (i = 1; i <= n; i++)
	{
		for (j = 1; j <= m; j++)
		{
			cout << dat[i][j];
		}
		cout << "\n";
	}

	return 0;
}


// 17:00 -
#include<bits/stdc++.h>
using namespace std;

#define fi first
#define se second

typedef pair<int,int> pint;

const int N = 2e6 + 5;
char g[N];
int s[N], ans[N];
int n, m;

inline int Idx(int x, int y){
	return x * (m+2) + y;
}

bool Judge(int dis){
	// find ans
	for(int i=1; i<=n; i++)
		for(int j=1; j<=m; j++){
			int id = Idx(i, j);
			ans[id] = 0;
			if(g[id] != 'X') continue;
			if(i - dis <= 0 || i + dis > n) continue;
			if(j - dis <= 0 || j + dis > m) continue;

			int x1 = i - dis, x2 = i + dis;
			int y1 = j - dis, y2 = j + dis;

			int sum = s[Idx(x2, y2)] + s[Idx(x1-1, y1-1)] - s[Idx(x1-1, y2)] - s[Idx(x2, y1-1)];
			int area = (x2 - x1 + 1) * (y2 - y1 + 1);
			if(sum == area) ans[id] = 1;
		}

	// fill ans
	static int t[N];
	fill(t, t+N, 0);

	for(int i=1; i<=n; i++)
		for(int j=1; j<=m; j++){
			int id = Idx(i, j);
			if(!ans[id]) continue;
			if(i - dis <= 0 || i + dis > n) continue;
			if(j - dis <= 0 || j + dis > m) continue;

			int x1 = i - dis, x2 = i + dis;
			int y1 = j - dis, y2 = j + dis;

			t[Idx(x1, y1)]++; t[Idx(x2+1, y2+1)]++;
			t[Idx(x1, y2+1)]--; t[Idx(x2+1, y1)]--;
		}

	for(int i=1; i<=n; i++)
		for(int j=1; j<=m; j++){
			int id = Idx(i, j);
			t[id] += t[Idx(i, j-1)] + t[Idx(i-1, j)] - t[Idx(i-1, j-1)];
			bool f1 = (t[id] > 0);
			bool f2 = (g[id] == 'X');
			if(f1 != f2) return 0;
		}

	return 1;
}

int BSearch(int L, int R){
	while(L < R){
		int M = (L+R) / 2;
		if(Judge(M)) L = M+1;
		else R = M;
	}
	return L;
}

int main(){
	ios::sync_with_stdio(0);

	cin >> n >> m;
	for(int i=1; i<=n; i++)
		for(int j=1; j<=m; j++){
			int id = Idx(i, j);
			cin >> g[id];
			s[id] = (g[id] == 'X') + s[Idx(i, j-1)] + s[Idx(i-1, j)] - s[Idx(i-1, j-1)];
		}

	int ansVal = BSearch(0, max(n, m) + 1) - 1;
	Judge(ansVal);

	cout << ansVal << endl;
	for(int i=1; i<=n; i++){
		for(int j=1; j<=m; j++)
			cout << (ans[Idx(i, j)] ? 'X' : '.');
		cout << endl;
	}

	return 0;
}


// 17:00 -
#include<bits/stdc++.h>
using namespace std;

#define fi first
#define se second

typedef pair<int,int> pint;

const int N = 4e6 + 5;
char g[N];
int s[N], ans[N];
int n, m;

inline int Idx(int x, int y){
	return x * (m+2) + y;
}

bool Judge(int dis){
	// fill ans
	static int t[N];
	for(int i=1; i<=n; i++)
		for(int j=1; j<=m; j++)
			t[Idx(i, j)] = 0;

	// find ans
	for(int i=1; i<=n; i++)
		for(int j=1; j<=m; j++){
			int id = Idx(i, j);
			ans[id] = 0;
			if(g[id] != 'X') continue;
			if(i - dis <= 0 || i + dis > n) continue;
			if(j - dis <= 0 || j + dis > m) continue;

			int x1 = i - dis, x2 = i + dis;
			int y1 = j - dis, y2 = j + dis;

			int sum = s[Idx(x2, y2)] + s[Idx(x1-1, y1-1)] - s[Idx(x1-1, y2)] - s[Idx(x2, y1-1)];
			int area = (x2 - x1 + 1) * (y2 - y1 + 1);
			if(sum == area) ans[id] = 1;

			if(!ans[id]) continue;
			t[Idx(x1, y1)]++; t[Idx(x2+1, y2+1)]++;
			t[Idx(x1, y2+1)]--; t[Idx(x2+1, y1)]--;
		}

	for(int i=1; i<=n; i++)
		for(int j=1; j<=m; j++){
			int id = Idx(i, j);
			t[id] += t[Idx(i, j-1)] + t[Idx(i-1, j)] - t[Idx(i-1, j-1)];
			bool f1 = (t[id] > 0);
			bool f2 = (g[id] == 'X');
			if(f1 != f2) return 0;
		}

	return 1;
}

int BSearch(int L, int R){
	while(L < R){
		int M = (L+R) / 2;
		if(Judge(M)) L = M+1;
		else R = M;
	}
	return L;
}

int main(){
	scanf("%d%d", &n, &m);
	for(int i=1; i<=n; i++){
		static char tmp[N];
		scanf("%s", tmp+1);
		for(int j=1; j<=m; j++){
			int id = Idx(i, j);
			g[id] = tmp[j];
			s[id] = (g[id] == 'X') + s[Idx(i, j-1)] + s[Idx(i-1, j)] - s[Idx(i-1, j-1)];
		}
	}

	int ansVal = BSearch(0, max(n, m) + 1) - 1;
	Judge(ansVal);

	printf("%d\n", ansVal);
	for(int i=1; i<=n; i++){
		for(int j=1; j<=m; j++)
			printf(ans[Idx(i, j)] ? "X" : ".");
		printf("\n");
	}

	return 0;
}


// 17:00 -
#include<bits/stdc++.h>
using namespace std;

#define fi first
#define se second

typedef pair<int,int> pint;

const int N = 4e6 + 5;
char g[N];
int s[N], ans[N];
int n, m;

inline int Idx(int x, int y){
	return x * (m+2) + y;
}

bool Judge(int dis){
	// find ans
	for(int i=1; i<=n; i++)
		for(int j=1; j<=m; j++){
			int id = Idx(i, j);
			ans[id] = 0;
			if(g[id] != 'X') continue;
			if(i - dis <= 0 || i + dis > n) continue;
			if(j - dis <= 0 || j + dis > m) continue;

			int x1 = i - dis, x2 = i + dis;
			int y1 = j - dis, y2 = j + dis;

			int sum = s[Idx(x2, y2)] + s[Idx(x1-1, y1-1)] - s[Idx(x1-1, y2)] - s[Idx(x2, y1-1)];
			int area = (x2 - x1 + 1) * (y2 - y1 + 1);
			if(sum == area) ans[id] = 1;
		}

	// fill ans
	static int t[N];
	for(int i=1; i<=n; i++)
		for(int j=1; j<=m; j++)
			t[Idx(i, j)] = 0;

	for(int i=1; i<=n; i++)
		for(int j=1; j<=m; j++){
			int id = Idx(i, j);
			if(!ans[id]) continue;
			if(i - dis <= 0 || i + dis > n) continue;
			if(j - dis <= 0 || j + dis > m) continue;

			int x1 = i - dis, x2 = i + dis;
			int y1 = j - dis, y2 = j + dis;

			t[Idx(x1, y1)]++; t[Idx(x2+1, y2+1)]++;
			t[Idx(x1, y2+1)]--; t[Idx(x2+1, y1)]--;
		}

	for(int i=1; i<=n; i++)
		for(int j=1; j<=m; j++){
			int id = Idx(i, j);
			t[id] += t[Idx(i, j-1)] + t[Idx(i-1, j)] - t[Idx(i-1, j-1)];
			bool f1 = (t[id] > 0);
			bool f2 = (g[id] == 'X');
			if(f1 != f2) return 0;
		}

	return 1;
}

int BSearch(int L, int R){
	while(L < R){
		int M = (L+R) / 2;
		if(Judge(M)) L = M+1;
		else R = M;
	}
	return L;
}

int main(){
	ios::sync_with_stdio(0);

	cin >> n >> m;
	for(int i=1; i<=n; i++)
		for(int j=1; j<=m; j++){
			int id = Idx(i, j);
			cin >> g[id];
			s[id] = (g[id] == 'X') + s[Idx(i, j-1)] + s[Idx(i-1, j)] - s[Idx(i-1, j-1)];
		}

	int ansVal = BSearch(0, max(n, m) + 1) - 1;
	Judge(ansVal);

	cout << ansVal << endl;
	for(int i=1; i<=n; i++){
		for(int j=1; j<=m; j++)
			cout << (ans[Idx(i, j)] ? 'X' : '.');
		cout << endl;
	}

	return 0;
}


// 17:00 -
#include<bits/stdc++.h>
using namespace std;

#define fi first
#define se second

typedef pair<int,int> pint;

const int N = 4e6 + 5;
char g[N];
int s[N], ans[N];
int n, m;

inline int Idx(int x, int y){
	return x * (m+2) + y;
}

bool Judge(int dis){
	// find ans
	for(int i=1; i<=n; i++)
		for(int j=1; j<=m; j++){
			int id = Idx(i, j);
			ans[id] = 0;
			if(g[id] != 'X') continue;
			if(i - dis <= 0 || i + dis > n) continue;
			if(j - dis <= 0 || j + dis > m) continue;

			int x1 = i - dis, x2 = i + dis;
			int y1 = j - dis, y2 = j + dis;

			int sum = s[Idx(x2, y2)] + s[Idx(x1-1, y1-1)] - s[Idx(x1-1, y2)] - s[Idx(x2, y1-1)];
			int area = (x2 - x1 + 1) * (y2 - y1 + 1);
			if(sum == area) ans[id] = 1;
		}

	// fill ans
	static int t[N];
	fill(t, t+N, 0);

	for(int i=1; i<=n; i++)
		for(int j=1; j<=m; j++){
			int id = Idx(i, j);
			if(!ans[id]) continue;
			if(i - dis <= 0 || i + dis > n) continue;
			if(j - dis <= 0 || j + dis > m) continue;

			int x1 = i - dis, x2 = i + dis;
			int y1 = j - dis, y2 = j + dis;

			t[Idx(x1, y1)]++; t[Idx(x2+1, y2+1)]++;
			t[Idx(x1, y2+1)]--; t[Idx(x2+1, y1)]--;
		}

	for(int i=1; i<=n; i++)
		for(int j=1; j<=m; j++){
			int id = Idx(i, j);
			t[id] += t[Idx(i, j-1)] + t[Idx(i-1, j)] - t[Idx(i-1, j-1)];
			bool f1 = (t[id] > 0);
			bool f2 = (g[id] == 'X');
			if(f1 != f2) return 0;
		}

	return 1;
}

int BSearch(int L, int R){
	while(L < R){
		int M = (L+R) / 2;
		if(Judge(M)) L = M+1;
		else R = M;
	}
	return L;
}

int main(){
	ios::sync_with_stdio(0);

	cin >> n >> m;
	for(int i=1; i<=n; i++)
		for(int j=1; j<=m; j++){
			int id = Idx(i, j);
			cin >> g[id];
			s[id] = (g[id] == 'X') + s[Idx(i, j-1)] + s[Idx(i-1, j)] - s[Idx(i-1, j-1)];
		}

	int ansVal = BSearch(0, max(n, m) + 1) - 1;
	Judge(ansVal);

	cout << ansVal << endl;
	for(int i=1; i<=n; i++){
		for(int j=1; j<=m; j++)
			cout << (ans[Idx(i, j)] ? 'X' : '.');
		cout << endl;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > a,f,dd,ans;
int n,m,c=0;
int cal(int x1,int y1,int x2,int y2)
{
    return f[x2][y2]+f[x1-1][y1-1]-f[x1-1][y2]-f[x2][y1-1];
}
void update(int x1,int y1,int x2,int y2)
{
    dd[x1][y1]++;
    dd[x2+1][y2+1]++;
    dd[x1][y2+1]--;
    dd[x2+1][y1]--;
}
int check(int k)
{
    for(int i=0;i<=n+1;i++)
    for(int j=0;j<=m+1;j++)
    dd[i][j]=0;

    for(int i=k+1;i<=n-k;i++)
        for(int j=k+1;j<=m-k;j++)
            if(cal(i-k,j-k,i+k,j+k)==(2*k+1)*(2*k+1))
                update(i-k,j-k,i+k,j+k);
    for(int i=1;i<=n;i++)
    for(int j=1;j<=m;j++)
    dd[i][j]+=dd[i][j-1];

    for(int i=1;i<=n;i++)
    for(int j=1;j<=m;j++)
    dd[i][j]+=dd[i-1][j];
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            if(a[i][j]&&dd[i][j]==0) return 0;
    return 1;
}
int tim(int l,int r)
{
    if(l>r) return 0;
    int mid=(l+r)/2;
    if(check(mid)) return max(mid,tim(mid+1,r));
    return tim(l,mid-1);
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin>>n>>m;
    a.resize(n+3);  f.resize(n+3);
    dd.resize(n+3); ans.resize(n+3);
    for(int i=0;i<=n+1;i++)
    for(int j=0;j<=m+1;j++)
    {
        dd[i].push_back(0);
        a[i].push_back(0);
        f[i].push_back(0);
        ans[i].push_back(0);
    }
    for(int i=1;i<=n;i++)
    {
        string t;
        cin>>t;
        for(int j=0;j<t.size();j++)
        {
            if(t[j]=='X') a[i][j+1]=1;
        }
    }
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            f[i][j]=f[i-1][j]+f[i][j-1]-f[i-1][j-1]+a[i][j];
    c=tim(1,min(n,m)/2+1);
    for(int i=c+1;i<=n-c;i++)
        for(int j=c+1;j<=m-c;j++)
            if(cal(i-c,j-c,i+c,j+c)==(2*c+1)*(2*c+1))
                ans[i][j]=1;
    cout<<c<<'\n';
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
            if(ans[i][j]) cout<<'X';
            else cout<<'.';
        cout<<'\n';
    }
}


#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > a,f,dd,ans;
int n,m,c=0;
int cal(int x1,int y1,int x2,int y2)
{
    return f[x2][y2]+f[x1-1][y1-1]-f[x1-1][y2]-f[x2][y1-1];
}
void update(int x1,int y1,int x2,int y2)
{
    dd[x1][y1]++;
    dd[x2+1][y2+1]++;
    dd[x1][y2+1]--;
    dd[x2+1][y1]--;
}
int check(int k)
{
    for(int i=0;i<=n+1;i++)
    for(int j=0;j<=m+1;j++)
    dd[i][j]=0;

    for(int i=k+1;i<=n-k;i++)
        for(int j=k+1;j<=m-k;j++)
            if(cal(i-k,j-k,i+k,j+k)==(2*k+1)*(2*k+1))
                update(i-k,j-k,i+k,j+k);
    for(int i=1;i<=n;i++)
    for(int j=1;j<=m;j++)
    dd[i][j]+=dd[i][j-1];

    for(int i=1;i<=n;i++)
    for(int j=1;j<=m;j++)
    dd[i][j]+=dd[i-1][j];
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            if(a[i][j]&&dd[i][j]==0) return 0;
    return 1;
}
int tim(int l,int r)
{
    if(l>r) return 0;
    int mid=(l+r)/2;
    if(check(mid)) return max(mid,tim(mid+1,r));
    return tim(l,mid-1);
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin>>n>>m;
    a.resize(n+3);  f.resize(n+3);
    dd.resize(n+3); ans.resize(n+3);
    for(int i=0;i<=n+1;i++)
    for(int j=0;j<=m+1;j++)
    {
        dd[i].push_back(0);
        a[i].push_back(0);
        f[i].push_back(0);
        ans[i].push_back(0);
    }
    for(int i=1;i<=n;i++)
    {
        string t;
        cin>>t;
        for(int j=0;j<t.size();j++)
        {
            if(t[j]=='X') a[i][j+1]=1;
        }
    }
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            f[i][j]=f[i-1][j]+f[i][j-1]-f[i-1][j-1]+a[i][j];
    c=tim(1,min(n,m)/2+1);
    for(int i=c+1;i<=n-c;i++)
        for(int j=c+1;j<=m-c;j++)
            if(cal(i-c,j-c,i+c,j+c)==(2*c+1)*(2*c+1))
                ans[i][j]=1;
    cout<<c<<'\n';
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
            if(ans[i][j]) cout<<'X';
            else cout<<'.';
        cout<<'\n';
    }
}


#include<bits/stdc++.h>

using namespace std;

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

	int n,m;
	cin >> n >> m;
	vector<vector<int>> ta(n+2,vector<int>(m+2));
	for(int i=0;i<n;i++){
		string str;
		cin >> str;
		for(int j=0;j<m;j++){
			if(str[j]=='X') ta[i+1][j+1]=1;
			else ta[i+1][j+1]=0;
		}
	}
	n++,m++;
	n++,m++;

	vector<vector<int>> ta2(n,vector<int>(m,1<<29));
	vector<vector<bool>> ju(n,vector<bool>(m));
	stack<pair<int,int>> st;
	vector<vector<int>> hou={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
	priority_queue<pair<int,pair<int,int>>,vector<pair<int,pair<int,int>>>,greater<pair<int,pair<int,int>>>> pq;
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			if(ta[i][j]==0){
				ju[i][j]=true;
				pq.push(make_pair(0,make_pair(i,j)));
			}
		}
	}
	while(pq.size()){
		pair<int,int> p0=pq.top().second;
		int x=p0.first,y=p0.second;
		ta2[x][y]=pq.top().first;
		pq.pop();
		for(vector<int> h:hou){
			int nx=x+h[0],ny=y+h[1];
			if(nx<0 || nx>=n) continue;
			if(ny<0 || ny>=m) continue;
			if(ju[nx][ny]) continue;
			int mn=1<<29;
			for(vector<int> h2:hou){
				int nnx=nx+h2[0],nny=ny+h2[1];
				if(nnx<0 || nnx>=n) continue;
				if(nny<0 || nny>=m) continue;
				mn=min(mn,ta2[nnx][nny]);
			}
			mn++;
			ju[nx][ny]=true;
			ta2[nx][ny]=mn;
			pq.push(make_pair(mn,make_pair(nx,ny)));
		}
	}

	int res=1<<29;
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			if(ta2[i][j]==0) continue;
			bool uniq=false;
			for(vector<int> h:hou){
				int x=i+h[0],y=j+h[1];
				if(x<0 || x>=n) continue;
				if(y<0 || y>=m) continue;
				if(ta2[x][y]>=ta2[i][j]) continue;
				bool uniq2=true;
				for(vector<int> h2:hou){
					int nx=x+h2[0],ny=y+h2[1];
					if(nx<0 || nx>=n) continue;
					if(ny<0 || ny>=m) continue;
					if(nx==i && ny==j) continue;
					if(ta2[nx][ny]==ta2[i][j]) uniq2=false;
				}
				if(uniq2) uniq=true;
			}
			if(!uniq) continue;
			bool bo=false;
			for(vector<int> h:hou){
				int x=i+h[0],y=j+h[1];
				if(x<0 || x>=n) continue;
				if(y<0 || y>=m) continue;
				if((ta2[x][y]-1)>=(ta2[i][j])) bo=true;
			}
			if(!bo){
				res=min(res,ta2[i][j]);
				//cout << i << " " << j << "\n";
			}
		}
	}

	cout << (res-1) << "\n";
	/*
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			cout << ta2[i][j] << " ";
		}
		cout << "\n";
	}*/

	n--,m--;
	n--,m--;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(ta2[i][j]<res) cout << ".";
			else cout << "X";
		}
		cout << "\n";
	}

	return 0;
}


#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
#include<bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

#define F first
#define S second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()

void accell() {
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(0);
}

signed main() {
    accell();
    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    int n, m;
    cin >> n >> m;
    vector<vector<int> >matr(n + 1, vector<int>(m + 1));
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            char x;
            cin >> x;
            if (x == 'X')
                matr[i][j] = 1;
            else
                matr[i][j] = 0;
        }
    }
    vector<vector<int> > sq(n + 1, vector<int>(m + 1));
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            if (!matr[i][j])
                continue;
            int cur = sq[i - 1][j - 1];
            cur = min(cur, sq[i - 1][j]);
            cur = min(cur, sq[i][j - 1]);
            sq[i][j] = cur + 1;
        }
    }
    vector<vector<int> >pref(n + 1, vector<int>(m + 1));
    vector<vector<int> >good(n + 1, vector<int>(m + 1));
    int l = -1, r = 2e6;
    while (r - l > 1) {
        int mid = l + r >> 1;
        int ch = 2 * mid + 1;
        // cout << l << ' ' << r << endl;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                pref[i][j] = good[i][j] = 0;
            }
        }
        bool cur = true;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                if (sq[i][j] >= ch) {
                    int x = i - ch + 1;
                    int y = j - ch + 1;
                    if (x <= 0 || y <= 0) {
                        continue;
                    }
                    pref[x][y] += 1;
                    good[i - ch / 2][j - ch / 2] = 1;
                    // if (mid == 1)
                    //    cout << i - ch / 2 << ' ' << j - ch / 2 << endl;
                }
            }
        }
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                pref[i][j] += pref[i - 1][j] + pref[i][j - 1] - pref[i - 1][j - 1];
            }
        }
        // cout << mid << ' ' << cur << endl;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                int x = max(1LL, i - ch + 1);
                int y = max(1LL, j - ch + 1);
                int res = pref[i][j];
                res -= pref[x - 1][j];
                res -= pref[i][y - 1];
                res += pref[x - 1][y - 1];
                if (matr[i][j] && !res) {
                    cur = false;
                }
                if (!matr[i][j] && res) {
                    cur = false;
                }
            }
        }
        if (cur) {
            l = mid;
        } else {
            r = mid;
        }
    }
    int ch = 2 * l + 1;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                pref[i][j] = good[i][j] = 0;
            }
        }
        bool cur = true;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                if (sq[i][j] >= ch) {
                    int x = i - ch + 1;
                    int y = j - ch + 1;
                    if (x <= 0 || y <= 0) {
                        continue;
                    }
                    good[i - ch / 2][j - ch / 2] = 1;
                }
            }
        }
    cout << l << '\n';
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            if (good[i][j]) {
                cout << 'X';
            } else {
                cout << '.';
            }
        }
        cout << endl;
    }
    return 0;
}


// #pragma GCC optimize("Ofast,no-stack-protector")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC optimize("fast-math")
#include<bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

#define F first
#define S second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()

void accell() {
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(0);
}

signed main() {
    accell();
    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    int n, m;
    cin >> n >> m;
    int matr[n + 1][m + 1];
    int sq[n + 1][m + 1];
    for (int i = 0; i <= n; ++i) {
        for (int j = 0; j <= m; ++j) {
            matr[i][j] = sq[i][j] = 0;
        }
    }
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            char x;
            cin >> x;
            if (x == 'X')
                matr[i][j] = 1;
            else
                matr[i][j] = 0;
        }
    }
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            if (!matr[i][j])
                continue;
            int cur = sq[i - 1][j - 1];
            cur = min(cur, sq[i - 1][j]);
            cur = min(cur, sq[i][j - 1]);
            sq[i][j] = cur + 1;
        }
    }
    int pref[n + 1][m + 1];
    int good[n + 1][m + 1];
    for (int i = 0; i <= n; ++i) {
        for (int j = 0; j <= m; ++j) {
            pref[i][j] = good[i][j] = 0;
        }
    }
    int l = 0, r = min(n, m) / 2 + 1;
    while (r - l > 1) {
        int mid = l + r >> 1;
        int ch = 2 * mid + 1;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                pref[i][j] = good[i][j] = 0;
            }
        }
        bool cur = true;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                if (sq[i][j] >= ch) {
                    int x = i - ch + 1;
                    int y = j - ch + 1;
                    if (x <= 0 || y <= 0) {
                        continue;
                    }
                    pref[x][y] += 1;
                    good[i - mid][j - mid] = 1;
                }
            }
        }
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                pref[i][j] += pref[i - 1][j] + pref[i][j - 1] - pref[i - 1][j - 1];
            }
        }
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                int x = max(1, i - ch + 1);
                int y = max(1, j - ch + 1);
                int res = pref[i][j];
                res -= pref[x - 1][j];
                res -= pref[i][y - 1];
                res += pref[x - 1][y - 1];
                if (matr[i][j] && !res) {
                    cur = false;
                }
                if (!matr[i][j] && res) {
                    cur = false;
                }
            }
        }
        if (cur) {
            l = mid;
        } else {
            r = mid;
        }
    }
    int ch = 2 * l + 1;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                pref[i][j] = good[i][j] = 0;
            }
        }
        bool cur = true;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                if (sq[i][j] >= ch) {
                    int x = i - ch + 1;
                    int y = j - ch + 1;
                    if (x <= 0 || y <= 0) {
                        continue;
                    }
                    good[i - ch / 2][j - ch / 2] = 1;
                }
            }
        }
    cout << l << '\n';
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            if (good[i][j]) {
                cout << 'X';
            } else {
                cout << '.';
            }
        }
        cout << '\n';
    }
    return 0;
}


// #pragma GCC optimize("Ofast,no-stack-protector")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC optimize("fast-math")
#include<bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

#define F first
#define S second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()

void accell() {
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(0);
}

signed main() {
    accell();
    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    int n, m;
    cin >> n >> m;
    int matr[n + 1][m + 1];
    int sq[n + 1][m + 1];
    for (int i = 0; i <= n; ++i) {
        for (int j = 0; j <= m; ++j) {
            matr[i][j] = sq[i][j] = 0;
        }
    }
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            char x;
            cin >> x;
            if (x == 'X')
                matr[i][j] = 1;
            else
                matr[i][j] = 0;
        }
    }
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            if (!matr[i][j])
                continue;
            int cur = sq[i - 1][j - 1];
            cur = min(cur, sq[i - 1][j]);
            cur = min(cur, sq[i][j - 1]);
            sq[i][j] = cur + 1;
        }
    }
    int pref[n + 1][m + 1];
    int good[n + 1][m + 1];
    for (int i = 0; i <= n; ++i) {
        for (int j = 0; j <= m; ++j) {
            pref[i][j] = good[i][j] = 0;
        }
    }
    int l = -1, r = min(n, m) / 2 + 1;
    while (r - l > 1) {
        int mid = l + r >> 1;
        int ch = 2 * mid + 1;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                pref[i][j] = good[i][j] = 0;
            }
        }
        bool cur = true;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                if (sq[i][j] >= ch) {
                    int x = i - ch + 1;
                    int y = j - ch + 1;
                    if (x <= 0 || y <= 0) {
                        continue;
                    }
                    pref[x][y] += 1;
                    good[i - mid][j - mid] = 1;
                }
            }
        }
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                pref[i][j] += pref[i - 1][j] + pref[i][j - 1] - pref[i - 1][j - 1];
            }
        }
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                int x = max(1, i - ch + 1);
                int y = max(1, j - ch + 1);
                int res = pref[i][j];
                res -= pref[x - 1][j];
                res -= pref[i][y - 1];
                res += pref[x - 1][y - 1];
                if (matr[i][j] && !res) {
                    cur = false;
                }
                if (!matr[i][j] && res) {
                    cur = false;
                }
            }
        }
        if (cur) {
            l = mid;
        } else {
            r = mid;
        }
    }
    int ch = 2 * l + 1;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                pref[i][j] = good[i][j] = 0;
            }
        }
        bool cur = true;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                if (sq[i][j] >= ch) {
                    int x = i - ch + 1;
                    int y = j - ch + 1;
                    if (x <= 0 || y <= 0) {
                        continue;
                    }
                    good[i - ch / 2][j - ch / 2] = 1;
                }
            }
        }
    cout << l << '\n';
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            if (good[i][j]) {
                cout << 'X';
            } else {
                cout << '.';
            }
        }
        cout << endl;
    }
    return 0;
}


#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
#include<bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

#define F first
#define S second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define int long long

void accell() {
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(0);
}

signed main() {
    accell();
    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    int n, m;
    cin >> n >> m;
    vector<vector<int> >matr(n + 1, vector<int>(m + 1));
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            char x;
            cin >> x;
            if (x == 'X')
                matr[i][j] = 1;
            else
                matr[i][j] = 0;
        }
    }
    vector<vector<int> > sq(n + 1, vector<int>(m + 1));
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            if (!matr[i][j])
                continue;
            int cur = sq[i - 1][j - 1];
            cur = min(cur, sq[i - 1][j]);
            cur = min(cur, sq[i][j - 1]);
            sq[i][j] = cur + 1;
        }
    }
    vector<vector<int> >pref(n + 1, vector<int>(m + 1));
    vector<vector<int> >good(n + 1, vector<int>(m + 1));
    int l = -1, r = 2e6;
    while (r - l > 1) {
        int mid = l + r >> 1;
        int ch = 2 * mid + 1;
        // cout << l << ' ' << r << endl;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                pref[i][j] = good[i][j] = 0;
            }
        }
        bool cur = true;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                if (sq[i][j] >= ch) {
                    int x = i - ch + 1;
                    int y = j - ch + 1;
                    if (x <= 0 || y <= 0) {
                        continue;
                    }
                    pref[x][y] += 1;
                    good[i - ch / 2][j - ch / 2] = 1;
                    // if (mid == 1)
                    //    cout << i - ch / 2 << ' ' << j - ch / 2 << endl;
                }
            }
        }
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                pref[i][j] += pref[i - 1][j] + pref[i][j - 1] - pref[i - 1][j - 1];
            }
        }
        // cout << mid << ' ' << cur << endl;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                int x = max(1LL, i - ch + 1);
                int y = max(1LL, j - ch + 1);
                int res = pref[i][j];
                res -= pref[x - 1][j];
                res -= pref[i][y - 1];
                res += pref[x - 1][y - 1];
                if (matr[i][j] && !res) {
                    cur = false;
                }
                if (!matr[i][j] && res) {
                    cur = false;
                }
            }
        }
        if (cur) {
            l = mid;
        } else {
            r = mid;
        }
    }
    int ch = 2 * l + 1;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                pref[i][j] = good[i][j] = 0;
            }
        }
        bool cur = true;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                if (sq[i][j] >= ch) {
                    int x = i - ch + 1;
                    int y = j - ch + 1;
                    if (x <= 0 || y <= 0) {
                        continue;
                    }
                    good[i - ch / 2][j - ch / 2] = 1;
                }
            }
        }
    cout << l << '\n';
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            if (good[i][j]) {
                cout << 'X';
            } else {
                cout << '.';
            }
        }
        cout << endl;
    }
    return 0;
}


// #pragma GCC optimize("Ofast,no-stack-protector")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC optimize("fast-math")
#include<bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

#define F first
#define S second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()

void accell() {
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(0);
}

signed main() {
    accell();
    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    int n, m;
    cin >> n >> m;
    int matr[n + 1][m + 1];
    int sq[n + 1][m + 1];
    for (int i = 0; i <= n; ++i) {
        for (int j = 0; j <= m; ++j) {
            matr[i][j] = sq[i][j] = 0;
        }
    }
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            char x;
            cin >> x;
            if (x == 'X')
                matr[i][j] = 1;
            else
                matr[i][j] = 0;
        }
    }
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            if (!matr[i][j])
                continue;
            int cur = sq[i - 1][j - 1];
            cur = min(cur, sq[i - 1][j]);
            cur = min(cur, sq[i][j - 1]);
            sq[i][j] = cur + 1;
        }
    }
    int pref[n + 1][m + 1];
    int good[n + 1][m + 1];
    for (int i = 0; i <= n; ++i) {
        for (int j = 0; j <= m; ++j) {
            pref[i][j] = good[i][j] = 0;
        }
    }
    int l = 0, r = min(n, m) / 2 + 1;
    while (r - l > 1) {
        int mid = l + r >> 1;
        int ch = 2 * mid + 1;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                pref[i][j] = good[i][j] = 0;
            }
        }
        bool cur = true;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                if (sq[i][j] >= ch) {
                    int x = i - ch + 1;
                    int y = j - ch + 1;
                    if (x <= 0 || y <= 0) {
                        continue;
                    }
                    pref[x][y] += 1;
                    good[i - mid][j - mid] = 1;
                }
            }
        }
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                pref[i][j] += pref[i - 1][j] + pref[i][j - 1] - pref[i - 1][j - 1];
            }
        }
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                int x = max(1, i - ch + 1);
                int y = max(1, j - ch + 1);
                int res = pref[i][j];
                res -= pref[x - 1][j];
                res -= pref[i][y - 1];
                res += pref[x - 1][y - 1];
                if (matr[i][j] && !res) {
                    cur = false;
                }
                if (!matr[i][j] && res) {
                    cur = false;
                }
            }
        }
        if (cur) {
            l = mid;
        } else {
            r = mid;
        }
    }
    int ch = 2 * l + 1;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                pref[i][j] = good[i][j] = 0;
            }
        }
        bool cur = true;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                if (sq[i][j] >= ch) {
                    int x = i - ch + 1;
                    int y = j - ch + 1;
                    if (x <= 0 || y <= 0) {
                        continue;
                    }
                    good[i - ch / 2][j - ch / 2] = 1;
                }
            }
        }
    cout << l << '\n';
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            if (good[i][j]) {
                cout << 'X';
            } else {
                cout << '.';
            }
        }
        cout << endl;
    }
    return 0;
}


#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
#include<bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

#define F first
#define S second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()

void accell() {
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(0);
}

signed main() {
    accell();
    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    int n, m;
    cin >> n >> m;
    vector<vector<int> >matr(n + 1, vector<int>(m + 1));
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            char x;
            cin >> x;
            if (x == 'X')
                matr[i][j] = 1;
            else
                matr[i][j] = 0;
        }
    }
    vector<vector<int> > sq(n + 1, vector<int>(m + 1));
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            if (!matr[i][j])
                continue;
            int cur = sq[i - 1][j - 1];
            cur = min(cur, sq[i - 1][j]);
            cur = min(cur, sq[i][j - 1]);
            sq[i][j] = cur + 1;
        }
    }
    vector<vector<int> >pref(n + 1, vector<int>(m + 1));
    vector<vector<int> >good(n + 1, vector<int>(m + 1));
    int l = -1, r = 500000;
    while (r - l > 1) {
        int mid = l + r >> 1;
        int ch = 2 * mid + 1;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                pref[i][j] = good[i][j] = 0;
            }
        }
        bool cur = true;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                if (sq[i][j] >= ch) {
                    int x = i - ch + 1;
                    int y = j - ch + 1;
                    if (x <= 0 || y <= 0) {
                        continue;
                    }
                    pref[x][y] += 1;
                    good[i - ch / 2][j - ch / 2] = 1;
                    // if (mid == 1)
                    //    cout << i - ch / 2 << ' ' << j - ch / 2 << endl;
                }
            }
        }
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                pref[i][j] += pref[i - 1][j] + pref[i][j - 1] - pref[i - 1][j - 1];
            }
        }
        // cout << mid << ' ' << cur << endl;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                int x = max(1, i - ch + 1);
                int y = max(1, j - ch + 1);
                int res = pref[i][j];
                res -= pref[x - 1][j];
                res -= pref[i][y - 1];
                res += pref[x - 1][y - 1];
                if (matr[i][j] && !res) {
                    cur = false;
                }
                if (!matr[i][j] && res) {
                    cur = false;
                }
            }
        }
        if (cur) {
            l = mid;
        } else {
            r = mid;
        }
    }
    int ch = 2 * l + 1;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                pref[i][j] = good[i][j] = 0;
            }
        }
        bool cur = true;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                if (sq[i][j] >= ch) {
                    int x = i - ch + 1;
                    int y = j - ch + 1;
                    if (x <= 0 || y <= 0) {
                        continue;
                    }
                    good[i - ch / 2][j - ch / 2] = 1;
                }
            }
        }
    cout << l << '\n';
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            if (good[i][j]) {
                cout << 'X';
            } else {
                cout << '.';
            }
        }
        cout << endl;
    }
    return 0;
}


#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
#include<bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

#define F first
#define S second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()

void accell() {
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(0);
}

signed main() {
    accell();
    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    int n, m;
    cin >> n >> m;
    vector<vector<int> >matr(n + 1, vector<int>(m + 1));
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            char x;
            cin >> x;
            if (x == 'X')
                matr[i][j] = 1;
            else
                matr[i][j] = 0;
        }
    }
    vector<vector<int> > sq(n + 1, vector<int>(m + 1));
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            if (!matr[i][j])
                continue;
            int cur = sq[i - 1][j - 1];
            cur = min(cur, sq[i - 1][j]);
            cur = min(cur, sq[i][j - 1]);
            sq[i][j] = cur + 1;
        }
    }
    vector<vector<int> >pref(n + 1, vector<int>(m + 1));
    vector<vector<int> >good(n + 1, vector<int>(m + 1));
    int l = -1, r = 2e6;
    while (r - l > 1) {
        int mid = l + r >> 1;
        int ch = 2 * mid + 1;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                pref[i][j] = good[i][j] = 0;
            }
        }
        bool cur = true;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                if (sq[i][j] >= ch) {
                    int x = i - ch + 1;
                    int y = j - ch + 1;
                    if (x <= 0 || y <= 0) {
                        continue;
                    }
                    pref[x][y] += 1;
                    good[i - ch / 2][j - ch / 2] = 1;
                    // if (mid == 1)
                    //    cout << i - ch / 2 << ' ' << j - ch / 2 << endl;
                }
            }
        }
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                pref[i][j] += pref[i - 1][j] + pref[i][j - 1] - pref[i - 1][j - 1];
            }
        }
        // cout << mid << ' ' << cur << endl;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                int x = max(1, i - ch + 1);
                int y = max(1, j - ch + 1);
                int res = pref[i][j];
                res -= pref[x - 1][j];
                res -= pref[i][y - 1];
                res += pref[x - 1][y - 1];
                if (matr[i][j] && !res) {
                    cur = false;
                }
                if (!matr[i][j] && res) {
                    cur = false;
                }
            }
        }
        if (cur) {
            l = mid;
        } else {
            r = mid;
        }
    }
    int ch = 2 * l + 1;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                pref[i][j] = good[i][j] = 0;
            }
        }
        bool cur = true;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                if (sq[i][j] >= ch) {
                    int x = i - ch + 1;
                    int y = j - ch + 1;
                    if (x <= 0 || y <= 0) {
                        continue;
                    }
                    good[i - ch / 2][j - ch / 2] = 1;
                }
            }
        }
    cout << l << '\n';
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            if (good[i][j]) {
                cout << 'X';
            } else {
                cout << '.';
            }
        }
        cout << endl;
    }
    return 0;
}


#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
#include<bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

#define F first
#define S second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()

void accell() {
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(0);
}

signed main() {
    accell();
    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    int n, m;
    cin >> n >> m;
    int matr[n + 1][m + 1];
    int sq[n + 1][m + 1];
    for (int i = 0; i <= n; ++i) {
        for (int j = 0; j <= m; ++j) {
            matr[i][j] = sq[i][j] = 0;
        }
    }
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            char x;
            cin >> x;
            if (x == 'X')
                matr[i][j] = 1;
            else
                matr[i][j] = 0;
        }
    }
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            if (!matr[i][j])
                continue;
            int cur = sq[i - 1][j - 1];
            cur = min(cur, sq[i - 1][j]);
            cur = min(cur, sq[i][j - 1]);
            sq[i][j] = cur + 1;
        }
    }
    int pref[n + 1][m + 1];
    int good[n + 1][m + 1];
    for (int i = 0; i <= n; ++i) {
        for (int j = 0; j <= m; ++j) {
            pref[i][j] = good[i][j] = 0;
        }
    }
    int l = -1, r = min(n, m) / 2 + 1;
    while (r - l > 1) {
        int mid = l + r >> 1;
        int ch = 2 * mid + 1;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                pref[i][j] = good[i][j] = 0;
            }
        }
        bool cur = true;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                if (sq[i][j] >= ch) {
                    int x = i - ch + 1;
                    int y = j - ch + 1;
                    if (x <= 0 || y <= 0) {
                        continue;
                    }
                    pref[x][y] += 1;
                    good[i - ch / 2][j - ch / 2] = 1;
                }
            }
        }
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                pref[i][j] += pref[i - 1][j] + pref[i][j - 1] - pref[i - 1][j - 1];
            }
        }
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                int x = max(1, i - ch + 1);
                int y = max(1, j - ch + 1);
                int res = pref[i][j];
                res -= pref[x - 1][j];
                res -= pref[i][y - 1];
                res += pref[x - 1][y - 1];
                if (matr[i][j] && !res) {
                    cur = false;
                }
                if (!matr[i][j] && res) {
                    cur = false;
                }
            }
        }
        if (cur) {
            l = mid;
        } else {
            r = mid;
        }
    }
    int ch = 2 * l + 1;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                pref[i][j] = good[i][j] = 0;
            }
        }
        bool cur = true;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                if (sq[i][j] >= ch) {
                    int x = i - ch + 1;
                    int y = j - ch + 1;
                    if (x <= 0 || y <= 0) {
                        continue;
                    }
                    good[i - ch / 2][j - ch / 2] = 1;
                }
            }
        }
    cout << l << '\n';
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            if (good[i][j]) {
                cout << 'X';
            } else {
                cout << '.';
            }
        }
        cout << endl;
    }
    return 0;
}


#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
#include<bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

#define F first
#define S second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()

void accell() {
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(0);
}

signed main() {
    accell();
    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    int n, m;
    cin >> n >> m;
    int matr[n + 1][m + 1];
    int sq[n + 1][m + 1];
    for (int i = 0; i <= n; ++i) {
        for (int j = 0; j <= m; ++j) {
            matr[i][j] = sq[i][j] = 0;
        }
    }
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            char x;
            cin >> x;
            if (x == 'X')
                matr[i][j] = 1;
            else
                matr[i][j] = 0;
        }
    }
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            if (!matr[i][j])
                continue;
            int cur = sq[i - 1][j - 1];
            cur = min(cur, sq[i - 1][j]);
            cur = min(cur, sq[i][j - 1]);
            sq[i][j] = cur + 1;
        }
    }
    int pref[n + 1][m + 1];
    int good[n + 1][m + 1];
    for (int i = 0; i <= n; ++i) {
        for (int j = 0; j <= m; ++j) {
            pref[i][j] = good[i][j] = 0;
        }
    }
    int l = -1, r = 500000;
    while (r - l > 1) {
        int mid = l + r >> 1;
        int ch = 2 * mid + 1;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                pref[i][j] = good[i][j] = 0;
            }
        }
        bool cur = true;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                if (sq[i][j] >= ch) {
                    int x = i - ch + 1;
                    int y = j - ch + 1;
                    if (x <= 0 || y <= 0) {
                        continue;
                    }
                    pref[x][y] += 1;
                    good[i - ch / 2][j - ch / 2] = 1;
                }
            }
        }
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                pref[i][j] += pref[i - 1][j] + pref[i][j - 1] - pref[i - 1][j - 1];
            }
        }
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                int x = max(1, i - ch + 1);
                int y = max(1, j - ch + 1);
                int res = pref[i][j];
                res -= pref[x - 1][j];
                res -= pref[i][y - 1];
                res += pref[x - 1][y - 1];
                if (matr[i][j] && !res) {
                    cur = false;
                }
                if (!matr[i][j] && res) {
                    cur = false;
                }
            }
        }
        if (cur) {
            l = mid;
        } else {
            r = mid;
        }
    }
    int ch = 2 * l + 1;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                pref[i][j] = good[i][j] = 0;
            }
        }
        bool cur = true;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                if (sq[i][j] >= ch) {
                    int x = i - ch + 1;
                    int y = j - ch + 1;
                    if (x <= 0 || y <= 0) {
                        continue;
                    }
                    good[i - ch / 2][j - ch / 2] = 1;
                }
            }
        }
    cout << l << '\n';
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            if (good[i][j]) {
                cout << 'X';
            } else {
                cout << '.';
            }
        }
        cout << endl;
    }
    return 0;
}