#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1100000;
int n, m;
char a[MAXN], b[MAXN];
int s[MAXN], t[MAXN];
inline int id(int x, int y) {
  if (x < 1 || x > n || y < 1 || y > m) return 0;
  return (x - 1) * m + y;
}
bool check(int k) {
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++)
      if (i - k >= 1 && i + k <= n && j - k >= 1 && j + k <= m &&
          s[id(i + k, j + k)] - s[id(i - k - 1, j + k)] -
                  s[id(i + k, j - k - 1)] + s[id(i - k - 1, j - k - 1)] ==
              0)
        b[id(i, j)] = 'X';
      else
        b[id(i, j)] = '.';
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      t[id(i, j)] = t[id(i - 1, j)] + t[id(i, j - 1)] - t[id(i - 1, j - 1)] +
                    (b[id(i, j)] == 'X');
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      int l1 = max(i - k, 1), r1 = min(i + k, n);
      int l2 = max(j - k, 1), r2 = min(j + k, m);
      if (a[id(i, j)] == 'X' && t[id(r1, r2)] - t[id(l1 - 1, r2)] -
                                        t[id(r1, l2 - 1)] +
                                        t[id(l1 - 1, l2 - 1)] ==
                                    0)
        return 0;
    }
  return 1;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%s", a + (i - 1) * m + 1);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      s[id(i, j)] = s[id(i - 1, j)] + s[id(i, j - 1)] - s[id(i - 1, j - 1)] +
                    (a[id(i, j)] != 'X');
  int l = 0, r = min(n, m);
  while (l < r) {
    int mid = l + r + 1 >> 1;
    if (check(mid))
      l = mid;
    else
      r = mid - 1;
  }
  printf("%d\n", l);
  check(l);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) putchar(b[id(i, j)]);
    putchar('\n');
  }
  return 0;
}
