#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<vector<char>> C;
int dx[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
inline bool check(int x, int y) {
  return x >= 0 and y >= 0 and x < n and y < m;
}
pair<bool, vector<vector<char>>> bfs(int t) {
  vector<vector<int>> dist(n, vector<int>(m, -1));
  queue<pair<int, pair<int, int>>> q;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (C[i][j] == '.') {
        q.push({0, {i, j}});
        dist[i][j] = 0;
      }
  while (!q.empty()) {
    int d = q.front().first;
    int l = q.front().second.first, r = q.front().second.second;
    q.pop();
    for (int i = 0; i < 8; i++) {
      int x = l + dx[i], y = r + dy[i];
      if (!check(x, y) or dist[x][y] != -1) continue;
      dist[x][y] = d + 1;
      q.push({d + 1, {x, y}});
    }
  }
  vector<vector<char>> cor(n, vector<char>(m, '.'));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (C[i][j] == 'X' and dist[i][j] > t) {
        q.push({0, {i, j}});
        cor[i][j] = 'X';
      }
  while (!q.empty()) {
    int d = q.front().first;
    int l = q.front().second.first, r = q.front().second.second;
    q.pop();
    if (d >= t) continue;
    for (int i = 0; i < 8; i++) {
      int x = l + dx[i], y = r + dy[i];
      if (!check(x, y) or cor[x][y] == 'X') continue;
      cor[x][y] = 'X';
      q.push({d + 1, {x, y}});
    }
  }
  bool fl = true;
  vector<vector<char>> ANS(n, vector<char>(m, '.'));
  for (int i = 0; i < n and fl; i++)
    for (int j = 0; j < m and fl; j++)
      if (C[i][j] != cor[i][j]) fl = false;
  if (!fl) return {fl, ANS};
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (C[i][j] == 'X' and dist[i][j] > t) ANS[i][j] = 'X';
  return {fl, ANS};
}
int main() {
  cin >> n >> m;
  n += 2;
  m += 2;
  C.resize(n);
  fill(C.begin(), C.end(), vector<char>(m, '.'));
  for (int i = 1; i < n - 1; i++)
    for (int j = 1; j < m - 1; j++) scanf(" %c", &C[i][j]);
  int b = 0, e = min(n, m), T = -1;
  vector<vector<char>> ANS;
  while (b <= e) {
    int t = (b + e) / 2;
    auto it = bfs(t);
    if (it.first) {
      ANS = it.second;
      T = t;
      b = t + 1;
    } else
      e = t - 1;
  }
  assert(T != -1);
  cout << T << '\n';
  for (int i = 1; i < n - 1; i++) {
    for (int j = 1; j < m - 1; j++) printf("%c", ANS[i][j]);
    puts("");
  }
  return 0;
}
