#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007LL;
long long large = 2000000000000000000LL;
char s[1000010];
int main() {
  ios::sync_with_stdio(false);
  int n, m;
  scanf("%d%d", &n, &m);
  vector<vector<char> > g(n + 2, vector<char>(m + 2, '.'));
  for (int i = 1; i <= n; i++) {
    scanf("%s", s);
    for (int j = 1; j <= m; j++) g[i][j] = s[j - 1];
  }
  int dr[] = {-1, 0, 1, 1, 1, 0, -1, -1};
  int dc[] = {-1, -1, -1, 0, 1, 1, 1, 0};
  vector<vector<int> > dis(n + 2, vector<int>(m + 2, -1));
  queue<pair<int, int> > q;
  for (int i = 0; i <= n + 1; i++) {
    for (int j = 0; j <= m + 1; j++) {
      if (g[i][j] == '.') {
        dis[i][j] = 0;
        q.push(pair<int, int>(i, j));
      }
    }
  }
  while (!q.empty()) {
    pair<int, int> u = q.front();
    q.pop();
    int ur = u.first;
    int uc = u.second;
    for (int j = 0; j < 8; j++) {
      int vr = ur + dr[j];
      int vc = uc + dc[j];
      if (vr >= 0 && vr <= n + 1 && vc >= 0 && vc <= m + 1) {
        if (dis[vr][vc] == -1) {
          dis[vr][vc] = dis[ur][uc] + 1;
          q.push(pair<int, int>(vr, vc));
        }
      }
    }
  }
  int low = 1;
  int high = 1;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) high = max(high, dis[i][j]);
  int ans = 0;
  vector<vector<int> > re;
  vector<vector<int> > dis2(n + 2, vector<int>(m + 2, -1));
  while (low <= high) {
    int mid = (low + high) / 2;
    queue<pair<int, int> > q;
    for (int i = 0; i <= n + 1; i++) {
      for (int j = 0; j <= m + 1; j++) {
        if (dis[i][j] >= mid) {
          dis2[i][j] = 0;
          q.push(pair<int, int>(i, j));
        } else
          dis2[i][j] = -1;
      }
    }
    if (q.empty()) {
      high = mid - 1;
      continue;
    }
    while (!q.empty()) {
      pair<int, int> u = q.front();
      q.pop();
      int ur = u.first;
      int uc = u.second;
      for (int j = 0; j < 8; j++) {
        int vr = ur + dr[j];
        int vc = uc + dc[j];
        if (vr >= 0 && vr <= n + 1 && vc >= 0 && vc <= m + 1) {
          if (dis2[vr][vc] == -1) {
            dis2[vr][vc] = dis2[ur][uc] + 1;
            q.push(pair<int, int>(vr, vc));
          }
        }
      }
    }
    bool flag = true;
    for (int i = 0; i <= n + 1; i++) {
      for (int j = 0; j <= m + 1; j++) {
        if (dis2[i][j] >= mid) {
          if (g[i][j] == 'X') flag = false;
        } else if (g[i][j] == '.')
          flag = false;
      }
    }
    if (flag) {
      ans = mid;
      low = mid + 1;
    } else
      high = mid - 1;
  }
  cout << ans - 1 << endl;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (dis[i][j] >= ans)
        putchar('X');
      else
        putchar('.');
    }
    puts("");
  }
  return 0;
}
