#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int N, M;
  cin >> N >> M;
  vector<string> G(N);
  for (int i = 0; i < N; i++) {
    cin >> G[i];
  }
  vector<vector<int>> sum(N + 1, vector<int>(M + 1));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      sum[i + 1][j + 1] =
          sum[i + 1][j] + sum[i][j + 1] - sum[i][j] + (G[i][j] == 'X');
    }
  }
  auto getSum = [&](int xlo, int xhi, int ylo, int yhi) -> int {
    if (xlo < 0 || xhi > N || ylo < 0 || yhi > M) {
      return 0;
    }
    assert(xlo < xhi && ylo < yhi);
    return sum[xhi][yhi] + sum[xlo][ylo] - sum[xlo][yhi] - sum[xhi][ylo];
  };
  vector<vector<int>> covered(N + 1, vector<int>(M + 1));
  auto isGood = [&](int t) -> bool {
    int s = 2 * t + 1;
    for (int i = 0; i <= N; i++) {
      covered[i].assign(M + 1, 0);
    }
    for (int i = t; i + t + 1 <= N; i++) {
      for (int j = t; j + t + 1 <= M; j++) {
        if (getSum(i - t, i + t + 1, j - t, j + t + 1) == s * s) {
          covered[i - t][j - t]++;
          covered[i + t + 1][j + t + 1]++;
          covered[i - t][j + t + 1]--;
          covered[i + t + 1][j - t]--;
        }
      }
    }
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < M; j++) {
        covered[i][j + 1] += covered[i][j];
      }
    }
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < M; j++) {
        covered[i + 1][j] += covered[i][j];
      }
    }
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < M; j++) {
        if (G[i][j] == 'X' && !covered[i][j]) {
          return false;
        }
      }
    }
    return true;
  };
  isGood(1);
  int mi = 0;
  int ma = min(N - 1, M - 1) / 2 + 1;
  while (ma - mi > 1) {
    int md = (mi + ma) / 2;
    if (isGood(md)) {
      mi = md;
    } else {
      ma = md;
    }
  }
  int ans = mi;
  int t = mi;
  int s = 2 * ans + 1;
  cout << ans << '\n';
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      if (getSum(i - t, i + t + 1, j - t, j + t + 1) == s * s) {
        cout << 'X';
      } else {
        cout << '.';
      }
    }
    cout << '\n';
  }
  return 0;
}
