#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
string g[N];
int n, m;
vector<int> dp[N];
int get(int x1, int y1, int x2, int y2) {
  return dp[x2][y2] - dp[x1 - 1][y2] - dp[x2][y1 - 1] + dp[x1 - 1][y1 - 1];
}
vector<int> d[N];
int dr[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dc[] = {0, 1, 1, 1, 0, -1, -1, -1};
string res[N];
bool check(int t, bool ned) {
  int x1, y1, x2, y2;
  queue<pair<int, int> > q;
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= m; j++) d[i][j] = 1e8;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      x1 = i - t, y1 = j - t, x2 = i + t, y2 = j + t;
      if (min(x1, y1) <= 0) continue;
      if (x2 > n || y2 > m) continue;
      int val = get(x1, y1, x2, y2);
      int area = (x2 - x1 + 1) * (y2 - y1 + 1);
      if (area != val) continue;
      if (ned) res[i - 1][j - 1] = 'X';
      q.push({i, j});
      d[i][j] = 0;
    }
  }
  while (q.size()) {
    int r = q.front().first;
    int c = q.front().second;
    q.pop();
    for (int i = 0; i < 8; i++) {
      int nr = r + dr[i];
      int nc = c + dc[i];
      if (min(nr, nc) <= 0 || nr > n || nc > m) continue;
      if (d[nr][nc] > d[r][c] + 1) {
        d[nr][nc] = d[r][c] + 1;
        q.push({nr, nc});
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (g[i][j - 1] == 'X' && d[i][j] > t) return 0;
      if (g[i][j - 1] == '.' && d[i][j] <= t) return 0;
    }
  }
  return 1;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  dp[0].resize(m + 2, 0);
  for (int i = 1; i <= n; i++) {
    cin >> g[i];
    dp[i].resize(m + 2, 0);
    for (int j = 0; j < m; j++) {
      dp[i][j + 1] = (g[i][j] == 'X');
      dp[i][j + 1] += dp[i - 1][j + 1];
      dp[i][j + 1] += dp[i][j];
      dp[i][j + 1] -= dp[i - 1][j];
    }
  }
  for (int i = 0; i <= n; i++) d[i].resize(m + 2);
  int s = 0, e = n * m, ans = 0;
  while (s <= e) {
    int mid = s + e >> 1;
    if (check(mid, 0))
      s = mid + 1, ans = mid;
    else
      e = mid - 1;
  }
  cout << ans << endl;
  for (int i = 0; i < n; i++) res[i].resize(m, '.');
  check(ans, 1);
  for (int i = 0; i < n; i++) cout << res[i] << '\n';
  return 0;
}
