#include <bits/stdc++.h>
using namespace std;
const int maxN = 1e6 + 7;
int N, M;
vector<int> mp[maxN], t[maxN];
vector<bool> X[maxN];
char s[maxN];
int Area(int lx, int ly, int rx, int ry) {
  return mp[rx][ry] - mp[lx - 1][ry] - mp[rx][ly - 1] + mp[lx - 1][ly - 1];
}
inline bool In_map(int x, int y) {
  return x >= 1 && y >= 1 && x <= N && y <= M;
}
inline bool check(int lim) {
  for (int i = 0; i <= N + 1; i++) t[i].clear();
  for (int i = 0; i <= M + 1; i++) t[0].push_back(0);
  for (int i = 1; i <= N + 1; i++)
    for (int j = 0; j <= M + 1; j++) t[i].push_back(0);
  int SS = (2 * lim + 1) * (2 * lim + 1);
  for (int i = lim + 1, x1, y1, x2, y2; i + lim <= N; i++) {
    for (int j = lim + 1; j + lim <= M; j++) {
      x1 = i - lim;
      y1 = j - lim;
      x2 = i + lim;
      y2 = j + lim;
      if (In_map(x1, y1) && In_map(x2, y2) && Area(x1, y1, x2, y2) == SS) {
        t[x1][y1]++;
        t[x1][y2 + 1]--;
        t[x2 + 1][y1]--;
        t[x2 + 1][y2 + 1]++;
      }
    }
  }
  for (int x = 1; x <= N; x++) {
    for (int y = 1; y <= M; y++) {
      t[x][y] = t[x][y] + t[x - 1][y] + t[x][y - 1] - t[x - 1][y - 1];
      if (X[x][y] && !t[x][y]) return false;
    }
  }
  return true;
}
int main() {
  scanf("%d%d", &N, &M);
  for (int i = 0; i <= M; i++) {
    mp[0].push_back(0);
    mp[N + 1].push_back(0);
  }
  for (int i = 1, val; i <= N; i++) {
    mp[i].push_back(0);
    X[i].push_back(false);
    scanf("%s", s + 1);
    for (int j = 1; j <= M; j++) {
      val = mp[i - 1][j] + mp[i][j - 1] - mp[i - 1][j - 1];
      if (s[j] == 'X') {
        val++;
        X[i].push_back(true);
      } else
        X[i].push_back(false);
      mp[i].push_back(val);
    }
  }
  int l = 0, r = min(N, M), mid, ans = 0;
  while (l <= r) {
    mid = (l + r) >> 1;
    if (check(mid)) {
      l = mid + 1;
      ans = mid;
    } else
      r = mid - 1;
  }
  printf("%d\n", ans);
  int SS = (2 * ans + 1) * (2 * ans + 1);
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= M; j++) {
      if (X[i][j] && In_map(i - ans, j - ans) && In_map(i + ans, j + ans) &&
          Area(i - ans, j - ans, i + ans, j + ans) == SS)
        printf("X");
      else
        printf(".");
    }
    puts("");
  }
  return 0;
}
