#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3fll;
const int N = 1e6 + 10;
const int M = 1e4 + 10;
const int maxn = 1e6 + 10;
const int mod = (int)1e9 + 7;
const double eps = 1e-10;
const double pi = acos(-1.0);
int n, m, cnt;
vector<string> s;
vector<vector<int> > a, b;
vector<pair<int, int> > res;
const int dx[] = {0, 0, -1, 1, 1, 1, -1, -1};
const int dy[] = {-1, 1, 0, 0, 1, -1, 1, -1};
bool in(int x, int y) { return 0 <= x && x < n && 0 <= y && y < m; }
queue<pair<int, int> > Q;
bool bfs(int mx) {
  for (int i = 0; i < n; i++) {
    for (int &val : b[i]) val = -1;
  }
  for (pair<int, int> &p : res) {
    b[p.first][p.second] = 0;
    Q.emplace(p.first, p.second);
  }
  int ccc = ((int)res.size());
  while (!Q.empty()) {
    pair<int, int> t = Q.front();
    Q.pop();
    if (b[t.first][t.second] == mx) continue;
    for (int i = 0; i < 8; i++) {
      int x = t.first + dx[i];
      int y = t.second + dy[i];
      if (in(x, y) && b[x][y] == -1 && s[x][y] == 'X') {
        b[x][y] = b[t.first][t.second] + 1;
        Q.emplace(x, y);
        ccc++;
      }
    }
  }
  return ccc == cnt;
}
bool check(int x) {
  int y = x << 1 | 1;
  if (y > min(n, m)) return 0;
  res.clear();
  for (int i = y - 1; i < n; i++) {
    for (int j = y - 1; j < m; j++) {
      int num = a[i][j];
      if (i - y >= 0) num -= a[i - y][j];
      if (j - y >= 0) num -= a[i][j - y];
      if (i - y >= 0 && j - y >= 0) num += a[i - y][j - y];
      if (num == y * y) res.emplace_back(i - x, j - x);
    }
  }
  return bfs(x);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  s.resize(n);
  a.resize(n);
  b.resize(n);
  for (int i = 0; i < n; i++) cin >> s[i];
  for (int i = 0; i < n; i++) {
    a[i].resize(m);
    b[i].resize(m);
    for (int j = 0; j < m; j++) {
      if (s[i][j] == '.')
        a[i][j] = 0;
      else
        a[i][j] = 1, cnt++;
      if (i) a[i][j] += a[i - 1][j];
      if (j) a[i][j] += a[i][j - 1];
      if (i && j) a[i][j] -= a[i - 1][j - 1];
    }
  }
  int l = 0, r = min(n, m) / 2, pos = 0;
  while (l <= r) {
    int mid = ((l + r) >> 1);
    if (check(mid))
      l = mid + 1, pos = mid;
    else
      r = mid - 1;
  }
  cout << pos << '\n';
  check(pos);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) s[i][j] = '.';
  }
  for (pair<int, int> &p : res) s[p.first][p.second] = 'X';
  for (int i = 0; i < n; i++) cout << s[i] << '\n';
  return 0;
}
