#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  n += 2;
  m += 2;
  char a[n][m];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (i == 0 || j == 0 || i == n - 1 || j == m - 1) {
        a[i][j] = '.';
      } else {
        cin >> a[i][j];
      }
    }
  }
  int lo = 0, hi = n + m;
  while (hi - lo > 1) {
    int t = (lo + hi) / 2;
    int pr[n + 1][m + 1];
    memset(pr, 0, sizeof(pr));
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (a[i][j] == '.') {
          int x1 = max(0, i - t), y1 = max(0, j - t),
              x2 = min(n - 1, i + t) + 1, y2 = min(m - 1, j + t) + 1;
          pr[x1][y1]++;
          pr[x2][y2]++;
          pr[x1][y2]--;
          pr[x2][y1]--;
        }
      }
    }
    vector<pair<int, int>> to;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (i) pr[i][j] += pr[i - 1][j];
        if (j) pr[i][j] += pr[i][j - 1];
        if (i && j) pr[i][j] -= pr[i - 1][j - 1];
        if (!pr[i][j]) to.push_back({i, j});
      }
    }
    memset(pr, 0, sizeof(pr));
    for (auto e : to) {
      int x1 = e.first - t, y1 = e.second - t, x2 = e.first + t + 1,
          y2 = e.second + t + 1;
      pr[x1][y1]++;
      pr[x1][y2]--;
      pr[x2][y1]--;
      pr[x2][y2]++;
    }
    bool ok = 1;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (i) pr[i][j] += pr[i - 1][j];
        if (j) pr[i][j] += pr[i][j - 1];
        if (i && j) pr[i][j] -= pr[i - 1][j - 1];
        if ((pr[i][j] && a[i][j] == '.') || (!pr[i][j] && a[i][j] == 'X'))
          ok = 0;
      }
    }
    if (ok) {
      lo = t;
    } else {
      hi = t;
    }
  }
  int t = lo;
  int pr[n + 1][m + 1];
  memset(pr, 0, sizeof(pr));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i][j] == '.') {
        int x1 = max(0, i - t), y1 = max(0, j - t), x2 = min(n - 1, i + t) + 1,
            y2 = min(m - 1, j + t) + 1;
        pr[x1][y1]++;
        pr[x1][y2]--;
        pr[x2][y1]--;
        pr[x2][y2]++;
      }
    }
  }
  cout << t << '\n';
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (i) pr[i][j] += pr[i - 1][j];
      if (j) pr[i][j] += pr[i][j - 1];
      if (i && j) pr[i][j] -= pr[i - 1][j - 1];
    }
  }
  for (int i = 1; i < n - 1; i++) {
    for (int j = 1; j < m - 1; j++) {
      cout << (pr[i][j] ? '.' : 'X');
    }
    cout << '\n';
  }
  return 0;
}
