#include <bits/stdc++.h>
using namespace std;
template <typename TH>
void _dbg(const char* sdbg, TH h) {
  cerr << sdbg << " = " << h << "\n";
}
template <typename TH, typename... TA>
void _dbg(const char* sdbg, TH h, TA... t) {
  while (*sdbg != ',') cerr << *sdbg++;
  cerr << " = " << h << ",";
  _dbg(sdbg + 1, t...);
}
const int INF = 1e6;
int n, m;
vector<vector<int>> a, s;
vector<pair<int, int>> pos;
bool Check(int t) {
  pos.clear();
  vector<vector<int>> b = vector<vector<int>>(n + 2, vector<int>(m + 2, 0));
  int cnt = 0;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (a[i][j] == 1) {
        int x = i - t, y = j - t, u = i + t, v = j + t;
        if (x < 1 || y < 1 || u > n || v > m) {
          continue;
        }
        if (s[u][v] - s[x - 1][v] - s[u][y - 1] + s[x - 1][y - 1] ==
            (u - x + 1) * (v - y + 1)) {
          b[x][y]++;
          b[x][v + 1]--;
          b[u + 1][y]--;
          b[u + 1][v + 1]++;
          pos.push_back({i, j});
        }
      }
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      b[i][j] += b[i - 1][j] + b[i][j - 1] - b[i - 1][j - 1];
      if (a[i][j] != (b[i][j] > 0)) {
        return false;
      }
    }
  }
  return true;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  a = vector<vector<int>>(n + 1, vector<int>(m + 1, 0));
  s = vector<vector<int>>(n + 1, vector<int>(m + 1, 0));
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      char c;
      cin >> c;
      a[i][j] = (c == 'X');
      s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + a[i][j];
    }
  }
  int lo = 0, hi = INF, ans = 0;
  while (lo <= hi) {
    int mi = (lo + hi) / 2;
    if (Check(mi)) {
      ans = mi;
      lo = mi + 1;
    } else {
      hi = mi - 1;
    }
  }
  cout << ans << '\n';
  Check(ans);
  vector<vector<char>> tab(n + 1, vector<char>(m + 1, '.'));
  for (auto p : pos) {
    tab[p.first][p.second] = 'X';
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      cout << tab[i][j];
    }
    cout << '\n';
  }
  return 0;
}
