#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 14, inf = 1e9;
int n, m;
string c[maxn];
vector<int> up[maxn], dn[maxn], lef[maxn], ri[maxn], d[maxn];
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> c[i];
    up[i].resize(m);
    dn[i].resize(m);
    lef[i].resize(m);
    ri[i].resize(m);
    d[i].resize(m);
    fill(d[i].begin(), d[i].end(), inf);
    for (int j = 0; j < m; j++)
      if (c[i][j] == 'X') {
        lef[i][j] = j ? lef[i][j - 1] + 1 : 1;
        up[i][j] = i ? up[i - 1][j] + 1 : 1;
      }
  }
  for (int i = n - 1; i >= 0; i--)
    for (int j = m - 1; j >= 0; j--)
      if (c[i][j] == 'X') {
        ri[i][j] = j != m - 1 ? ri[i][j + 1] + 1 : 1;
        dn[i][j] = i != n - 1 ? dn[i + 1][j] + 1 : 1;
      }
  int t = 1e9;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (c[i][j] == 'X')
        t = min(t, min(dn[i][j] + up[i][j], lef[i][j] + ri[i][j]));
  t = (t - 2) / 2;
  cout << t << '\n';
  queue<pair<int, int> > q;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (c[i][j] == '.') {
        q.push({i, j});
        d[i][j] = 0;
      }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (c[i][j] != '.' && (i == 0 || i == n - 1 || j == 0 || j == m - 1)) {
        q.push({i, j});
        d[i][j] = 1;
      }
  while (q.size()) {
    auto [x, y] = q.front();
    q.pop();
    for (int i = -1; i < 2; i++)
      for (int j = -1; j < 2; j++) {
        int nx = x + i, ny = y + j;
        if (0 <= nx && nx < n && 0 <= ny && ny < m && d[nx][ny] > 1 + d[x][y]) {
          d[nx][ny] = 1 + d[x][y];
          q.push({nx, ny});
        }
      }
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (c[i][j] == 'X' && d[i][j] <= t) c[i][j] = '.';
  for (int i = 0; i < n; i++) cout << c[i] << '\n';
}
