#include <bits/stdc++.h>
using namespace std;
vector<long long> f[1000003], sum[1000003], dd[1000003], a[1000003],
    resu[1000003];
int n, m, res, tt;
void inp() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  freopen(
      ""
      ".inp",
      "r", stdin);
  freopen(
      ""
      ".out",
      "w", stdout);
}
long long calcul(int i, int j, int t, long long k) {
  if (k == 0) {
    int x = i + t, y = j + t;
    i = i - t;
    j = j - t;
    if (x > n || y > m || i < 1 || j < 1) return -1;
    return f[x][y] - f[i - 1][y] - f[x][j - 1] + f[i - 1][j - 1];
  } else {
    long long x = min(i + t, n), y = min(j + t, m);
    i = max(i - t, 1);
    j = max(j - t, 1);
    return sum[x][y] - sum[i - 1][y] - sum[x][j - 1] + sum[i - 1][j - 1];
  }
}
bool check(long long x) {
  for (long long i = 1; i <= n; i++)
    for (long long j = 1; j <= m; j++) {
      sum[i][j] = 0;
      dd[i][j] = 0;
    }
  for (long long i = 1; i <= n; i++)
    for (long long j = 1; j <= m; j++) {
      if (calcul(i, j, x, 0) == (2 * x + 1) * (2 * x + 1)) {
        dd[i][j] = 1;
      }
      sum[i][j] = sum[i - 1][j] + sum[i][j - 1] + dd[i][j] - sum[i - 1][j - 1];
    }
  for (long long i = 1; i <= n; i++)
    for (long long j = 1; j <= m; j++) {
      if (calcul(i, j, x, 1) == 0 && a[i][j] == 1) return false;
    }
  return true;
}
void memo() {
  for (long long i = 1; i <= n; i++)
    for (long long j = 1; j <= m; j++) resu[i][j] = dd[i][j];
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  char x;
  cin >> n >> m;
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= m; j++) {
      f[i].push_back(0);
      a[i].push_back(0);
      resu[i].push_back(0);
      dd[i].push_back(0);
      sum[i].push_back(0);
    }
  for (long long i = 1; i <= n; i++)
    for (long long j = 1; j <= m; j++) {
      cin >> x;
      if (x == 'X') a[i][j] = 1;
    }
  for (long long i = 1; i <= n; i++)
    for (long long j = 1; j <= m; j++) {
      f[i][j] = f[i - 1][j] + f[i][j - 1] + a[i][j] - f[i - 1][j - 1];
    }
  long long l = 0, r = min(n, m);
  long long d = 0;
  while (l <= r) {
    if (check(((l + r) / 2)) == true) {
      memo();
      res = ((l + r) / 2);
      l = ((l + r) / 2) + 1;
    } else
      r = ((l + r) / 2) - 1;
    d++;
  }
  cout << res << '\n';
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= m; j++)
      if (resu[i][j] == 1)
        cout << "X";
      else
        cout << ".";
    cout << '\n';
  }
  return 0;
}
