#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
vector<int> sum[N];
string s[N];
int vist[N], q[N], dx[8] = {0, 0, 1, 1, 1, -1, -1, -1},
                   dy[8] = {1, -1, 1, 0, -1, 1, 0, -1}, dep[N];
int n, m;
void insert(int x) {
  int len, i, j, cnt;
  memset(vist, 0, sizeof(vist));
  len = x * 2 + 1;
  for (i = 0; i < n; i++)
    for (j = 0; j < m; j++) {
      cnt = sum[i][j];
      if (i >= len) cnt -= sum[i - len][j];
      if (j >= len) cnt -= sum[i][j - len];
      if (i >= len && j >= len) cnt += sum[i - len][j - len];
      if (cnt == len * len) vist[(i - x) * m + j - x] = 1;
    }
}
int bfs(int depth) {
  int head, tail, i, j, x, y, xx, yy, k;
  head = 0;
  tail = 0;
  for (i = 0; i < n * m; i++)
    if (vist[i]) {
      q[++tail] = i;
      dep[i] = 0;
      if (s[i / m][i % m] == '.') return 0;
    }
  while (head < tail) {
    head++;
    x = q[head] / m;
    y = q[head] % m;
    if (dep[x * m + y] > depth) break;
    if (dep[x * m + y] < depth)
      for (k = 0; k < 8; k++) {
        xx = x + dx[k];
        yy = y + dy[k];
        if (xx >= 0 && yy >= 0 && xx < n && yy < m && !vist[xx * m + yy]) {
          vist[xx * m + yy] = 1;
          q[++tail] = xx * m + yy;
          dep[xx * m + yy] = dep[x * m + y] + 1;
          if (s[xx][yy] == '.') return 0;
        }
      }
  }
  for (i = 0; i < n; i++)
    for (j = 0; j < m; j++)
      if (s[i][j] == 'X' && vist[i * m + j] == 0) return 0;
  return 1;
}
int judge(int x) {
  insert(x);
  return bfs(x);
}
int main() {
  int i, j, L, R;
  scanf("%d%d", &n, &m);
  for (i = 0; i < n; i++) cin >> s[i];
  for (i = 0; i < n; i++)
    for (j = 0; j < m; j++) {
      if (s[i][j] == 'X')
        sum[i].push_back(1);
      else
        sum[i].push_back(0);
    }
  for (i = 1; i < n; i++) sum[i][0] += sum[i - 1][0];
  for (j = 1; j < m; j++) sum[0][j] += sum[0][j - 1];
  for (i = 1; i < n; i++)
    for (j = 1; j < m; j++)
      sum[i][j] += sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];
  L = 0;
  R = min(n, m) + 1;
  while (R - L > 1) {
    int mid = (L + R) >> 1;
    if (judge(mid))
      L = mid;
    else
      R = mid;
  }
  printf("%d\n", L);
  insert(L);
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      if (vist[i * m + j])
        printf("X");
      else
        printf(".");
    }
    printf("\n");
  }
  return 0;
}
