#include <bits/stdc++.h>
using namespace std;
int n, m, dx[8] = {0, 1, 1, 1, 0, -1, -1, -1},
          dy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
bool isPossible(int t, vector<vector<bool>>& g, bool cr = false) {
  vector<vector<bool>> v(n, vector<bool>(m));
  queue<pair<int, int>> q;
  for (int i = 1; i < n - 1; i++)
    for (int j = 1; j < m - 1; j++)
      if (!g[i][j]) q.push({i, j});
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (g[i][j]) v[i][j] = true;
  for (int tt = 0; tt < t; tt++) {
    int s = ((int)((q).size()));
    if (s == 0) return false;
    for (int k = 0; k < s; k++) {
      auto p = q.front();
      q.pop();
      int i = p.first, j = p.second;
      for (int d = 0; d < 8; d++)
        if (v[i + dx[d]][j + dy[d]])
          v[i + dx[d]][j + dy[d]] = false, q.push({i + dx[d], j + dy[d]});
    }
  }
  if (cr) {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) g[i][j] = v[i][j];
    return true;
  }
  while (!q.empty()) q.pop();
  for (int i = 1; i < n - 1; i++)
    for (int j = 1; j < m - 1; j++)
      if (v[i][j]) q.push({i, j});
  for (int tt = 0; tt < t; tt++) {
    int s = ((int)((q).size()));
    for (int k = 0; k < s; k++) {
      auto p = q.front();
      q.pop();
      int i = p.first, j = p.second;
      for (int d = 0; d < 8; d++)
        if (!v[i + dx[d]][j + dy[d]])
          v[i + dx[d]][j + dy[d]] = true, q.push({i + dx[d], j + dy[d]});
    }
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (g[i][j] != v[i][j]) return false;
  return true;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(10);
  cin >> n >> m;
  vector<vector<bool>> g(n + 4, vector<bool>(m + 4));
  vector<vector<bool>> v(n + 4, vector<bool>(m + 4));
  char c;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) cin >> c, g[i + 2][j + 2] = (c == 'X');
  n += 4, m += 4;
  int l = 0, r = min(n, m);
  while (l + 1 < r) {
    int mid = (l + r) / 2;
    if (isPossible(mid, g))
      l = mid;
    else
      r = mid;
  }
  isPossible(l, g, true);
  cout << l << "\n";
  for (int i = 2; i < n - 2; i++, cout << "\n")
    for (int j = 2; j < m - 2; j++) cout << (g[i][j] ? 'X' : '.');
}
