#include <bits/stdc++.h>
using namespace std;
const int mxn = 5000010;
int n, m, sum, a[mxn], b[mxn], g[mxn], q[mxn];
char s[mxn];
int f(int x, int y) { return x * (m + 3) + y; }
bool check(int k) {
  memset(g, 0, sizeof(g));
  int hd = 0, tl = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      if (b[f(i, j)] >= k) g[f(i, j)] = 1, q[++tl] = f(i, j);
  for (int i = 1; i < k; ++i) {
    int p = tl;
    for (; hd < p;) {
      int x = q[++hd];
      if (!g[x - 1]) g[x - 1] = 1, q[++tl] = x - 1;
      if (!g[x + 1]) g[x + 1] = 1, q[++tl] = x + 1;
      if (!g[x + m + 3]) g[x + m + 3] = 1, q[++tl] = x + m + 3;
      if (!g[x - m - 3]) g[x - m - 3] = 1, q[++tl] = x - m - 3;
      if (!g[x + m + 2]) g[x + m + 2] = 1, q[++tl] = x + m + 2;
      if (!g[x + m + 4]) g[x + m + 4] = 1, q[++tl] = x + m + 4;
      if (!g[x - m - 4]) g[x - m - 4] = 1, q[++tl] = x - m - 4;
      if (!g[x - m - 2]) g[x - m - 2] = 1, q[++tl] = x - m - 2;
    }
  }
  return tl == sum;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    scanf("%s", s + 1);
    for (int j = 1; j <= m; ++j) a[f(i, j)] = s[j] == 'X', sum += a[f(i, j)];
  }
  int hd = 0, tl = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      if (a[f(i, j)]) {
        int flg = 0;
        if (!a[f(i - 1, j)]) flg = 1;
        if (!a[f(i + 1, j)]) flg = 1;
        if (!a[f(i, j - 1)]) flg = 1;
        if (!a[f(i, j + 1)]) flg = 1;
        if (!a[f(i - 1, j - 1)]) flg = 1;
        if (!a[f(i + 1, j - 1)]) flg = 1;
        if (!a[f(i - 1, j + 1)]) flg = 1;
        if (!a[f(i + 1, j + 1)]) flg = 1;
        if (flg) q[++tl] = f(i, j), b[f(i, j)] = 1;
      }
  for (; hd < tl;) {
    int x = q[++hd];
    if (a[x - 1] && !b[x - 1]) b[x - 1] = b[x] + 1, q[++tl] = x - 1;
    if (a[x + 1] && !b[x + 1]) b[x + 1] = b[x] + 1, q[++tl] = x + 1;
    if (a[x + m + 3] && !b[x + m + 3])
      b[x + m + 3] = b[x] + 1, q[++tl] = x + m + 3;
    if (a[x - m - 3] && !b[x - m - 3])
      b[x - m - 3] = b[x] + 1, q[++tl] = x - m - 3;
    if (a[x + m + 2] && !b[x + m + 2])
      b[x + m + 2] = b[x] + 1, q[++tl] = x + m + 2;
    if (a[x + m + 4] && !b[x + m + 4])
      b[x + m + 4] = b[x] + 1, q[++tl] = x + m + 4;
    if (a[x - m - 4] && !b[x - m - 4])
      b[x - m - 4] = b[x] + 1, q[++tl] = x - m - 4;
    if (a[x - m - 2] && !b[x - m - 2])
      b[x - m - 2] = b[x] + 1, q[++tl] = x - m - 2;
  }
  int l = 1, r = max(n, m);
  for (; l <= r;)
    if (check(((l + r) >> 1)))
      l = ((l + r) >> 1) + 1;
    else
      r = ((l + r) >> 1) - 1;
  printf("%d\n", r - 1);
  for (int i = 1; i <= n; puts(""), ++i)
    for (int j = 1; j <= m; ++j)
      if (b[f(i, j)] >= r)
        putchar('X');
      else
        putchar('.');
  return 0;
}
