#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int s = 0, w = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    s = (s << 3) + (s << 1) + (ch ^ 48);
    ch = getchar();
  }
  return s * w;
}
const long long mod = 998244353;
const int N = 1001000;
int n, m;
int a[N];
char str[N];
inline int get_sum(vector<vector<int> > &Pre, int x, int y, int r) {
  int s = Pre[x][y] - Pre[x - r][y] - Pre[x][y - r] + Pre[x - r][y - r];
  return s;
}
inline int get_sum1(vector<vector<int> > &Pre, int x, int y, int i, int j) {
  int s = Pre[i][j] - Pre[x][j] - Pre[i][y] + Pre[x][y];
  return s;
}
inline int check(vector<vector<int> > &Pre, vector<vector<char> > &Q, int mid) {
  vector<vector<int> > P(n + 1, vector<int>(m + 1, 0));
  for (int i = 2 * mid + 1; i <= n; i++) {
    for (int j = 2 * mid + 1; j <= m; j++) {
      if (get_sum(Pre, i, j, 2 * mid + 1) == (2 * mid + 1) * (2 * mid + 1))
        P[i][j]++;
      P[i][j] += P[i - 1][j] + P[i][j - 1] - P[i - 1][j - 1];
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (Q[i][j] != 'X') continue;
      int val =
          get_sum1(P, i - 1, j - 1, min(n, i + 2 * mid), min(m, j + 2 * mid));
      if (val == 0) return 0;
    }
  }
  return 1;
}
int main() {
  n = read();
  m = read();
  vector<vector<char> > Q(n + 1, vector<char>(m + 1, 0));
  vector<vector<int> > Pre(n + 1, vector<int>(m + 1, 0));
  vector<vector<char> > A(n + 1, vector<char>(m + 1, '.'));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      char str;
      scanf("%c", &str);
      int val = (str == 'X' ? 1 : 0);
      Q[i][j] = str;
      Pre[i][j] = Pre[i - 1][j] + Pre[i][j - 1] - Pre[i - 1][j - 1] + val;
    }
    getchar();
  }
  int l = 0, r = min(n, m) / 2, ans = 0;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(Pre, Q, mid) == 1) {
      ans = max(ans, mid);
      l = mid + 1;
    } else
      r = mid - 1;
  }
  for (int i = ans + 1; i <= n - ans; i++) {
    for (int j = ans + 1; j <= m - ans; j++) {
      if (get_sum1(Pre, i - ans - 1, j - ans - 1, i + ans, j + ans) ==
          (2 * ans + 1) * (2 * ans + 1))
        A[i][j] = 'X';
    }
  }
  printf("%d\n", ans);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      printf("%c", A[i][j]);
    }
    printf("\n");
    ;
  }
}
