#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1);
template <class T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
template <class T>
using max_heap = priority_queue<T>;
const int mod = 998244353;
const int inf = 0x3fffffff;
const int N = 2 * 1e5 + 10;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
struct PairHash {
  template <typename T1, typename T2>
  std::size_t operator()(const pair<T1, T2> &p) const {
    return hash<T1>()(p.first) ^ hash<T2>()(p.second);
  }
};
int n, m;
const int d[][2] = {{0, 1}, {1, 0},  {0, -1}, {-1, 0},
                    {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
void calc_ng(vector<char *> &ng, const vector<char *> &g, int T,
             const vector<vector<int>> &sum) {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      ng[i][j] = '.';
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int zi = i - T * 2;
      int zj = j - T * 2;
      if (zi < 0 || zj < 0) continue;
      int s = sum[i][j] - (zi - 1 >= 0 ? sum[zi - 1][j] : 0) -
              (zj - 1 >= 0 ? sum[i][zj - 1] : 0) +
              (zi - 1 >= 0 && zj - 1 >= 0 ? sum[zi - 1][zj - 1] : 0);
      if (s == (2 * T + 1) * (2 * T + 1)) ng[i - T][j - T] = 'X';
    }
  }
}
bool verify_ng(vector<char *> &ng, const vector<char *> &g, int T) {
  queue<pair<int, int>> q;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (ng[i][j] == 'X') q.push(pair<int, int>(i, j));
    }
  }
  for (int t = 0; t < T; t++) {
    vector<pair<int, int>> n_e;
    while (!q.empty()) {
      pair<int, int> head = q.front();
      q.pop();
      for (int k = 0; k < 8; k++) {
        int nx = head.first + d[k][0];
        int ny = head.second + d[k][1];
        if (nx < 0 || nx >= n || ny < 0 || ny >= m) return false;
        if (ng[nx][ny] != '.') continue;
        ng[nx][ny] = 'X';
        if (g[nx][ny] == '.') return false;
        n_e.push_back(pair<int, int>(nx, ny));
      }
    }
    for (auto &p : n_e) q.push(p);
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (ng[i][j] != g[i][j]) return false;
    }
  }
  return true;
}
bool okay(int T, const vector<char *> &g, vector<char *> &ng,
          const vector<vector<int>> &sum) {
  calc_ng(ng, g, T, sum);
  return verify_ng(ng, g, T);
}
void solve(int ncase) {
  scanf("%d%d", &n, &m);
  vector<char *> g(n);
  for (int i = 0; i < n; i++) {
    g[i] = new char[m + 1];
  }
  for (int i = 0; i < n; i++) {
    scanf("%s", g[i]);
    g[i][m] = 0;
  }
  int ans = max(n, m);
  int T = (ans + 1) / 2 - 1;
  if (T == 0) {
    printf("%d\n", 0);
    for (int i = 0; i < n; i++) printf("%s\n", g[i]);
    return;
  }
  vector<vector<int>> col_sum(n, vector<int>(m));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      col_sum[i][j] = (j > 0 ? col_sum[i][j - 1] : 0) + (g[i][j] == 'X');
    }
  }
  vector<vector<int>> sum(n, vector<int>(m));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      sum[i][j] = (i > 0 ? sum[i - 1][j] : 0) + col_sum[i][j];
    }
  }
  vector<char *> ng(n);
  for (int i = 0; i < n; i++) {
    ng[i] = new char[m + 1];
    for (int j = 0; j < m; j++) {
      ng[i][j] = '.';
    }
    ng[i][m] = 0;
  }
  int l = 0, r = T + 1;
  while (l + 1 < r) {
    int mid = (l + r) / 2;
    if (okay(mid, g, ng, sum))
      l = mid;
    else
      r = mid;
  }
  printf("%d\n", l);
  calc_ng(ng, g, l, sum);
  for (int i = 0; i < n; i++) printf("%s\n", ng[i]);
}
void solve_all_cases() {
  int T = 1;
  int ncase = 0;
  while (T--) {
    solve(++ncase);
  }
}
int main() {
  cout << std::fixed;
  cout << setprecision(9);
  solve_all_cases();
}
