#include <bits/stdc++.h>
using namespace std;
long long n, m;
vector<string> a;
vector<vector<long long>> pr;
vector<pair<long long, long long>> nxt{{1, 0},  {1, 1},   {1, -1}, {-1, 0},
                                       {-1, 1}, {-1, -1}, {0, -1}, {0, 1}};
bool check(long long time) {
  queue<pair<long long, long long>> q;
  vector<vector<long long>> mn(n, vector<long long>(m, 1e9));
  long long k = (time + time + 1);
  k = k * k;
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < m; j++) {
      if (i - time < 0 || j - time < 0 || i + time >= n || j + time >= m)
        continue;
      if (pr[i + time + 1][j + time + 1] + pr[i - time][j - time] -
              pr[i + time + 1][j - time] - pr[i - time][j + time + 1] !=
          k)
        continue;
      else {
        q.push({i, j});
        mn[i][j] = 0;
      }
    }
  }
  while (!q.empty()) {
    auto first = q.front();
    q.pop();
    for (auto j : nxt) {
      if (first.first + j.first >= n || first.first + j.first < 0 ||
          first.second + j.second >= m || first.second + j.second < 0)
        continue;
      if (mn[first.first + j.first][first.second + j.second] != 1e9) continue;
      mn[first.first + j.first][first.second + j.second] =
          mn[first.first][first.second] + 1;
      q.push({first.first + j.first, first.second + j.second});
    }
  }
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < m; j++) {
      if ((a[i][j] == '.' && mn[i][j] <= time) ||
          (a[i][j] == 'X' && mn[i][j] > time))
        return false;
    }
  }
  return true;
}
int32_t main() {
  cin.tie(NULL);
  cout.tie(NULL);
  ios::sync_with_stdio(false);
  cin >> n >> m;
  a.resize(n);
  for (long long i = 0; i < n; i++) cin >> a[i];
  pr.resize(n + 1, vector<long long>(m + 1, 0));
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < m; j++) {
      pr[i + 1][j + 1] =
          pr[i + 1][j] + pr[i][j + 1] - pr[i][j] + (a[i][j] == 'X' ? 1 : 0);
    }
  }
  long long l = 0, r = min(n, m) + 1, mid;
  while (r - l > 1) {
    mid = (r + l) / 2;
    if (check(mid))
      l = mid;
    else
      r = mid;
  }
  long long time = l;
  cout << time << '\n';
  long long k = (time + time + 1);
  k = k * k;
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < m; j++) {
      if (i - time < 0 || j - time < 0 || i + time >= n || j + time >= m) {
        cout << '.';
        continue;
      }
      if (pr[i + time + 1][j + time + 1] + pr[i - time][j - time] -
              pr[i + time + 1][j - time] - pr[i - time][j + time + 1] !=
          k)
        cout << '.';
      else
        cout << 'X';
    }
    cout << '\n';
  }
  return 0;
}
