#include <bits/stdc++.h>
using namespace std;
void GO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
}
const long long INF = 1e18;
const long long mod = (1e9) + 7;
const int maxn = (int)1 << 20;
mt19937 rnd(time(0));
int n, m;
vector<string> a;
vector<vector<int> > dp, val, pref;
int get_sum(int lx, int ly, int rx, int ry) {
  if (lx <= 0 || ly <= 0 || rx > n || ry > m) return 0;
  return (pref[rx][ry] - pref[rx][ly - 1] - pref[lx - 1][ry] +
          pref[lx - 1][ly - 1]) == (rx - lx + 1) * (ry - ly + 1);
}
void add(int lx, int ly, int rx, int ry) {
  val[lx][ly]++, val[lx][ry + 1]--;
  val[rx + 1][ly]--, val[rx + 1][ry + 1]++;
}
int f(int x) {
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) val[i][j] = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      if (get_sum(i - x, j - x, i + x, j + x)) add(i - x, j - x, i + x, j + x);
  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] + val[i][j];
      if (a[i][j] == 'X' && !dp[i][j]) return 0;
    }
  }
  return 1;
}
void solve() {
  cin >> n >> m;
  for (int i = 0; i <= n + 1; ++i) {
    a.push_back({}), pref.push_back({});
    val.push_back({}), dp.push_back({});
    for (int j = 0; j <= m + 1; ++j) {
      a.back().push_back('?');
      pref.back().push_back(0);
      val.back().push_back(0);
      dp.back().push_back(0);
    }
  }
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) cin >> a[i][j];
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      pref[i][j] = pref[i - 1][j] + pref[i][j - 1] - pref[i - 1][j - 1];
      if (a[i][j] == 'X') pref[i][j]++;
    }
  }
  int lef = -1, righ = max(n, m) + 1;
  while (righ - lef > 1) {
    int mid = (righ + lef) / 2;
    if (f(mid))
      lef = mid;
    else
      righ = mid;
  }
  cout << lef << '\n';
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (get_sum(i - lef, j - lef, i + lef, j + lef))
        cout << 'X';
      else
        cout << '.';
    }
    cout << '\n';
  }
}
signed main() {
  GO();
  int Q = 1;
  while (Q--) solve();
}
