#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> sum;
vector<vector<bool>> used;
int n, m;
int g = 0;
int k = 0;
vector<pair<int, int>> p = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
void dfs(int first, int second, const int &r1, const int &r2, const int &l1,
         const int &l2) {
  if (first <= r1 || first > r2 || second <= l1 || second > l2 ||
      used[first][second])
    return;
  used[first][second] = true;
  k++;
  for (int i = 0; i < 4; i++)
    dfs(first + p[i].first, second + p[i].second, r1, r2, l1, l2);
}
bool f(int r) {
  r = r * 2 + 1;
  k = 0;
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= m; j++) used[i][j] = false;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (i >= r && j >= r &&
          sum[i][j] - sum[i - r][j] - sum[i][j - r] + sum[i - r][j - r] ==
              r * r) {
        dfs(i, j, i - r, i, j - r, j);
      }
  if (k == g)
    return true;
  else
    return false;
}
int main() {
  cin >> n >> m;
  sum.resize(n + 1, vector<int>(m + 1, 0));
  used.resize(n + 1, vector<bool>(m + 1, 0));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      char c;
      cin >> c;
      sum[i + 1][j + 1] = sum[i][j + 1] + sum[i + 1][j] - sum[i][j];
      if (c == 'X') sum[i + 1][j + 1]++;
    }
  g = sum.back().back();
  int l = 0, r = min(n / 2 + 2, m / 2 + 2);
  while (l + 1 < r) {
    int c = (l + r) / 2;
    if (f(c))
      l = c;
    else
      r = c;
  }
  cout << l << '\n';
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (i >= l + 1 && j >= l + 1 && i + l <= n && j + l <= m &&
          sum[i + l][j + l] - sum[i - l - 1][j + l] - sum[i + l][j - l - 1] +
                  sum[i - l - 1][j - l - 1] ==
              (2 * l + 1) * (2 * l + 1))
        cout << 'X';
      else
        cout << '.';
    }
    cout << '\n';
  }
  cin >> n;
  return 0;
}
