#include <bits/stdc++.h>
using namespace std;
vector<vector<char>> a;
vector<vector<int>> p;
int n, m;
int cnt(int i, int j, int x, int y) {
  int s = p[x][y];
  if (i > 0) s -= p[i - 1][y];
  if (j > 0) s -= p[x][j - 1];
  if (i > 0 && j > 0) s += p[i - 1][j - 1];
  return s;
}
struct pos {
  int i, j, T;
};
bool check(int t) {
  queue<pos> q;
  auto b = a;
  b.assign(n, vector<char>(m, '.'));
  for (int i = t; i < n - t; i++) {
    for (int j = t; j < m - t; j++) {
      if (cnt(i - t, j - t, i + t, j + t) == 1ll * (t * 2 + 1) * (t * 2 + 1)) {
        q.push({i, j, t});
        b[i][j] = 'X';
      }
    }
  }
  while ((int)(q).size()) {
    pos cur = q.front();
    q.pop();
    if (cur.T == 0) continue;
    for (int i = -1; i <= 1; i++) {
      for (int j = -1; j <= 1; j++) {
        if (cur.i + i >= 0 && cur.i + i < n && cur.j + j >= 0 &&
            cur.j + j < m && b[i + cur.i][j + cur.j] == '.') {
          q.push({cur.i + i, cur.j + j, cur.T - 1});
          b[cur.i + i][cur.j + j] = 'X';
        }
      }
    }
  }
  return b == a;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  cin >> n >> m;
  p.resize(n, vector<int>(m));
  a.resize(n, vector<char>(m));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
      p[i][j] = a[i][j] == 'X';
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (i > 0) p[i][j] += p[i - 1][j];
      if (j > 0) p[i][j] += p[i][j - 1];
      if (i > 0 && j > 0) p[i][j] -= p[i - 1][j - 1];
    }
  }
  int left = 0, right = min(n, m) + 1;
  while (left + 1 < right) {
    int x = (left + right) / 2;
    if (check(x)) {
      left = x;
    } else {
      right = x;
    }
  }
  int t = left;
  cout << t << '\n';
  a.assign(n, vector<char>(m, '.'));
  for (int i = t; i < n - t; i++) {
    for (int j = t; j < m - t; j++) {
      if (cnt(i - t, j - t, i + t, j + t) == 1ll * (t * 2 + 1) * (t * 2 + 1)) {
        a[i][j] = 'X';
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cout << a[i][j];
    }
    cout << '\n';
  }
  return 0;
}
