#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<vector<int> > arr;
vector<vector<int> > sum;
vector<vector<int> > chk;
vector<vector<int> > chk2;
vector<vector<int> > aa;
int getSum(int x1, int y1, int x2, int y2) {
  return sum[x2][y2] - sum[x2][y1 - 1] - sum[x1 - 1][y2] + sum[x1 - 1][y1 - 1];
}
bool able(int g) {
  for (int i = 0; i <= n + 1; i++) {
    fill(chk[i].begin(), chk[i].end(), 0);
    fill(chk2[i].begin(), chk2[i].end(), 0);
  }
  for (int i = g + 1; i + g <= n; i++) {
    for (int j = g + 1; j + g <= m; j++) {
      if (getSum(i - g, j - g, i + g, j + g) == (2 * g + 1) * (2 * g + 1)) {
        chk[i + g + 1][j + g + 1] += 1, chk[i - g][j - g] += 1;
        chk[i + g + 1][j - g] += -1, chk[i - g][j + g + 1] += -1;
        chk2[i][j] = 1;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      chk[i][j] += chk[i - 1][j] + chk[i][j - 1] - chk[i - 1][j - 1];
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if ((!!chk[i][j]) ^ (!!arr[i][j])) return false;
    }
  }
  return true;
}
int main() {
  scanf("%d %d", &n, &m);
  arr.resize(n + 2);
  sum.resize(n + 2);
  chk.resize(n + 2);
  chk2.resize(n + 2);
  for (int i = 0; i <= n + 1; i++) {
    arr[i].resize(m + 2);
    sum[i].resize(m + 2);
    chk[i].resize(m + 2);
    chk2[i].resize(m + 2);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      char c;
      scanf(" %c", &c);
      arr[i][j] = (c == 'X');
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + arr[i][j];
    }
  }
  int l = 0, r = (min(n, m) - 1) / 2, ans = -1;
  while (l <= r) {
    int m = (l + r) >> 1;
    if (able(m)) {
      aa = chk2;
      ans = m;
      l = m + 1;
    } else {
      r = m - 1;
    }
  }
  printf("%d\n", ans);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      printf("%c", aa[i][j] ? 'X' : '.');
    }
    puts("");
  }
}
