#include <bits/stdc++.h>
using namespace std;
const int M = 1000005;
int read() {
  int x = 0, f = 1;
  char c;
  while ((c = getchar()) < '0' || c > '9') {
    if (c == '-') f = -1;
  }
  while (c >= '0' && c <= '9') {
    x = (x << 3) + (x << 1) + (c ^ 48);
    c = getchar();
  }
  return x * f;
}
int n, m, ans, c1, c2;
char s[M];
int dx[8] = {-1, 1, 0, 0, 1, -1, 1, -1}, dy[8] = {0, 0, -1, 1, 1, -1, -1, 1};
vector<int> f[M], a[M];
struct node {
  int x, y, d;
};
bool check(int x) {
  queue<node> q;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      f[i][j] = 0;
      if (i - x >= 1 && j - x >= 1 && i + x <= n && j + x <= m) {
        int t = a[i + x][j + x] + a[i - x - 1][j - x - 1] -
                a[i - x - 1][j + x] - a[i + x][j - x - 1];
        if (t == (2 * x + 1) * (2 * x + 1)) {
          f[i][j] = 1;
          q.push(node{i, j, 0});
        }
      }
    }
  c2 = 0;
  while (!q.empty()) {
    node t = q.front();
    c2++;
    q.pop();
    if (t.d == x) continue;
    for (int i = 0; i < 8; i++) {
      int tx = t.x + dx[i], ty = t.y + dy[i];
      if (tx >= 1 && tx <= n && ty >= 1 && ty <= m && !f[tx][ty]) {
        f[tx][ty] = 1;
        q.push(node{tx, ty, t.d + 1});
      }
    }
  }
  return c1 == c2;
}
void dich(int l, int r) {
  if (l > r) return;
  int mid = (l + r) >> 1;
  if (check(mid)) {
    ans = mid;
    dich(mid + 1, r);
  } else
    dich(l, mid - 1);
}
signed main() {
  n = read();
  m = read();
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= m; j++) {
      f[i].push_back(0);
      a[i].push_back(0);
    }
  for (int i = 1; i <= n; i++) {
    scanf("%s", s + 1);
    for (int j = 1; j <= m; j++) {
      if (s[j] == 'X') c1++;
      a[i][j] = (s[j] == 'X' ? 1 : 0);
    }
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
  dich(0, max(n, m));
  printf("%d\n", ans);
  for (int i = 1; i <= n; i++, puts(""))
    for (int j = 1, x = ans; j <= m; j++)
      if (i - x >= 1 && j - x >= 1 && i + x <= n && j + x <= m) {
        int t = a[i + x][j + x] + a[i - x - 1][j - x - 1] -
                a[i - x - 1][j + x] - a[i + x][j - x - 1];
        if (t == (2 * x + 1) * (2 * x + 1))
          printf("X");
        else
          printf(".");
      } else
        printf(".");
}
