#include <bits/stdc++.h>
using namespace std;
const int maxn = 5000005;
char p[maxn];
int a[maxn], b[maxn], c[maxn];
int n, m;
int ad(int i, int j) { return (i - 1) * m + j; }
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%s", p + ad(i, 1));
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      if (p[ad(i, j)] == '.')
        a[ad(i, j)] = 0;
      else if (i == 1 || j == 1)
        a[ad(i, j)] = 1;
      else
        a[ad(i, j)] =
            min(a[ad(i - 1, j - 1)], min(a[ad(i - 1, j)], a[ad(i, j - 1)])) + 1;
    }
  int l = 0, r = 1000000;
  int mid, x;
  while (r > l + 1) {
    mid = (l + r) >> 1;
    x = 2 * mid + 1;
    bool flag = true;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++)
        if (a[ad(i, j)] >= x)
          c[ad(i, j)] = x;
        else
          c[ad(i, j)] = 0;
    for (int i = n; i >= 1; i--)
      for (int j = m; j >= 1; j--) {
        if (i > 1) c[ad(i - 1, j)] = max(c[ad(i - 1, j)], c[ad(i, j)] - 1);
        if (j > 1) c[ad(i, j - 1)] = max(c[ad(i, j - 1)], c[ad(i, j)] - 1);
        if (i > 1 && j > 1)
          c[ad(i - 1, j - 1)] = max(c[ad(i - 1, j - 1)], c[ad(i, j)] - 1);
      }
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++) {
        if (a[ad(i, j)] == 0) continue;
        if (c[ad(i, j)] == 0) flag = false;
      }
    if (flag)
      l = mid;
    else
      r = mid;
  }
  printf("%d\n", l);
  x = 2 * l + 1;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) b[ad(i, j)] = '.';
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (a[ad(i, j)] >= x) b[ad(i - l, j - l)] = 'X';
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) printf("%c", b[ad(i, j)]);
    puts("");
  }
  return 0;
}
