#include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream& operator<<(ostream& o, const vector<T>& v) {
  int b = 0;
  for (const auto& a : v) o << (b++ ? ", " : "") << a;
  return o;
}
template <typename T>
ostream& operator<<(ostream& o, const set<T>& v) {
  int b = 0;
  for (const auto& a : v) o << (b++ ? ", " : "") << a;
  return o;
}
template <typename T, typename U>
ostream& operator<<(ostream& o, const pair<T, U>& p) {
  o << p.first << ' ' << p.second;
  return o;
}
vector<vector<char>> D;
vector<vector<int>> pref;
pair<int, int> marked[1000000];
int len_marked;
int N, M;
vector<vector<bool>> seen;
int f(int i, int j) {
  if (i < 0 || j < 0) return 0;
  auto& x = pref[i][j];
  if (x != -1) return x;
  return x = f(i - 1, j) + f(i, j - 1) - f(i - 1, j - 1) + (D[i][j] == 'X');
}
int size(int t) { return 2 * (t + 1) - 1; }
int in_region(int x, int y, int t) {
  int s = size(t);
  return pref[x][y] - ((x - s) >= 0 ? pref[x - s][y] : 0) -
         ((y - s) >= 0 ? pref[x][y - s] : 0) +
         (((x - s) >= 0 && (y - s) >= 0) ? pref[x - s][y - s] : 0);
}
pair<int, int> center(int T, pair<int, int> coord) {
  int q = size(T) / 2;
  return {coord.first - q, coord.second - q};
}
bool in_bounds(int r, int c) { return r >= 0 && c >= 0 && r < N && c < M; }
int run_bfs(int t) {
  int n_visited = 0;
  queue<pair<int, pair<int, int>>> Q;
  seen.assign(N, vector<bool>(M, false));
  for (int i = 0; i < len_marked; i++) {
    Q.push({0, marked[i]});
  }
  while (!Q.empty()) {
    auto [i, p] = Q.front();
    Q.pop();
    auto [r, c] = p;
    if (i > t) break;
    if (seen[r][c]) continue;
    seen[r][c] = true;
    n_visited++;
    for (int _r : {-1, 0, 1}) {
      for (int _c : {-1, 0, 1}) {
        if (in_bounds(r + _r, c + _c) && !seen[r + _r][c + _c])
          Q.push({i + 1, {r + _r, c + _c}});
      }
    }
  }
  return n_visited;
}
bool check(int T, int count) {
  len_marked = 0;
  int s = size(T) * size(T);
  int i = 0;
  for (int r = 0; r < N; r++) {
    for (int c = 0; c < M; c++) {
      if (in_region(r, c, T) == s) {
        marked[i++] = center(T, {r, c});
        len_marked++;
      }
    }
  }
  int b = run_bfs(T);
  return b == count;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> N >> M;
  D = vector<vector<char>>(N, vector<char>(M));
  pref = vector<vector<int>>(N, vector<int>(M, -1));
  seen.resize(N, vector<bool>(M));
  int count = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      cin >> D[i][j];
      if (D[i][j] == 'X') count++;
    }
  }
  f(N - 1, M - 1);
  int lo = 1, hi = min(N, M);
  while (hi - lo > 2) {
    int mid = (hi + lo) / 2;
    (check(mid, count) ? lo : hi) = mid;
  }
  while (check(lo, count)) lo++;
  lo--;
  check(lo, count);
  D = vector<vector<char>>(N, vector<char>(M, '.'));
  for (int i = 0; i < len_marked; i++) {
    D[marked[i].first][marked[i].second] = 'X';
  }
  cout << lo << endl;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      cout << D[i][j];
    }
    cout << '\n';
  }
}
