#include <bits/stdc++.h>
using namespace std;
namespace zzc {
const int maxn = 1e6 + 5;
int n, m;
vector<int> sum[maxn], tot[maxn];
vector<bool> ans[maxn];
char ch[maxn];
bool check(int x) {
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      tot[i][j] = 0;
    }
  }
  for (int i = x + 1; i <= n - x; i++) {
    for (int j = x + 1; j <= m - x; j++) {
      if (sum[i + x][j + x] - sum[i - x - 1][j + x] - sum[i + x][j - x - 1] +
              sum[i - x - 1][j - x - 1] ==
          (2 * x + 1) * (2 * x + 1)) {
        tot[i + x + 1][j + x + 1] += 1;
        tot[i - x][j + x + 1] += -1;
        tot[i + x + 1][j - x] += -1;
        tot[i - x][j - x] += 1;
      }
    }
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (tot[i][j] += tot[i - 1][j] + tot[i][j - 1] - tot[i - 1][j - 1]) {
        cnt++;
      }
    }
  }
  return cnt == sum[n][m];
}
void work() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i <= n + 1; i++) {
    for (int j = 0; j <= m + 1; j++) {
      sum[i].push_back(0);
      tot[i].push_back(0);
      ans[i].push_back(false);
    }
  }
  for (int i = 1; i <= n; i++) {
    scanf("%s", ch + 1);
    for (int j = 1; j <= m; j++) {
      sum[i][j] = (ch[j] == 'X' ? 1 : 0);
      sum[i][j] += sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];
    }
  }
  int mid, l = 0, r = (min(n, m) - 1) / 2, res;
  while (l <= r) {
    mid = (l + r) >> 1;
    if (check(mid)) {
      res = mid;
      l = mid + 1;
    } else
      r = mid - 1;
  }
  printf("%d\n", res);
  for (int i = res + 1; i <= n - res; i++) {
    for (int j = res + 1; j <= m - res; j++) {
      if (sum[i + res][j + res] - sum[i - res - 1][j + res] -
              sum[i + res][j - res - 1] + sum[i - res - 1][j - res - 1] ==
          (2 * res + 1) * (2 * res + 1)) {
        ans[i][j] = true;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      putchar((ans[i][j] ? 'X' : '.'));
    }
    puts(" ");
  }
}
}  // namespace zzc
int main() {
  zzc::work();
  return 0;
}
