#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
int power(int first, unsigned int second, unsigned int m) {
  if (second == 0) return 1;
  int p = power(first, second / 2, m) % m;
  p = (p * p) % m;
  return (second % 2 == 0) ? p : (first * p) % m;
}
int modInverse(int a, int m) {
  { return power(a, m - 2, m); }
}
const int maxn = 1000003;
char *a[maxn];
int n, m;
int *f[maxn];
int *pre[maxn];
bool *place[maxn];
bool check(int l) {
  l = l << 1 | 1;
  for (int i = 0; i <= n + 1; i++) {
    for (int j = 0; j <= m; j++) {
      f[i][j] = 0;
      place[i][j] = 0;
    }
  }
  for (int i = 1; i <= n - l + 1; i++) {
    for (int j = 1; j <= m - l + 1; j++) {
      if (pre[i + l - 1][j + l - 1] - pre[i + l - 1][j - 1] -
              pre[i - 1][j + l - 1] + pre[i - 1][j - 1] ==
          l * l) {
        place[i + (l >> 1)][j + (l >> 1)] = 1;
        f[i][j]++;
        f[i + l][j]--;
        f[i][j + l]--;
        f[i + l][j + l]++;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      f[i][j] += f[i - 1][j] + f[i][j - 1] - f[i - 1][j - 1];
      if (!f[i][j] && a[i][j] == 'X') return false;
    }
  }
  return true;
}
void solve() {
  cin >> n >> m;
  char p;
  for (int i = 0; i <= n + 3; i++) {
    f[i] = new int[m + 3];
    place[i] = new bool[m + 3];
    pre[i] = new int[m + 3];
    a[i] = new char[m + 4];
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];
      pre[i][j] = 0;
      if (a[i][j] == 'X') pre[i][j] = 1;
      pre[i][j] += pre[i - 1][j] + pre[i][j - 1] - pre[i - 1][j - 1];
    }
  }
  int l = 0, r = min(n, m);
  int ans = 0;
  while (l <= r) {
    int mid = l + r >> 1;
    if (check(mid)) {
      ans = mid;
      l = mid + 1;
    } else
      r = mid - 1;
  }
  check(ans);
  cout << ans << "\n";
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (place[i][j])
        cout << 'X';
      else
        cout << '.';
    }
    cout << "\n";
  }
}
signed main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
  return 0;
}
