#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
int N, M;
vector<pii> cur, nxt;
inline bool Ok(int ni, int nj) {
  return ni >= 0 and ni < N and nj >= 0 and nj < M;
}
inline vector<vector<char>> Forward(vector<vector<char>> f, int t) {
  cur.clear();
  for (int i(0); i < N; ++i) {
    for (int j(0); j < M; ++j) {
      if (f[i][j]) {
        cur.emplace_back(i, j);
      }
    }
  }
  while (t--) {
    nxt.clear();
    for (auto [i, j] : cur) {
      for (int di(-1); di <= 1; ++di) {
        for (int dj(-1); dj <= 1; ++dj) {
          int ni(i + di), nj(j + dj);
          if (!Ok(ni, nj) or f[ni][nj]) continue;
          f[ni][nj] = true;
          nxt.emplace_back(ni, nj);
        }
      }
    }
    cur.swap(nxt);
  }
  return f;
}
inline vector<vector<char>> Backward(vector<vector<char>> f, int t) {
  cur.clear();
  for (int i(-1); i <= N; ++i) {
    cur.emplace_back(i, -1);
    cur.emplace_back(i, M);
  }
  for (int j(-1); j <= M; ++j) {
    cur.emplace_back(-1, j);
    cur.emplace_back(N, j);
  }
  for (int i(0); i < N; ++i) {
    for (int j(0); j < M; ++j) {
      if (f[i][j]) continue;
      cur.emplace_back(i, j);
    }
  }
  while (t--) {
    nxt.clear();
    for (auto [i, j] : cur) {
      for (int di(-1); di <= 1; ++di) {
        for (int dj(-1); dj <= 1; ++dj) {
          int ni(i + di), nj(j + dj);
          if (Ok(ni, nj) and f[ni][nj]) {
            f[ni][nj] = false;
            nxt.emplace_back(ni, nj);
          }
        }
      }
    }
    cur.swap(nxt);
  }
  return f;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> N >> M;
  vector<vector<char>> f(N, vector<char>(M));
  for (int i(0); i < N; ++i) {
    for (int j(0); j < M; ++j) {
      char c;
      cin >> c;
      f[i][j] = c == 'X';
    }
  }
  int l(0), r(min(N, M));
  while (l + 1 < r) {
    int m((l + r) / 2);
    if (Forward(Backward(f, m), m) == f)
      l = m;
    else
      r = m;
  }
  cout << l << '\n';
  for (auto& line : Backward(f, l)) {
    for (auto x : line) cout << (x ? 'X' : '.');
    cout << '\n';
  }
}
