#include <bits/stdc++.h>
using namespace std;
int PREP = (cin.tie(nullptr), ios::sync_with_stdio(false),
            cout << fixed << setprecision(12), 0);
using Int = long long;
const int DX[8] = {-1, 0, +1, -1, +1, -1, 0, +1};
const int DY[8] = {-1, -1, -1, 0, 0, +1, +1, +1};
int main() {
  int N, M;
  cin >> N >> M;
  vector<string> A(N);
  for (auto &s : A) cin >> s;
  vector<vector<int>> B(N, vector<int>(M, 1e9));
  queue<tuple<int, int, int>> Q;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      if (A[i][j] == '.') {
        B[i][j] = 0;
        Q.emplace(0, i, j);
      }
    }
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      if (A[i][j] == 'X' && (i == 0 || i == N - 1 || j == 0 || j == M - 1)) {
        B[i][j] = 1;
        Q.emplace(1, i, j);
      }
    }
  }
  while (!Q.empty()) {
    auto [k, i, j] = Q.front();
    Q.pop();
    for (int t = 0; t < 8; t++) {
      int ni = i + DY[t];
      int nj = j + DX[t];
      if (0 <= ni && ni < N && 0 <= nj && nj < M && B[ni][nj] > k + 1) {
        B[ni][nj] = k + 1;
        Q.emplace(k + 1, ni, nj);
      }
    }
  }
  auto solve = [&](int k) {
    vector<vector<int>> D(N + 1, vector<int>(M + 1));
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < M; j++) {
        if (B[i][j] > k) {
          if (i - k < 0 || j - k < 0 || i + k + 1 > N || j + k + 1 > M)
            return false;
          D[i - k][j - k] += 1;
          D[i - k][j + k + 1] -= 1;
          D[i + k + 1][j - k] -= 1;
          D[i + k + 1][j + k + 1] += 1;
        }
      }
    }
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < M; j++) {
        D[i][j + 1] += D[i][j];
      }
    }
    for (int j = 0; j < M; j++) {
      for (int i = 0; i < N; i++) {
        D[i + 1][j] += D[i][j];
      }
    }
    bool ok = true;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < M; j++) {
        int a = (A[i][j] == 'X');
        int b = (D[i][j] > 0);
        if (a != b) ok = false;
      }
    }
    return ok;
  };
  int ok = 0, ng = min(N, M);
  while (abs(ok - ng) > 1) {
    int mid = (ok + ng) / 2;
    (solve(mid) ? ok : ng) = mid;
  }
  cout << ok << '\n';
  if (ok == 0) {
    for (auto a : A) cout << a << '\n';
    return 0;
  } else {
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < M; j++) {
        cout << (B[i][j] > ok ? 'X' : '.');
      }
      cout << '\n';
    }
  }
  return 0;
}
