#include <bits/stdc++.h>
using namespace std;
long long int n, m;
long long int k = 0;
void dfs(vector<pair<long long int, long long int>> &shit,
         vector<vector<bool>> &vis, long long int tm) {
  queue<pair<long long int, pair<long long int, long long int>>> aw;
  for (auto it : shit) {
    vis[it.first + 1][it.second + 1] = true;
    ++k;
    aw.push({0ll, {it.first + 1, it.second + 1}});
  }
  while (!aw.empty()) {
    pair<long long int, pair<long long int, long long int>> w = aw.front();
    aw.pop();
    if (w.first == tm) continue;
    for (int i = w.second.first - 1; i <= w.second.first + 1; i++)
      for (int j = w.second.second - 1; j <= w.second.second + 1; j++) {
        if (i >= 1 && i <= n && j >= 1 && j <= m && !vis[i][j]) {
          vis[i][j] = true;
          ++k;
          aw.push({w.first + 1, {i, j}});
        }
      }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> m;
  string a[n];
  for (int x = 0; x < n; x++) cin >> a[x];
  long long int w[n + 1][m + 1];
  vector<vector<bool>> vis(n + 1);
  for (int x = 0; x <= n; x++) vis[x].resize(m + 1);
  memset(w, 0, sizeof(w));
  long long int co = 0;
  for (int x = 1; x <= n; x++)
    for (int y = 1; y <= m; y++) {
      if (a[x - 1][y - 1] == 'X') {
        ++w[x][y];
        ++co;
      }
      w[x][y] += w[x - 1][y] + w[x][y - 1] - w[x - 1][y - 1];
    }
  long long int tl = 0;
  long long int tr = max(n, m);
  vector<pair<long long int, long long int>> shit1;
  while (tl < tr) {
    long long int tm = (tl + tr + 1) / 2;
    vector<pair<long long int, long long int>> shit;
    for (int x = 0; x < n; x++)
      for (int y = 0; y < m; y++) {
        if (a[x][y] == 'X') {
          long long int r1 = x + 1 - tm;
          long long int r2 = x + 1 + tm;
          long long int c1 = (y + 1 - tm);
          long long int c2 = (y + 1 + tm);
          if (r1 >= 1 && c1 >= 1 && c2 <= m && r2 <= n) {
            if (w[r2][c2] - w[r2][c1 - 1] - w[r1 - 1][c2] + w[r1 - 1][c1 - 1] ==
                (2 * tm + 1) * (2 * tm + 1))
              shit.push_back({x, y});
          }
        }
      }
    for (int x = 0; x <= n; x++)
      for (int y = 0; y <= m; y++) vis[x][y] = false;
    k = 0;
    dfs(shit, vis, tm);
    if (k == co) {
      tl = tm;
      shit1 = shit;
    } else
      tr = tm - 1;
  }
  cout << tl << "\n";
  if (tl == 0) {
    for (int x = 0; x < n; x++) cout << a[x] << "\n";
    return 0;
  }
  char damm[n][m];
  for (int x = 0; x < n; x++)
    for (int y = 0; y < m; y++) damm[x][y] = '.';
  for (auto it : shit1) damm[it.first][it.second] = 'X';
  for (int x = 0; x < n; x++) {
    for (int y = 0; y < m; y++) {
      cout << damm[x][y];
    }
    cout << "\n";
  }
}
