#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 100;
string s[N];
char s1[N];
int n, m;
vector<vector<int> > dist;
vector<vector<int> > pre;
void init() {
  dist.resize(n + 2);
  pre.resize(n + 2);
  for (int i = 0; i <= n + 1; i++) {
    dist[i].resize(m + 2);
    pre[i].resize(m + 2);
    for (int j = 0; j <= m + 1; j++) {
      dist[i][j] = 1e8;
      pre[i][j] = 0;
    }
  }
  for (int j = 0; j <= m + 1; j++) {
    s[0].push_back('.');
    s[n + 1].push_back('.');
  }
}
bool ch(int x, int y) {
  if (x < 0 || y < 0) return 0;
  if (x > n + 1 || y > m + 1) return 0;
  return 1;
}
int dx[] = {0, 0, -1, 1, 1, 1, -1, -1};
int dy[] = {-1, 1, 0, 0, 1, -1, 1, -1};
void bfs() {
  queue<pair<int, int> > q;
  for (int i = 0; i <= n + 1; i++) {
    for (int j = 0; j <= m + 1; j++) {
      if (s[i][j] != '.') continue;
      q.push({i, j});
      dist[i][j] = 0;
    }
  }
  int x, y;
  while (!q.empty()) {
    x = q.front().first;
    y = q.front().second;
    q.pop();
    for (int i = 0; i < 8; i++) {
      int a = x + dx[i];
      int b = y + dy[i];
      if (!ch(a, b)) continue;
      if (dist[a][b] <= dist[x][y] + 1) continue;
      dist[a][b] = dist[x][y] + 1;
      q.push({a, b});
    }
  }
}
bool ch(int mid) {
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      pre[i][j] = 0;
    }
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      if (s[i][j] == '.') continue;
      if (dist[i][j] <= mid) continue;
      pre[i - mid][j - mid]++;
      pre[i - mid][j + mid + 1]--;
      pre[i + mid + 1][j - mid]--;
      pre[i + mid + 1][j + mid + 1]++;
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      pre[i][j] += pre[i - 1][j];
      pre[i][j] += pre[i][j - 1];
      pre[i][j] -= pre[i - 1][j - 1];
      if (!pre[i][j] && s[i][j] == 'X') return 0;
    }
  }
  return 1;
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    scanf("%s", s1 + 1);
    s1[0] = '.';
    s1[m + 1] = '.';
    s1[m + 2] = '\0';
    s[i] = s1;
  }
  init();
  bfs();
  int l = 0, h = 2e6, res = 0;
  while (l <= h) {
    int m = (l + h) / 2;
    if (ch(m)) {
      res = m;
      l = m + 1;
    } else {
      h = m - 1;
    }
  }
  cout << res << endl;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (s[i][j] == '.' || dist[i][j] <= res) {
        printf(".");
      } else
        printf("X");
    }
    printf("\n");
  }
  return 0;
}
