#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
constexpr int dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
constexpr int dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
constexpr int inf = 1e9;
int main() {
  int n, m;
  cin >> n >> m;
  vector<string> v(n);
  int fire_cnt = 0;
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
    for (int j = 0; j < m; ++j) {
      fire_cnt += v[i][j] == 'X';
    }
  }
  auto in_range = [&](int y, int x) {
    return 0 <= y && y < n && 0 <= x && x < m;
  };
  queue<pii> que;
  vector<vector<int>> dist(n, vector<int>(m, inf));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (v[i][j] != 'X') continue;
      bool fst = false;
      for (int k = 0; k < 8; ++k) {
        const int ny = i + dy[k], nx = j + dx[k];
        fst |= !in_range(ny, nx) || v[ny][nx] == '.';
      }
      if (fst) {
        dist[i][j] = 0;
        que.emplace(i, j);
      }
    }
  }
  while (!que.empty()) {
    const auto [y, x] = que.front();
    que.pop();
    for (int i = 0; i < 8; ++i) {
      const int ny = y + dy[i], nx = x + dx[i];
      if (!in_range(ny, nx)) continue;
      if (v[ny][nx] == 'X' && dist[ny][nx] == inf) {
        dist[ny][nx] = dist[y][x] + 1;
        que.emplace(ny, nx);
      }
    }
  }
  vector<vector<bool>> vis(n, vector<bool>(m));
  auto check = [&](int t) {
    for (auto& b : vis) {
      fill(b.begin(), b.end(), false);
    }
    queue<tuple<int, int, int>> que;
    int cnt = 0;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < m; ++j) {
        if (v[i][j] == 'X' && dist[i][j] >= t) {
          que.emplace(i, j, dist[i][j]);
          vis[i][j] = true;
          cnt += 1;
        }
      }
    }
    while (!que.empty()) {
      auto const [y, x, rem] = que.front();
      que.pop();
      if (rem == 0) continue;
      for (int i = 0; i < 8; ++i) {
        const int ny = y + dy[i], nx = x + dx[i];
        if (!in_range(ny, nx) || vis[ny][nx] || v[ny][nx] == '.') continue;
        vis[ny][nx] = true;
        cnt += 1;
        que.emplace(ny, nx, rem - 1);
      }
    }
    return cnt == fire_cnt;
  };
  int lb = 0, ub = m * n;
  while (ub - lb > 1) {
    const int mid = (ub + lb) / 2;
    (check(mid) ? lb : ub) = mid;
  }
  auto ans = v;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (dist[i][j] < lb) {
        ans[i][j] = '.';
      }
    }
  }
  cout << lb << endl;
  for (auto const& s : ans) {
    cout << s << "\n";
  }
}
