#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0, f = 1;
  char s = getchar();
  while (s < '0' || s > '9') {
    if (s == '-') f *= -1;
    s = getchar();
  }
  while (s >= '0' && s <= '9') {
    x = x * 10 + s - '0';
    s = getchar();
  }
  return x * f;
}
inline char gchar() {
  char s = getchar();
  while (s != 'X' && s != '.') s = getchar();
  return s;
}
int n = read(), m = read(), d[8][2] = {{0, -1}, {-1, -1}, {-1, 0}, {-1, 1},
                                       {0, 1},  {1, 1},   {1, 0},  {1, -1}};
int main() {
  bool p[n + 5][m + 5], h[n + 5][m + 5], l[n + 5][m + 5], e[n + 5][m + 5],
      f[n + 5][m + 5];
  int v[n + 5][m + 5];
  memset(p, 0, sizeof(p));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (gchar() == 'X') p[i][j] = 1;
      h[i][j] = l[i][j] = e[i][j] = f[i][j] = 0;
    }
  }
  int s = min(n, m);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (p[i][j] && !h[i][j]) {
        if (!p[i][j - 1]) {
          int len;
          for (len = 1; j + len - 1 < m; len++) {
            h[i][j + len - 1] = 1;
            if (!p[i][j + len]) break;
          }
          s = min(len, s);
          j += len;
        }
      }
    }
  }
  for (int j = 1; j <= m; j++) {
    for (int i = 1; i <= n; i++) {
      if (p[i][j] && !l[i][j]) {
        if (!p[i - 1][j]) {
          int len;
          for (len = 1; i + len - 1 < n; len++) {
            l[i + len - 1][j] = 1;
            if (!p[i + len][j]) break;
          }
          s = min(len, s);
          i += len;
        }
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (p[i][j] && !e[i][j]) {
        if (!p[i - 1][j - 1] && !p[i][j - 1] && !p[i - 1][j]) {
          int len;
          for (len = 1; j + len - 1 < m && i + len - 1 < n; len++) {
            e[i + len - 1][j + len - 1] = 1;
            if (!p[i + len][j + len]) break;
          }
          s = min(len, s);
        }
        if (!p[i + 1][j + 1] && !p[i + 1][j] && !p[i][j + 1]) {
          int len;
          for (len = 1; j - len > 0 && i - len > 0; len++) {
            e[i - len + 1][j - len + 1] = 1;
            if (!p[i - len][j - len]) break;
          }
          s = min(len, s);
        }
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = m; j > 0; j--) {
      if (p[i][j] && !f[i][j]) {
        if (!p[i - 1][j + 1] && !p[i][j + 1] && !p[i - 1][j]) {
          int len;
          for (len = 1; j - len > 0 && i + len - 1 < n; len++) {
            f[i + len - 1][j - len + 1] = 1;
            if (!p[i + len][j - len]) break;
          }
          s = min(len, s);
        }
        if (!p[i + 1][j - 1] && !p[i][j - 1] && !p[i + 1][j]) {
          int len;
          for (len = 1; j + len - 1 < m && i - len > 0; len++) {
            f[i - len + 1][j + len - 1] = 1;
            if (!p[i - len][j + len]) break;
          }
          s = min(len, s);
        }
      }
    }
  }
  int t = (s - 1) >> 1;
  printf("%d\n", t);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (p[i][j]) v[i][j] = max(n, m);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      if (p[i][j]) {
        for (int k = 0; k < 4; k++) {
          if (!p[i + d[k][0]][j + d[k][1]])
            v[i][j] = min(v[i][j], 1);
          else
            v[i][j] = min(v[i][j], v[i + d[k][0]][j + d[k][1]] + 1);
        }
      }
    }
  for (int i = n; i > 0; i--)
    for (int j = m; j > 0; j--) {
      if (p[i][j]) {
        for (int k = 4; k < 8; k++) {
          if (!p[i + d[k][0]][j + d[k][1]])
            v[i][j] = min(v[i][j], 1);
          else
            v[i][j] = min(v[i][j], v[i + d[k][0]][j + d[k][1]] + 1);
        }
      }
    }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (!p[i][j])
        putchar('.');
      else if (v[i][j] <= t)
        putchar('.');
      else
        putchar('X');
    }
    putchar('\n');
  }
  return 0;
}
