#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -1;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
  return x * f;
}
const int MAXN = 2000010;
const int INF = 2147483600;
int N, M;
long long s[MAXN + 1], Ad[MAXN + 1];
inline long long Query(int x1, int y1, int x2, int y2) {
  return s[(max(((x2)-1) * M + (y2), 0))] -
         s[(max(((x2)-1) * M + (y1 - 1), 0))] -
         s[(max(((x1 - 1) - 1) * M + (y2), 0))] +
         s[(max(((x1 - 1) - 1) * M + (y1 - 1), 0))];
}
char str[MAXN + 1];
inline bool check(int k) {
  for (int i = 0; i <= N + 1; i++)
    for (int j = 0; j <= M + 1; j++) Ad[(max(((i)-1) * M + (j), 0))] = 0;
  for (int i = k + 1; i <= N - k; i++) {
    for (int j = k + 1; j <= M - k; j++)
      if ((long long)Query(i - k, j - k, i + k, j + k) ==
          (long long)(2 * k + 1) * (2 * k + 1)) {
        Ad[(max(((i - k) - 1) * M + (j - k), 0))]++,
            Ad[(max(((i - k) - 1) * M + (j + k + 1), 0))]--,
            Ad[(max(((i + k + 1) - 1) * M + (j - k), 0))]--,
            Ad[(max(((i + k + 1) - 1) * M + (j + k + 1), 0))]++;
      }
  }
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= M; j++) {
      Ad[(max(((i)-1) * M + (j), 0))] =
          Ad[(max(((i - 1) - 1) * M + (j), 0))] +
          Ad[(max(((i)-1) * M + (j - 1), 0))] +
          Ad[(max(((i)-1) * M + (j), 0))] -
          Ad[(max(((i - 1) - 1) * M + (j - 1), 0))];
      if (str[(max(((i)-1) * M + (j), 0))] == 'X' &&
          !Ad[(max(((i)-1) * M + (j), 0))]) {
        return false;
      }
      if (str[(max(((i)-1) * M + (j), 0))] != 'X' &&
          Ad[(max(((i)-1) * M + (j), 0))]) {
        return false;
      }
    }
  }
  return true;
}
int main() {
  N = read(), M = read();
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= M; j++) scanf("%c", &str[(max(((i)-1) * M + (j), 0))]);
    getchar();
  }
  for (int i = 1; i <= N; i++)
    for (int j = 1; j <= M; j++)
      s[(max(((i)-1) * M + (j), 0))] =
          s[(max(((i - 1) - 1) * M + (j), 0))] +
          s[(max(((i)-1) * M + (j - 1), 0))] +
          (str[(max(((i)-1) * M + (j), 0))] == 'X') -
          s[(max(((i - 1) - 1) * M + (j - 1), 0))];
  int l = 0, r = min(N, M), ans = 0;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(mid))
      ans = mid, l = mid + 1;
    else
      r = mid - 1;
  }
  printf("%d\n", ans);
  long long k = ans;
  memset(str, '.', sizeof(str));
  for (int i = k + 1; i <= N - k; i++) {
    for (int j = k + 1; j <= M - k; j++)
      if ((long long)Query(i - k, j - k, i + k, j + k) ==
          (long long)(2 * k + 1) * (2 * k + 1))
        str[(max(((i)-1) * M + (j), 0))] = 'X';
  }
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= M; j++) printf("%c", str[(max(((i)-1) * M + (j), 0))]);
    printf("\n");
  }
  return 0;
}
