#include <bits/stdc++.h>
using namespace std;
mt19937 rng(time(0));
uniform_int_distribution<int> uid(-1e9, 1e9);
int n, m;
vector<string> a;
vector<vector<long long>> p, v, dp;
int f(int lx, int ly, int rx, int ry) {
  if (lx <= 0 || ly <= 0 || rx > n || ry > m) return 0;
  return (p[rx][ry] - p[rx][ly - 1] - p[lx - 1][ry] + p[lx - 1][ly - 1]) ==
         (rx - lx + 1) * (ry - ly + 1);
}
int check(int x) {
  for (int i = 1; i <= n; ++i) fill((v[i]).begin(), (v[i]).end(), 0);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      if (f(i - x, j - x, i + x, j + x))
        ++v[i - x][j - x], --v[i - x][j + x + 1], --v[i + x + 1][j - x],
            ++v[i + x + 1][j + x + 1];
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) {
      dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] + v[i][j];
      if (a[i - 1][j - 1] == 'X' && dp[i][j] == 0) return 0;
    }
  return 1;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  a.resize(n);
  dp.assign(n + 2, vector<long long>(m + 2, 0));
  p.assign(n + 2, vector<long long>(m + 2, 0));
  v.assign(n + 2, vector<long long>(m + 2, 0));
  for (auto& i : a) cin >> i;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      p[i][j] = p[i - 1][j] + p[i][j - 1] - p[i - 1][j - 1] +
                (a[i - 1][j - 1] == 'X');
  int l = -1, r = max(n, m) + 1;
  while (l + 1 != r) {
    int m = (l + r) / 2;
    (check(m) ? l : r) = m;
  }
  cout << l << '\n';
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j)
      cout << (f(i - l, j - l, i + l, j + l) ? 'X' : '.');
    cout << '\n';
  }
}
