#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
using namespace std;
int fx[4] = {0, 1, 0, -1};
int fy[4] = {1, 0, -1, 0};
const long long M = 2e5 + 10, M2 = 1e6 + 10, oo = 1e9 + 7;
const long long MC = (1 << 22) + 5;
vector<string> vs, setfire;
int n, m;
void getpre(int id, vector<vector<int> > &pre) {
  pre = vector<vector<int> >(n + 2, vector<int>(m + 2, 0));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (id == 0 && vs[i][j] == 'X') pre[i][j]++;
      if (id == 1 && setfire[i][j] == 'X') pre[i][j]++;
      if (i > 0) pre[i][j] += pre[i - 1][j];
      if (j > 0) pre[i][j] += pre[i][j - 1];
      if (i > 0 && j > 0) pre[i][j] -= pre[i - 1][j - 1];
    }
  }
}
int getsum(int lx, int ly, int rx, int ry, vector<vector<int> > &pre) {
  if (lx < 0 || lx >= n || ly < 0 || ly >= m || rx < 0 || rx >= n || ry < 0 ||
      ry >= m)
    return 0;
  int ret = pre[rx][ry];
  if (lx > 0) ret -= pre[lx - 1][ry];
  if (ly > 0) ret -= pre[rx][ly - 1];
  if (lx > 0 && ly > 0) ret += pre[lx - 1][ly - 1];
  return ret;
}
int iscenter(int x, int y, int t, vector<vector<int> > &pre) {
  int lx = x - t, ly = y - t, rx = x + t, ry = y + t;
  if (getsum(lx, ly, rx, ry, pre) == (2 * t + 1) * (2 * t + 1)) return 1;
  return 0;
}
int iscell(int x, int y, int t, vector<vector<int> > &cenpre) {
  if (vs[x][y] == '.') return 1;
  int lx = max(0, x - t), ly = max(0, y - t), rx = min(n - 1, x + t),
      ry = min(m - 1, y + t);
  int ret = getsum(lx, ly, rx, ry, cenpre);
  return (ret > 0);
}
void precomp() {}
void solve() {
  cin >> n >> m;
  vector<vector<int> > pre, cenpre;
  string s;
  for (int i = 0; i < n; i++) {
    cin >> s;
    vs.push_back(s);
  }
  getpre(0, pre);
  setfire = vector<string>(n + 2, string(m + 2, '.'));
  auto can = [&](int tm) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (iscenter(i, j, tm, pre)) {
          setfire[i][j] = 'X';
        } else
          setfire[i][j] = '.';
      }
    }
    getpre(1, cenpre);
    int ret = 1;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        ret &= (iscell(i, j, tm, cenpre));
      }
    }
    return ret;
  };
  int lo = 0, hi = min(n, m);
  while (lo < hi) {
    int mid = (lo + hi) / 2;
    if (can(mid))
      lo = mid + 1;
    else
      hi = mid;
  }
  cout << lo - 1 << '\n';
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (iscenter(i, j, lo - 1, pre))
        cout << "X";
      else
        cout << ".";
    }
    cout << '\n';
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  precomp();
  solve();
  return 0;
}
