#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000050;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
vector<string> s;
vector<vector<int>> a;
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int n, m, i, j, k, x, y;
  cin >> n >> m;
  s.resize(n);
  for (i = 0; i < n; i++) {
    cin >> s[i];
  }
  a.resize(n + 2, vector<int>(m + 2));
  for (i = 0; i <= n; i++) a[i][0] = 0;
  for (i = 0; i <= m; i++) a[0][i] = 0;
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      if (s[i][j] == '.') continue;
      a[i + 1][j + 1] = min(a[i][j], min(a[i + 1][j], a[i][j + 1])) + 1;
    }
  }
  int l = 0, r = min(n, m), mid, ans;
  vector<vector<int>> b(n + 2, vector<int>(m + 2));
  while (l <= r) {
    mid = (l + r) / 2;
    x = 2 * mid + 1;
    for (i = 1; i <= n; i++)
      for (j = 1; j <= m; j++) b[i][j] = a[i][j] >= x ? x : 0;
    for (i = n; i >= 1; i--) {
      for (j = m; j >= 1; j--) {
        b[i - 1][j] = max(b[i - 1][j], b[i][j] - 1);
        b[i][j - 1] = max(b[i][j - 1], b[i][j] - 1);
        b[i - 1][j - 1] = max(b[i - 1][j - 1], b[i][j] - 1);
      }
    }
    int f = 0;
    for (i = 1; i <= n; i++)
      for (j = 1; j <= m; j++)
        if (s[i - 1][j - 1] == 'X') {
          if (b[i][j] == 0) {
            f = 1;
            break;
          }
        }
    if (f)
      r = mid - 1;
    else {
      l = mid + 1;
      ans = mid;
    }
  }
  vector<string> ans1(n, string(m, '.'));
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      if (a[i][j] >= 2 * ans + 1) ans1[i - ans - 1][j - ans - 1] = 'X';
    }
  }
  cout << ans << endl;
  for (i = 0; i < n; i++) cout << ans1[i] << endl;
  return 0;
}
