#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int n, m;
int dx[8] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, -1, 1};
vector<vector<int>> board;
vector<vector<int>> psum;
vector<vector<bool>> mark;
bool inside(int a, int b) { return (a >= 0 && a < n && b >= 0 && b < m); }
bool go(int a) {
  queue<pair<int, int>> Q;
  int tot = 0;
  int sz = 2 * a + 1;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      mark[i][j] = false;
    }
  }
  for (int i = sz; i < n + 1; ++i) {
    for (int j = sz; j < m + 1; ++j) {
      if (psum[i][j] - psum[i - sz][j] - psum[i][j - sz] +
              psum[i - sz][j - sz] ==
          sz * sz) {
        Q.push({i - 1 - a, j - 1 - a});
        mark[i - 1 - a][j - 1 - a] = true;
      }
    }
  }
  int cnt = 0;
  while (cnt < a) {
    tot += Q.size();
    int siz = Q.size();
    for (int i = 0; i < siz; ++i) {
      auto p = Q.front();
      Q.pop();
      int x = p.first;
      int y = p.second;
      for (int d = 0; d < 8; ++d) {
        if (inside(x + dx[d], y + dy[d]) && !mark[x + dx[d]][y + dy[d]]) {
          Q.push({x + dx[d], y + dy[d]});
          mark[x + dx[d]][y + dy[d]] = true;
        }
      }
    }
    cnt++;
  }
  tot += Q.size();
  return tot == psum[n][m];
}
int main() {
  cin >> n >> m;
  board.resize(n);
  psum.resize(n + 1);
  mark.resize(n);
  for (int i = 0; i < n; ++i) {
    board[i].resize(m);
    psum[i].resize(m + 1);
    mark[i].resize(m);
  }
  psum[n].resize(m + 1);
  string k;
  for (int i = 0; i < n; ++i) {
    cin >> k;
    for (int j = 0; j < m; ++j) {
      board[i][j] = k[j] == 'X';
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      psum[i + 1][j + 1] =
          board[i][j] + psum[i][j + 1] + psum[i + 1][j] - psum[i][j];
    }
  }
  int l = 0;
  int r = min(n, m);
  while (l < r) {
    int mid = (l + r + 1) / 2;
    if (go(mid)) {
      l = mid;
    } else {
      r = mid - 1;
    }
  }
  int sz = 2 * l + 1;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      mark[i][j] = false;
    }
  }
  for (int i = sz; i < n + 1; ++i) {
    for (int j = sz; j < m + 1; ++j) {
      if (psum[i][j] - psum[i - sz][j] - psum[i][j - sz] +
              psum[i - sz][j - sz] ==
          sz * sz) {
        mark[i - 1 - l][j - 1 - l] = true;
      }
    }
  }
  cout << l << "\n";
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (mark[i][j])
        cout << "X";
      else
        cout << ".";
    }
    cout << "\n";
  }
  return 0;
}
