#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e6 + 10;
const int MOD = 998244353;
int s[maxn], R[maxn];
char ss[maxn];
int n, m;
int mp(int i, int j) { return i * (m + 2) + j; }
int Q(int x1, int y1, int x2, int y2) {
  x1--;
  y1--;
  return s[mp(x2, y2)] - s[mp(x1, y2)] - s[mp(x2, y1)] + s[mp(x1, y1)];
}
void U(int x1, int y1, int x2, int y2) {
  x2++;
  y2++;
  s[mp(x1, y1)]++;
  s[mp(x1, y2)]--;
  s[mp(x2, y1)]--;
  s[mp(x2, y2)]++;
}
int check(int t) {
  memset(s, 0, sizeof(s));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (R[mp(i, j)] >= t && ss[mp(i, j)] == 'X') {
        U(i - t, j - t, i + t, j + t);
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      s[mp(i, j)] += s[mp(i - 1, j)] + s[mp(i, j - 1)] - s[mp(i - 1, j - 1)];
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (ss[mp(i, j)] == 'X' && s[mp(i, j)] == 0) return 0;
    }
  }
  return 1;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%s", ss + mp(i, 1));
    for (int j = 1; j <= m; j++) {
      s[mp(i, j)] = s[mp(i - 1, j)] + s[mp(i, j - 1)] - s[mp(i - 1, j - 1)] +
                    (ss[mp(i, j)] == 'X');
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (ss[mp(i, j)] == 'X') {
        int l = 0, r = min(min(i, n - i), min(j, m - j)), tmp;
        while (l <= r) {
          int mid = l + r >> l;
          long long S = (long long)(1 + 2 * mid) * (1 + 2 * mid);
          if (Q(i - mid, j - mid, i + mid, j + mid) == S) {
            tmp = mid;
            l = mid + 1;
          } else
            r = mid - 1;
        }
        R[mp(i, j)] = tmp;
      }
    }
  }
  int l = 0, r = 1e6;
  int ans;
  while (l <= r) {
    int mid = l + r >> 1;
    if (check(mid)) {
      ans = mid;
      l = mid + 1;
    } else
      r = mid - 1;
  }
  printf("%d\n", ans);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (ss[mp(i, j)] == 'X' && R[mp(i, j)] >= ans)
        printf("X");
      else
        printf(".");
    }
    printf("\n");
  }
  return 0;
}
