#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
int n, m;
char c[maxn], ans[maxn];
int d[10];
int id(int x, int y) {
  if (x <= 0 || x > n || y <= 0 || y > m) return 0;
  return (x - 1) * m + y;
}
bool f[maxn], g[maxn];
void cacf(int y, int lb, int ub, int t) {
  for (int i = lb + t; i <= ub - t; i++) f[id(i, y)] = 1;
}
void cacg(int x, int lb, int ub, int t) {
  for (int i = lb + t; i <= ub - t; i++) g[id(x, i)] = 1;
}
int vis[maxn];
queue<int> q;
bool bfs(int mid) {
  for (int i = 1; i <= n * m; i++)
    if (!vis[i]) q.push(i);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    if (vis[u] == mid) continue;
    for (int i = 0; i < 8; i++) {
      if (vis[u + d[i]] == -1) vis[u + d[i]] = vis[u] + 1, q.push(u + d[i]);
    }
  }
  for (int i = 1; i <= n * m; i++)
    if (c[i] == 'X' && vis[i] == -1) return false;
  return true;
}
bool check(int mid, bool inv) {
  for (int i = 1; i <= n * m; i++) f[i] = g[i] = 0, vis[i] = -1;
  f[0] = 0;
  for (int j = 1; j <= m; j++) {
    int lb = -1, ub = -1;
    for (int i = 1; i <= n; i++) {
      if (c[id(i - 1, j)] == '.' && c[id(i, j)] == 'X') lb = i;
      if (c[id(i + 1, j)] == '.' && c[id(i, j)] == 'X') ub = i;
      if (lb != -1 && ub != -1) cacf(j, lb, ub, mid), lb = -1, ub = -1;
    }
  }
  for (int i = 1; i <= n; i++) {
    int lb = -1, ub = -1;
    for (int j = 1; j <= m; j++) {
      if (!f[id(i, j - 1)] && f[id(i, j)]) lb = j;
      if (!f[id(i, j + 1)] && f[id(i, j)]) ub = j;
      if (lb != -1 && ub != -1) cacg(i, lb, ub, mid), lb = -1, ub = -1;
    }
  }
  for (int i = 1; i <= n * m; i++) {
    if (g[i]) {
      if (inv) ans[i] = 'X';
      vis[i] = 0;
    }
  }
  return bfs(mid);
}
int main() {
  scanf("%d%d", &n, &m);
  c[0] = '.';
  for (int i = 1; i <= n * m; i++) cin >> c[i], ans[i] = '.';
  d[0] = -m - 1;
  d[1] = -m;
  d[2] = -m + 1;
  d[3] = -1;
  d[4] = 1;
  d[5] = m - 1;
  d[6] = m;
  d[7] = m + 1;
  int lb = 0, ub = max(n, m) + 1;
  while (ub - lb > 1) {
    int mid = (lb + ub) >> 1;
    if (check(mid, 0))
      lb = mid;
    else
      ub = mid;
  }
  check(lb, 1);
  printf("%d\n", lb);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) cout << ans[id(i, j)];
    printf("\n");
  }
  return 0;
}
