#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<string> board;
vector<int> acmap;
bool check(int mid) {
  int sub = mid * 2;
  long long need = (sub + 1) * (long long)(sub + 1);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (board[i][j] != 'X') continue;
      int neighbors = 0;
      bool N = false, W = false, S = false, E = false;
      if (i > 0) N = (board[i - 1][j] == 'X');
      if (j > 0) W = (board[i][j - 1] == 'X');
      if (i + 1 < n) S = (board[i + 1][j] == 'X');
      if (j + 1 < m) E = (board[i][j + 1] == 'X');
      neighbors = (N ? 1 : 0) + (W ? 1 : 0) + (S ? 1 : 0) + (E ? 1 : 0);
      if (neighbors <= 1) return false;
      if (neighbors >= 3) continue;
      if (N && S) return false;
      if (W && E) return false;
      int r1, c1, r2, c2;
      tie(r2, c2) = make_pair(i, j);
      if (N && W) {
        tie(r1, c1) = make_pair(i - sub, j - sub);
      }
      if (N && E) {
        tie(r1, c1) = make_pair(i - sub, j + sub);
      }
      if (S && W) {
        tie(r1, c1) = make_pair(i + sub, j - sub);
      }
      if (S && E) {
        tie(r1, c1) = make_pair(i + sub, j + sub);
      }
      if (r1 < 0 || c1 < 0 || r2 < 0 || c2 < 0) return false;
      if (r1 >= n || r2 >= n || c1 >= m || c2 >= m) return false;
      if (r1 > r2) swap(r1, r2);
      if (c1 > c2) swap(c1, c2);
      int summation = acmap[(r2 + 1) * (m + 1) + (c2 + 1)] -
                      acmap[(r1) * (m + 1) + (c2 + 1)] -
                      acmap[(r2 + 1) * (m + 1) + (c1)] +
                      acmap[(r1) * (m + 1) + (c1)];
      if (summation != need) return false;
    }
  }
  return true;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n >> m;
  board.resize(n);
  for (int i = 0; i < n; i++) cin >> board[i];
  acmap.resize((n + 1) * (m + 1));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int cur = ((board[i][j] == 'X') ? 1 : 0);
      acmap[(i + 1) * (m + 1) + (j + 1)] =
          cur + acmap[(i + 1) * (m + 1) + (j)] +
          acmap[(i) * (m + 1) + (j + 1)] - acmap[(i) * (m + 1) + (j)];
    }
  }
  int low = 1, high = min(n, m);
  int ans = 0;
  while (low <= high) {
    int mid = (low + high) >> 1;
    if (check(mid)) {
      ans = mid;
      low = mid + 1;
    } else {
      high = mid - 1;
    }
  }
  cout << ans << "\n";
  vector<string> ansm(n, string(m, '.'));
  long long need = (ans * 2 + 1) * (long long)(ans * 2 + 1);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int r2 = i + 2 * ans, c2 = j + 2 * ans;
      if (r2 >= n || c2 >= m) continue;
      int summation = acmap[(r2 + 1) * (m + 1) + (c2 + 1)] -
                      acmap[(r2 + 1) * (m + 1) + (j)] -
                      acmap[(i) * (m + 1) + (c2 + 1)] +
                      acmap[(i) * (m + 1) + (j)];
      if (summation == need) {
        ansm[i + ans][j + ans] = 'X';
      }
    }
  }
  for (int i = 0; i < n; i++) {
    cout << ansm[i] << "\n";
  }
  return 0;
}
