#include <bits/stdc++.h>
using namespace std;
bool check(int x, vector<string>& v, bool output) {
  int n = v.size(), m = v[0].size();
  vector<string> out_init(n, string(m, '.'));
  vector<vector<int>> pref(n, vector<int>(m, 0));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (v[i][j] == '.') pref[i][j] += 1;
      if (i > 0) pref[i][j] += pref[i - 1][j];
      if (j > 0) pref[i][j] += pref[i][j - 1];
      if (i > 0 && j > 0) pref[i][j] -= pref[i - 1][j - 1];
    }
  }
  auto try_place = [&](int i, int j) {
    if (i - x < 0 || j - x < 0 || i + x >= n || j + x >= m) return false;
    int s = pref[i + x][j + x];
    if (i - x > 0) s -= pref[i - x - 1][j + x];
    if (j - x > 0) s -= pref[i + x][j - x - 1];
    if (i - x > 0 && j - x > 0) s += pref[i - x - 1][j - x - 1];
    if (s > 0) return false;
    out_init[i][j] = 'X';
    return true;
  };
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      try_place(i, j);
    }
  }
  vector<string> out_fin = out_init;
  vector<vector<int>> dist(n, vector<int>(m, -1));
  vector<pair<int, int>> q;
  auto push = [&](int i, int j, int d) {
    if (d > x) return;
    if (dist[i][j] != -1) return;
    dist[i][j] = d;
    out_fin[i][j] = 'X';
    q.emplace_back(i, j);
  };
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (out_init[i][j] == 'X') {
        push(i, j, 0);
      }
    }
  }
  for (int at = 0; at < (int)q.size(); ++at) {
    int i, j;
    tie(i, j) = q[at];
    for (int di = -1; di <= 1; ++di) {
      for (int dj = -1; dj <= 1; ++dj) {
        push(i + di, j + dj, dist[i][j] + 1);
      }
    }
  }
  if (output) {
    assert(out_fin == v);
    for (int i = 0; i < n; ++i) {
      cout << out_init[i] << '\n';
    }
  }
  return out_fin == v;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<string> v(n);
  for (int i = 0; i < n; ++i) cin >> v[i];
  int ans = 0;
  int init = 1;
  while (init < min(n, m)) init *= 2;
  for (int step = init; step; step /= 2) {
    if (ans + step > n && ans + step > m) continue;
    if (check(ans + step, v, false)) {
      ans += step;
    }
  }
  cout << ans << '\n';
  check(ans, v, true);
  return 0;
}
