#include <bits/stdc++.h>
using namespace std;
template <typename T>
void maxtt(T& t1, T t2) {
  t1 = max(t1, t2);
}
template <typename T>
void mintt(T& t1, T t2) {
  t1 = min(t1, t2);
}
bool debug = 0;
int n, m, k;
string direc = "URDL";
const long long MOD2 = (long long)1000000007 * (long long)1000000007;
long long ln, lk, lm;
void etp(bool f = 0) {
  puts(f ? "YES" : "NO");
  exit(0);
}
void addmod(int& x, int y, int mod = 1000000007) {
  x += y;
  if (x >= mod) x -= mod;
  if (x < 0) x += mod;
  assert(x >= 0 && x < mod);
}
void et(int x = -1) {
  printf("%d\n", x);
  exit(0);
}
long long fastPow(long long x, long long y, int mod = 1000000007) {
  long long ans = 1;
  while (y > 0) {
    if (y & 1) ans = (x * ans) % mod;
    x = x * x % mod;
    y >>= 1;
  }
  return ans;
}
long long gcd1(long long x, long long y) { return y ? gcd1(y, x % y) : x; }
char s[1000035];
vector<int> mp[1000035], ps[1000035], ot[1000035];
int area(int a, int b, int c, int d) {
  return mp[c][d] - mp[a - 1][d] - mp[c][b - 1] + mp[a - 1][b - 1];
}
bool ck(int T, bool fg) {
  int len = T * 2 + 1;
  for (int(i) = 1; (i) <= (int)(n); (i)++)
    for (int(j) = 1; (j) <= (int)(m); (j)++) ps[i][j] = 0;
  for (int(i) = 1; (i) <= (int)(n + 1 - len); (i)++)
    for (int(j) = 1; (j) <= (int)(m + 1 - len); (j)++)
      if (area(i, j, i, j) == 1) {
        if (area(i, j, i + len - 1, j + len - 1) == len * len) {
          if (fg) {
            ot[i + T][j + T] = 1;
          }
          ps[i][j]++;
          ps[i][j + len]--;
          ps[i + len][j]--;
          ps[i + len][j + len]++;
        }
      }
  for (int(i) = 1; (i) <= (int)(n); (i)++) {
    for (int(j) = 1; (j) <= (int)(m); (j)++)
      ps[i][j] += ps[i][j - 1] + ps[i - 1][j] - ps[i - 1][j - 1];
  }
  for (int(i) = 1; (i) <= (int)(n); (i)++)
    for (int(j) = 1; (j) <= (int)(m); (j)++) {
      bool isx = area(i, j, i, j);
      bool isy = ps[i][j] >= 1;
      if (isx != isy) return 0;
    }
  return 1;
}
void fmain(int tid) {
  scanf("%d%d", &n, &m);
  for (int(i) = 0; (i) < (int)(n + 2); (i)++) {
    mp[i].resize(m + 5, 0);
    ps[i].resize(m + 5, 0);
    ot[i].resize(m + 5, 0);
  }
  for (int(i) = 1; (i) <= (int)(n); (i)++) {
    scanf("%s", s + 1);
    for (int(j) = 1; (j) <= (int)(m); (j)++)
      if (s[j] == 'X') mp[i][j] = 1;
    for (int(j) = 1; (j) <= (int)(m); (j)++)
      mp[i][j] += mp[i][j - 1] + mp[i - 1][j] - mp[i - 1][j - 1];
  }
  int l = 0, r = max(m, n);
  while (l + 1 < r) {
    int mid = (l + r) / 2;
    if (ck(mid, 0))
      l = mid;
    else
      r = mid - 1;
  }
  int ans = ck(r, 0) ? r : l;
  printf("%d\n", ans);
  ck(ans, 1);
  for (int(i) = 1; (i) <= (int)(n); (i)++) {
    for (int(j) = 1; (j) <= (int)(m); (j)++) printf("%c", ot[i][j] ? 'X' : '.');
    puts("");
  }
}
int main() {
  int t = 1;
  for (int(i) = 1; (i) <= (int)(t); (i)++) {
    fmain(i);
  }
  return 0;
}
