#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
long long N = 1234567891, M = 31;
long long n, m, t, k, f, mi = 2e9, x, y, z, ma, x3, x2, y2, l, r, i, j, ans, vv;
char c[1010000];
string s;
vector<long long> v, v2, v3;
vector<long long> a[1100000], b[1100000], d, d2, dp[1100000];
int main() {
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++) {
    scanf("%s", c);
    s = c;
    a[i].push_back(0);
    for (j = 1; j <= m; j++) {
      a[i].push_back((s[j - 1] == 'X'));
    }
  }
  for (i = 0; i <= n + 1; i++)
    for (j = 0; j <= m + 1; j++) {
      b[i].push_back(0);
      dp[i].push_back(0);
    }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      b[i][j] = a[i][j] + b[i - 1][j] + b[i][j - 1] - b[i - 1][j - 1];
    }
  }
  long long l = 0, r = min((n - 1) / 2, (m - 1) / 2) + 1;
  while (r - l > 1) {
    int t2 = (l + r) / 2;
    int t = t2 * 2 + 1;
    for (i = 0; i <= n + 1; i++) {
      for (j = 0; j <= m + 1; j++) {
        dp[i][j] = 0;
      }
    }
    for (i = t; i <= n; i++) {
      for (j = t; j <= m; j++) {
        if (b[i][j] - b[i - t][j] - b[i][j - t] + b[i - t][j - t] == t * t) {
          dp[i - t + 1][j - t + 1]++;
          dp[i - t + 1][j + 1]--;
          dp[i + 1][j - t + 1]--;
          dp[i + 1][j + 1]++;
        }
      }
    }
    f = 0;
    for (i = 1; i <= n; i++) {
      for (j = 1; j <= m; j++) {
        dp[i][j] = dp[i][j] + dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1];
        if (dp[i][j] && !a[i][j]) f = 1;
        if (!dp[i][j] && a[i][j]) f = 1;
      }
    }
    if (!f)
      l = t2;
    else
      r = t2;
  }
  cout << l << endl;
  t = l * 2 + 1;
  for (i = 0; i <= n + 1; i++) {
    for (j = 0; j <= m + 1; j++) {
      dp[i][j] = 0;
    }
  }
  for (i = t; i <= n; i++) {
    for (j = t; j <= m; j++) {
      if (b[i][j] - b[i - t][j] - b[i][j - t] + b[i - t][j - t] == t * t) {
        dp[i - l][j - l] = 1;
      }
    }
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      if (dp[i][j])
        printf("X");
      else
        printf(".");
    }
    printf("\n");
  }
}
