#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
int n, m;
vector<string> v(maxn);
int func(int x, vector<vector<int>> &s) {
  vector<vector<int>> t(n + 3, vector<int>(m + 3, 0));
  int i, j;
  for (i = 1; i <= n - 2 * x; i++) {
    for (j = 1; j <= m - 2 * x; j++)
      if (s[i + 2 * x][j + 2 * x] - s[i - 1][j + 2 * x] - s[i + 2 * x][j - 1] +
              s[i - 1][j - 1] ==
          (2 * x + 1) * (2 * x + 1)) {
        t[i][j]++, t[i + 2 * x + 1][j + 2 * x + 1]++;
        t[i][j + 2 * x + 1]--, t[i + 2 * x + 1][j]--;
      }
  }
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++) {
      t[i][j] = t[i][j] + t[i - 1][j] + t[i][j - 1] - t[i - 1][j - 1];
      if (v[i][j - 1] == 'X' && t[i][j] <= 0) return 0;
    }
  return 1;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  int i, j;
  cin >> n >> m;
  vector<vector<int>> s(n + 2, vector<int>(m + 2, 0));
  for (i = 1; i <= n; i++) {
    cin >> v[i];
    for (j = 1; j <= m; j++)
      s[i][j] =
          s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + (v[i][j - 1] == 'X');
  }
  int l = 0, r = n, mid;
  while (l < r) {
    mid = (l + r + 1) >> 1;
    if (func(mid, s))
      l = mid;
    else
      r = mid - 1;
  }
  if (l == 0) {
    cout << 0 << "\n";
    for (i = 1; i <= n; i++) cout << v[i] << "\n";
    return 0;
  }
  cout << l << "\n";
  vector<string> ans(n + 1, string(m, '.'));
  for (i = 1; i <= n - 2 * l; i++) {
    for (j = 1; j <= m - 2 * l; j++) {
      if (s[i + 2 * l][j + 2 * l] - s[i - 1][j + 2 * l] - s[i + 2 * l][j - 1] +
              s[i - 1][j - 1] ==
          (2 * l + 1) * (2 * l + 1))
        ans[i + l][j + l - 1] = 'X';
    }
  }
  for (i = 1; i <= n; i++) cout << ans[i] << "\n";
  return 0;
}
