#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 100;
vector<int> g[N], mp[N], c[N], ans[N];
char s[N];
int n, m;
bool flg;
bool check(int a, int b, int x, int y) {
  if (a < 1 || b < 1 || x > n || y > m) return false;
  return g[x][y] + g[a - 1][b - 1] - g[a - 1][y] - g[x][b - 1] ==
         (x - a + 1) * (y - b + 1);
}
bool solve(int t) {
  for (int i = 0; i <= n + 1; ++i)
    for (int j = 0; j <= m + 1; ++j) c[i][j] = 0;
  for (int i = 1 + t; i <= n - t; ++i) {
    for (int j = 1 + t; j <= m - t; ++j) {
      if (mp[i][j] && check(i - t, j - t, i + t, j + t)) {
        c[i - t][j - t]++;
        c[i - t][j + t + 1]--;
        c[i + t + 1][j - t]--;
        c[i + t + 1][j + t + 1]++;
      }
    }
  }
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) c[i][j] += c[i][j - 1];
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) c[i][j] += c[i - 1][j];
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      if ((c[i][j] > 0) != (mp[i][j] > 0)) return false;
  return true;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i <= n + 1; ++i) g[i].resize(m + 2);
  for (int i = 0; i <= n + 1; ++i) mp[i].resize(m + 2);
  for (int i = 0; i <= n + 1; ++i) c[i].resize(m + 2);
  for (int i = 1; i <= n; ++i) {
    scanf("%s", s + 1);
    for (int j = 1; j <= m; ++j) g[i][j] = mp[i][j] = s[j] == 'X' ? 1 : 0;
  }
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      g[i][j] += g[i][j - 1] + g[i - 1][j] - g[i - 1][j - 1];
  int l = 0, r = min(n / 2, m / 2) + 1;
  while (l + 1 < r) {
    int mid = (l + r) >> 1;
    if (solve(mid))
      l = mid;
    else
      r = mid;
  }
  printf("%d\n", l);
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j)
      s[j] = mp[i][j] && check(i - l, j - l, i + l, j + l) ? 'X' : '.';
    printf("%s\n", s + 1);
  }
}
