#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  vector<vector<bool>> G(n + 10, vector<bool>(m + 10));
  for (int r = 1; r <= n; r++) {
    for (int c = 1; c <= m; c++) {
      char v;
      cin >> v;
      if (v == 'X') G[r][c] = 1;
    }
  }
  vector<vector<int>> GP(n + 10, vector<int>(m + 10));
  for (int r = 1; r <= n; r++)
    for (int c = 1; c <= m; c++)
      GP[r][c] = int(G[r][c]) + GP[r - 1][c] + GP[r][c - 1] - GP[r - 1][c - 1];
  auto calc = [&](int r1, int c1, int r2, int c2) -> int {
    r1 = max(r1, 1);
    c1 = max(c1, 1);
    r2 = min(r2, n);
    c2 = min(c2, m);
    return GP[r2][c2] - GP[r1 - 1][c2] - GP[r2][c1 - 1] + GP[r1 - 1][c1 - 1];
  };
  vector<vector<int>> DP(n + 10, vector<int>(m + 10));
  auto solve = [&](int K) {
    if (K % 2 == 0) K--;
    for (int r = 1; r <= n; r++)
      for (int c = 1; c <= m; c++) DP[r][c] = 0;
    for (int r = 1; r <= n; r++) {
      for (int c = 1; c <= m; c++) {
        if (calc(r, c, r + K - 1, c + K - 1) == K * K) {
          DP[r][c]++;
          DP[r + K][c]--;
          DP[r][c + K]--;
          DP[r + K][c + K]++;
        }
      }
    }
    for (int r = 1; r <= n; r++) {
      for (int c = 1; c <= m; c++) {
        DP[r][c] += DP[r - 1][c] + DP[r][c - 1] - DP[r - 1][c - 1];
        if ((DP[r][c] > 0) != G[r][c]) return false;
      }
    }
    return true;
  };
  int low = 0, high = min(n, m);
  while (low < high) {
    int K = (low + high + 1) / 2;
    if (solve(K))
      low = K;
    else
      high = K - 1;
  }
  int K = low;
  if (K % 2 == 0) K--;
  vector<vector<bool>> ans(n + 10, vector<bool>(m + 10));
  for (int r = 1; r <= n - K + 1; r++) {
    for (int c = 1; c <= m - K + 1; c++) {
      int j = calc(r, c, r + K - 1, c + K - 1);
      if (j == K * K) ans[(2 * r + K - 1) / 2][(2 * c + K - 1) / 2] = 1;
    }
  }
  cout << K / 2 << endl;
  for (int r = 1; r <= n; r++) {
    for (int c = 1; c <= m; c++) {
      if (ans[r][c] == 1)
        cout << 'X';
      else
        cout << '.';
    }
    cout << "\n";
  }
  return 0;
}
