#include <bits/stdc++.h>
#pragma GCC optimize("Ofast", "unroll-loops", "omit-frame-pointer", "inline")
#pragma GCC option("arch=native", "tune=native", "no-zero-upper")
#pragma GCC target("avx2")
#pragma GCC optimize("03")
#pragma GCC target("sse4")
using namespace std;
const long long oo = (long long)(1e9);
string second[(long long)(1e6 + 5)];
vector<bool> mk[(long long)(1e6 + 5)];
vector<int> lvl[(long long)(1e6 + 5)];
vector<int> kk[(long long)(1e6 + 5)];
int n, m;
string ss[(long long)(1e6 + 5)];
bool ok(int x) {
  queue<pair<long long, long long> > q;
  for (int i = 0; i < n; i++) {
    ss[i] = second[i];
    kk[i].resize(m);
    for (int j = 0; j < m; j++) {
      if (second[i][j] != 'X') continue;
      ss[i][j] = '.';
      mk[i][j] = 0;
      kk[i][j] = 0;
      if (lvl[i][j] >= x) {
        q.push(pair<long long, long long>(i, j));
        mk[i][j] = 1;
        kk[i][j] = x;
      }
    }
  }
  while (!q.empty()) {
    pair<long long, long long> u = q.front();
    q.pop();
    int x = u.first;
    int y = u.second;
    ss[x][y] = 'X';
    if (!kk[x][y]) continue;
    for (int dx = -1; dx < 2; dx++)
      for (int dy = -1; dy < 2; dy++) {
        if (dx + x < 0 || dx + x >= n) return 0;
        if (dy + y < 0 || dy + y >= m) return 0;
        int i = dx + x;
        int j = dy + y;
        if (mk[i][j]) continue;
        if (second[i][j] == '.') return 0;
        mk[i][j] = 1;
        kk[i][j] = kk[x][y] - 1;
        q.push(pair<long long, long long>(i, j));
      }
  }
  bool res = 1;
  for (int i = 0; i < n; i++) {
    res &= (second[i] == ss[i]);
  }
  return res;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> second[i];
    mk[i].resize(m);
    lvl[i].resize(m);
  }
  queue<pair<long long, long long> > q;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      if (second[i][j] != 'X') continue;
      if (!i) {
        q.push(pair<long long, long long>(i, j));
        mk[i][j] = 1;
        continue;
      }
      if (i == n - 1) {
        q.push(pair<long long, long long>(i, j));
        mk[i][j] = 1;
        continue;
      }
      if (!j) {
        q.push(pair<long long, long long>(i, j));
        mk[i][j] = 1;
        continue;
      }
      if (j == m - 1) {
        q.push(pair<long long, long long>(i, j));
        mk[i][j] = 1;
        continue;
      }
      bool kk = 0;
      for (int dx = -1; dx < 2; dx++)
        for (int dy = -1; dy < 2; dy++) kk |= (second[i + dx][j + dy] == '.');
      if (kk) {
        mk[i][j] = 1;
        q.push(pair<long long, long long>(i, j));
      }
    }
  int ma = 0;
  while (!q.empty()) {
    pair<long long, long long> u = q.front();
    q.pop();
    int x = u.first;
    int y = u.second;
    for (int dx = -1; dx < 2; dx++)
      for (int dy = -1; dy < 2; dy++) {
        if (dx + x < 0 || dx + x >= n) continue;
        if (dy + y < 0 || dy + y >= m) continue;
        int i = dx + x;
        int j = dy + y;
        if (mk[i][j] || second[i][j] == '.') continue;
        mk[i][j] = 1;
        lvl[i][j] = lvl[x][y] + 1;
        ma = max(ma, lvl[i][j]);
        q.push(pair<long long, long long>(i, j));
      }
  }
  int res = 0;
  for (int po = (1 << 19); po; po >>= 1)
    if (res + po <= ma && ok(res + po)) res += po;
  cout << (res) << '\n';
  for (int i = 0; i < n; i++, cout << '\n')
    for (int j = 0; j < m; j++) {
      if (second[i][j] == '.' || lvl[i][j] < res)
        cout << '.';
      else
        cout << 'X';
    }
  return 0;
}
