#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> fld, fld1, pr;
vector<int> aval;
int n, m;
inline void init() {
  fld.assign(n, vector<int>(m));
  fld1.assign(n, vector<int>(m));
  pr.assign(n + 1, vector<int>(m + 1));
  aval.assign(m, 0);
}
inline int get(int i1, int j1, int i2, int j2, const vector<vector<int>>& pr) {
  return pr[i2][j2] - pr[i1][j2] - pr[i2][j1] + pr[i1][j1];
}
bool check(int l) {
  fld1.assign(n, vector<int>(m));
  aval.assign(m, 0);
  vector<vector<int>> pr1(n + 1, vector<int>(m + 1));
  for (int i = l; i + l + 1 <= n; i++)
    for (int j = l; j + l + 1 <= m; j++) {
      if (fld[i][j] && get(i - l, j - l, i + l + 1, j + l + 1, pr) ==
                           (2 * l + 1) * (2 * l + 1))
        fld1[i][j] = 1;
    }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      pr1[i + 1][j + 1] =
          pr1[i][j + 1] + pr1[i + 1][j] - pr1[i][j] + fld1[i][j];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (fld[i][j] && !get(max(0, i - l), max(0, j - l), min(i + l + 1, n),
                            min(j + l + 1, m), pr1))
        return 0;
  return 1;
}
inline void solve() {
  init();
  char c;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      cin >> c;
      fld[i][j] = (c == 'X');
    }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      pr[i + 1][j + 1] = pr[i][j + 1] + pr[i + 1][j] - pr[i][j] + fld[i][j];
  int l = 0, r = min(n, m);
  while (r - l > 1) {
    int m = (l + r) / 2;
    if (check(m))
      l = m;
    else
      r = m;
  }
  cout << l << '\n';
  check(l);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) cout << ".X"[fld1[i][j]];
    cout << '\n';
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  while (cin >> n >> m) solve();
}
