#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 7;
const int INF = 1e9 + 7;
int xx[8] = {1, 1, 1, 0, -1, -1, -1, 0}, yy[8] = {1, 0, -1, -1, -1, 0, 1, 1};
int n, m, hd = 0, ti = 0;
vector<int> a[N], p[N], dis[N];
char s[N];
int Qux[N * 5], Quy[N * 5];
void bfs() {
  while (hd != ti) {
    int tx = Qux[++hd], ty = Quy[hd];
    for (int j = 0; j <= 8 - 1; j++) {
      if (tx + xx[j] < 0 || tx + xx[j] > n + 1) continue;
      if (ty + yy[j] < 0 || ty + yy[j] > m + 1) continue;
      if (dis[tx + xx[j]][ty + yy[j]] > dis[tx][ty] + 1) {
        dis[tx + xx[j]][ty + yy[j]] = dis[tx][ty] + 1;
        Qux[++ti] = tx + xx[j];
        Quy[ti] = ty + yy[j];
      }
    }
  }
}
bool check(int x) {
  for (int i = 0; i <= n + 1 - 1; i++)
    for (int j = 0; j <= m + 1 - 1; j++) p[i][j] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (dis[i][j] >= x) {
        if (i - x + 1 <= 0) return 0;
        if (i + x - 1 > n) return 0;
        if (j - x + 1 <= 0) return 0;
        if (j + x - 1 > m) return 0;
        p[i - x + 1][j - x + 1]++;
        p[i - x + 1][j + x]--;
        p[i + x][j - x + 1]--;
        p[i + x][j + x]++;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      p[i][j] = p[i][j] + p[i - 1][j] + p[i][j - 1] - p[i - 1][j - 1];
      if (!p[i][j] && a[i][j]) return 0;
    }
  }
  return 1;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    Qux[++ti] = 0;
    Quy[ti] = i;
    Qux[++ti] = n + 1;
    Quy[ti] = i;
  }
  for (int i = 0; i <= n + 2 - 1; i++)
    for (int j = 0; j <= m + 2 - 1; j++) dis[i].push_back(0);
  for (int i = 0; i <= n + 2 - 1; i++)
    for (int j = 0; j <= m + 2 - 1; j++) p[i].push_back(0);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s + 1);
    a[i].push_back(0);
    Qux[++ti] = i;
    Quy[ti] = 0;
    Qux[++ti] = i;
    Quy[ti] = m + 1;
    for (int j = 1; j <= m; j++) {
      if (s[j] == 'X') {
        a[i].push_back(1);
        dis[i][j] = INF;
      } else {
        a[i].push_back(0);
        Qux[++ti] = i;
        Quy[ti] = j;
      }
    }
  }
  bfs();
  int l = 1, r = (min(n, m) + 1) / 2;
  while (l < r - 1) {
    int mid = (l + r) >> 1;
    if (check(mid))
      l = mid;
    else
      r = mid;
  }
  int ans;
  if (check(r))
    ans = r;
  else
    ans = l;
  check(ans);
  printf("%d\n", ans - 1);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++)
      if (dis[i][j] >= ans)
        putchar('X');
      else
        putchar('.');
    puts("");
  }
  return 0;
}
