#include <bits/stdc++.h>
using namespace std;
int gcd(int x, int y) { return (!y ? x : gcd(y, x % y)); }
const int maxn = 1e6 + 10, inf = 1e9;
vector<int> a[maxn], d[maxn], fir[maxn];
int dx[] = {0, 0, -1, 1, -1, -1, 1, 1};
int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};
int n, m;
bool isin(int x, int y) { return (x >= 0 && x < n && y >= 0 && y < m); }
void bfs() {
  queue<pair<int, int> > q;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      if (a[i][j])
        d[i][j] = inf;
      else {
        d[i][j] = 0;
        q.push({i, j});
      }
    }
  while (q.size()) {
    auto t = q.front();
    q.pop();
    int x = t.first, y = t.second;
    for (int dir = 0; dir < 8; dir++) {
      int nx = x + dx[dir], ny = y + dy[dir];
      if (isin(nx, ny) && d[nx][ny] > d[x][y] + 1) {
        d[nx][ny] = d[x][y] + 1;
        q.push({nx, ny});
      }
    }
  }
}
bool check(int val) {
  queue<pair<int, int> > q;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (a[i][j] && d[i][j] > val)
        fir[i][j] = 0, q.push({i, j});
      else
        fir[i][j] = inf;
  while (q.size()) {
    auto t = q.front();
    q.pop();
    int x = t.first, y = t.second;
    for (int dir = 0; dir < 8; dir++) {
      int nx = x + dx[dir], ny = y + dy[dir];
      if (isin(nx, ny) && fir[nx][ny] > fir[x][y] + 1) {
        fir[nx][ny] = fir[x][y] + 1;
        q.push({nx, ny});
      }
    }
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      if (a[i][j] && fir[i][j] > val) return false;
      if (!a[i][j] && fir[i][j] <= val) return false;
    }
  return true;
}
int32_t main() {
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> n >> m;
  n += 2, m += 2;
  for (int i = 0; i < n; i++) {
    a[i].resize(m), d[i].resize(m), fir[i].resize(m);
    d[i].shrink_to_fit();
    a[i].shrink_to_fit();
    fir[i].shrink_to_fit();
  }
  for (int i = 1; i + 1 < n; i++) {
    string s;
    cin >> s;
    for (int j = 1; j + 1 < m; j++) a[i][j] = (s[j - 1] == 'X');
  }
  bfs();
  int lo = 0, hi = n + m + 2;
  while (hi - lo > 1) {
    int mid = hi + lo >> 1;
    if (check(mid))
      lo = mid;
    else
      hi = mid;
  }
  check(lo);
  cout << lo << '\n';
  for (int i = 1; i + 1 < n; i++) {
    string res;
    for (int j = 1; j + 1 < m; j++) {
      if (fir[i][j] == 0)
        res += 'X';
      else
        res += '.';
    }
    cout << res << '\n';
  }
}
