#include <bits/stdc++.h>
using namespace std;
vector<string> a;
vector<vector<int> > d, f;
int n, m;
int target;
bool ok(int T) {
  f.resize(a.size());
  for (int i = 0; i < a.size(); ++i) {
    f[i].assign(a[i].size(), int(1e9));
  }
  queue<pair<int, int> > Q;
  for (int i = 0; i < d.size(); ++i) {
    for (int j = 0; j < d[i].size(); ++j) {
      if (d[i][j] > T) {
        Q.push(make_pair(i, j));
        f[i][j] = 0;
      }
    }
  }
  int cnt = 0;
  while (!Q.empty()) {
    int x = Q.front().first;
    int y = Q.front().second;
    Q.pop();
    cnt++;
    if (f[x][y] >= T) continue;
    for (int u = x - 1; u <= x + 1; ++u) {
      for (int v = y - 1; v <= y + 1; ++v)
        if (u != x || v != y) {
          if (u < 0 || v < 0 || u >= a.size() || v >= a[0].size()) continue;
          if (f[u][v] > f[x][y] + 1) {
            f[u][v] = f[x][y] + 1;
            Q.push(make_pair(u, v));
          }
        }
    }
  }
  return cnt >= target;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> m >> n;
  a.resize(m + 2);
  a[0] = string(n + 2, '.');
  for (int i = 1; i <= m; ++i) {
    string s;
    cin >> s;
    a[i] = '.' + s + '.';
    for (char c : s) target += c == 'X';
  }
  a[m + 1] = string(n + 2, '.');
  d.resize(a.size());
  for (int i = 0; i < d.size(); ++i) d[i].assign(a[i].size(), int(1e9));
  queue<pair<int, int> > Q;
  for (int i = 0; i < a.size(); ++i) {
    for (int j = 0; j < a[i].size(); ++j) {
      if (a[i][j] == '.') {
        Q.push(make_pair(i, j));
        d[i][j] = 0;
      }
    }
  }
  while (!Q.empty()) {
    int x = Q.front().first;
    int y = Q.front().second;
    Q.pop();
    for (int u = x - 1; u <= x + 1; ++u) {
      for (int v = y - 1; v <= y + 1; ++v)
        if (u != x || v != y) {
          if (u < 0 || v < 0 || u >= a.size() || v >= a[0].size()) continue;
          if (d[u][v] > d[x][y] + 1) {
            d[u][v] = d[x][y] + 1;
            Q.push(make_pair(u, v));
          }
        }
    }
  }
  int l = 0, r = 1e6 + 6;
  int res = 0;
  while (l <= r) {
    int mid = l + r >> 1;
    if (ok(mid)) {
      res = mid;
      l = mid + 1;
    } else {
      r = mid - 1;
    }
  }
  cout << res << endl;
  ok(res);
  for (int i = 1; i <= m; ++i) {
    for (int j = 1; j <= n; ++j) {
      if (f[i][j] == 0) {
        cout << 'X';
      } else {
        cout << '.';
      }
    }
    cout << '\n';
  }
}
