#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 239;
vector<int> dx = {0, 0, 1, -1, 1, 1, -1, -1};
vector<int> dy = {-1, 1, 0, 0, 1, -1, 1, -1};
int n, m;
vector<vector<int>> a;
vector<vector<int>> ps;
vector<vector<int>> cand;
vector<vector<int>> d;
int gt(int i, int j) {
  if (i == -1 || j == -1) {
    return 0;
  } else {
    return ps[i][j];
  }
}
void init() {
  ps.resize(n, vector<int>(m));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      ps[i][j] = a[i][j] + gt(i, j - 1) + gt(i - 1, j) - gt(i - 1, j - 1);
    }
  }
}
int sm(int i, int j, int m) {
  return gt(i + m - 1, j + m - 1) - gt(i - 1, j + m - 1) -
         gt(i + m - 1, j - 1) + gt(i - 1, j - 1);
}
bool bfs(int x) {
  queue<pair<int, int>> q;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (cand[i][j]) {
        d[i][j] = 0;
        q.push({i, j});
      }
    }
  }
  while (!q.empty()) {
    int i, j;
    tie(i, j) = q.front();
    q.pop();
    for (int k = 0; k < 8; k++) {
      int ni = i + dx[k];
      int nj = j + dy[k];
      if (ni >= 0 && ni < n && nj >= 0 && nj < m && d[ni][nj] > d[i][j] + 1) {
        d[ni][nj] = d[i][j] + 1;
        q.push({ni, nj});
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i][j] && d[i][j] > x) {
        return false;
      }
    }
  }
  return true;
}
bool can(int x) {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cand[i][j] = 0;
      d[i][j] = INF;
    }
  }
  int len = 2 * x + 1;
  for (int i = 0; i + len <= n; i++) {
    for (int j = 0; j + len <= m; j++) {
      if (sm(i, j, len) == len * len) {
        cand[i + len / 2][j + len / 2] = 1;
      }
    }
  }
  return bfs(x);
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  a.resize(n, vector<int>(m));
  cand.resize(n, vector<int>(m, 0));
  d.resize(n, vector<int>(m, INF));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      char c;
      cin >> c;
      a[i][j] = (c == 'X');
    }
  }
  init();
  int l = 0;
  int r = 507;
  while (r - l > 1) {
    int x = (r + l) >> 1;
    if (can(x)) {
      l = x;
    } else {
      r = x;
    }
  }
  assert(can(l));
  cout << l << endl;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (cand[i][j])
        cout << 'X';
      else
        cout << '.';
    }
    cout << '\n';
  }
}
