#include <bits/stdc++.h>
using namespace std;
int N, M;
vector<int> a[1000005], cent[1000005], acmd[1000005], acmr[1000005];
vector<int> ps[1000005];
void calcDps() {
  for (int i = 1; i <= N; i++)
    for (int j = 1; j <= M; j++)
      ps[i][j] = a[i][j] + ps[i - 1][j] + ps[i][j - 1] - ps[i - 1][j - 1];
}
bool full(int xl, int yl, int xr, int yr) {
  if (xl < 1 or yl < 1 or xr > N or yr > M) return false;
  int sum = ps[xr][yr] - ps[xl - 1][yr] - ps[xr][yl - 1] + ps[xl - 1][yl - 1];
  return sum == (xr - xl + 1) * (yr - yl + 1);
}
bool can(int x) {
  for (int i = 1; i <= N; i++)
    for (int j = 1; j <= M; j++) {
      cent[i][j] = 0;
      acmd[i][j] = 0;
      acmr[i][j] = 0;
    }
  for (int i = 1; i <= N; i++)
    for (int j = 1; j <= M; j++)
      if (full(i - x, j - x, i + x, j + x)) {
        cent[i][j] = 1;
        acmd[i - x][j - x]++;
        acmd[i + x + 1][j - x]--;
      }
  for (int j = 1; j <= M; j++) {
    int acm = 0;
    for (int i = 1; i <= N; i++) {
      acm += acmd[i][j];
      if (acm > 0 && !a[i][j]) return false;
      if (acm > 0) {
        acmr[i][j]++;
        acmr[i][j + 2 * x + 1]--;
      }
    }
  }
  for (int i = 1; i <= N; i++) {
    int acm = 0;
    for (int j = 1; j <= M; j++) {
      acm += acmr[i][j];
      if (acm > 0 && !a[i][j]) return false;
      if (!acm && a[i][j]) return false;
    }
  }
  return true;
}
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cin >> N >> M;
  for (int j = 0; j <= M + 1; j++) {
    a[0].push_back(0);
    acmd[0].push_back(0);
    acmr[0].push_back(0);
    cent[0].push_back(0);
    ps[0].push_back(0);
  }
  for (int i = 1; i <= N; i++) {
    a[i].push_back(0);
    acmd[i].push_back(0);
    acmr[i].push_back(0);
    cent[i].push_back(0);
    ps[i].push_back(0);
    for (int j = 1; j <= M; j++) {
      char x;
      cin >> x;
      a[i].push_back(x == 'X');
      acmd[i].push_back(0);
      acmr[i].push_back(0);
      cent[i].push_back(0);
      ps[i].push_back(0);
    }
    a[i].push_back(0);
    acmd[i].push_back(0);
    acmr[i].push_back(0);
    cent[i].push_back(0);
    ps[i].push_back(0);
  }
  for (int j = 0; j <= M + 1; j++) {
    a[N + 1].push_back(0);
    acmd[N + 1].push_back(0);
    acmr[N + 1].push_back(0);
    cent[N + 1].push_back(0);
    ps[N + 1].push_back(0);
  }
  calcDps();
  if (!can(1)) {
    cout << "0\n";
    for (int i = 1; i <= N; i++) {
      for (int j = 1; j <= M; j++) cout << (a[i][j] ? 'X' : '.');
      cout << "\n";
    }
    exit(0);
  }
  int l = 1;
  int r = (N - 1) / 2;
  while (l < r) {
    int mid = (l + r + 1) / 2;
    if (can(mid))
      l = mid;
    else
      r = mid - 1;
  }
  can(l);
  cout << l << "\n";
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= M; j++) cout << (cent[i][j] ? 'X' : '.');
    cout << "\n";
  }
  return 0;
}
