#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000010;
int n, m;
vector<string> x;
int z[MAXN];
bool Inside(int a, int b) {
  return 0 <= a && a <= n - 1 && 0 <= b && b <= m - 1;
}
int Getp(int a, int b) { return a * m + b; }
bool Verify(const vector<pair<int, int>>& pos, int d) {
  queue<pair<int, int>> q;
  memset(z, 0, sizeof(z));
  for (auto [a, b] : pos) {
    q.push({a, b});
    z[Getp(a, b)] = 1;
  }
  vector<string> y = x;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) y[i][j] = '.';
  }
  while (!q.empty()) {
    auto [a, b] = q.front();
    q.pop();
    y[a][b] = 'X';
    if (z[Getp(a, b)] == d + 1) continue;
    for (int i = -1; i <= 1; ++i) {
      for (int j = -1; j <= 1; ++j) {
        int c = a + i, d = b + j;
        if (!Inside(c, d) || z[Getp(c, d)] > 0) continue;
        z[Getp(c, d)] = z[Getp(a, b)] + 1;
        q.push({c, d});
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    if (x[i] != y[i]) return false;
  }
  return true;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  x = vector<string>(n);
  for (int i = 0; i < n; ++i) cin >> x[i];
  if (n == 1 || m == 1) {
    cout << 0 << endl;
    for (int i = 0; i < n; ++i) cout << x[i] << "\n";
    return 0;
  }
  vector<vector<int>> f(n + 1, vector<int>(m + 1));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (x[i][j] == '.') continue;
      f[i + 1][j + 1] = min(min(f[i][j + 1], f[i + 1][j]), f[i][j]) + 1;
    }
  }
  int l = 0, r = 0;
  vector<pair<int, int>> ans;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (x[i][j] == 'X') ans.push_back({i, j});
      r = max(r, f[i + 1][j + 1] / 2 + 1);
    }
  }
  while (l + 1 < r) {
    int mid = (l + r) / 2;
    bool flag = true;
    vector<pair<int, int>> temp;
    for (int i = 0; i < n; ++i) {
      if (!flag) break;
      for (int j = 0; j < m; ++j) {
        if (x[i][j] == '.') continue;
        int c = i + mid, d = j + mid;
        if (!Inside(c, d) || f[c + 1][d + 1] < mid + mid + 1) continue;
        temp.push_back({i, j});
      }
    }
    if (Verify(temp, mid)) {
      l = mid;
      ans = std::move(temp);
    } else {
      r = mid;
    }
  }
  cout << l << endl;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) x[i][j] = '.';
  }
  for (auto [a, b] : ans) x[a][b] = 'X';
  for (int i = 0; i < n; ++i) cout << x[i] << endl;
  return 0;
}
