#include <bits/stdc++.h>
using namespace std;
const int inf = (int)1e9;
const int mod = inf + 7;
const double eps = 1e-9;
const double pi = acos(-1.0);
int n, m;
vector<string> a;
vector<vector<int> > dp;
void precalc(vector<string> &a, vector<vector<int> > &dp) {
  dp.assign(n, vector<int>(m, 0));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      dp[i][j] = (a[i][j] == 'X');
      if (i) dp[i][j] += dp[i - 1][j];
      if (j) dp[i][j] += dp[i][j - 1];
      if (i && j) dp[i][j] -= dp[i - 1][j - 1];
    }
  }
}
void normalize(int &x, int &y) {
  if (x < 0) x = 0;
  if (x >= n) x = n - 1;
  if (y < 0) y = 0;
  if (y >= m) y = m - 1;
}
int calc(vector<vector<int> > &dp, int x1, int stupid_cmath, int x2, int y2) {
  normalize(x1, stupid_cmath);
  normalize(x2, y2);
  if (x1 > x2 || stupid_cmath > y2) return 0;
  int ans = dp[x2][y2];
  if (x1) ans -= dp[x1 - 1][y2];
  if (stupid_cmath) ans -= dp[x2][stupid_cmath - 1];
  if (x1 && stupid_cmath) ans += dp[x1 - 1][stupid_cmath - 1];
  return ans;
}
void assign(vector<string> &a, vector<vector<int> > &dp, vector<string> &b,
            int T) {
  b = a;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) b[i][j] = '.';
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (calc(dp, i - T, j - T, i + T, j + T) ==
          (2ll * T + 1) * (2ll * T + 1)) {
        b[i][j] = 'X';
      }
    }
  }
}
int dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
int dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
bool ok(int T) {
  vector<string> b;
  assign(a, dp, b, T);
  vector<vector<int> > t(n, vector<int>(m, -1));
  queue<pair<int, int> > q;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (b[i][j] == 'X') {
        q.push(make_pair(i, j));
        t[i][j] = 0;
      }
    }
  }
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();
    if (t[p.first][p.second] > T) continue;
    for (int d = 0; d < 8; d++) {
      int x = p.first + dx[d];
      int y = p.second + dy[d];
      if (x >= 0 && x < n && y >= 0 && y < m && t[x][y] == -1) {
        t[x][y] = t[p.first][p.second] + 1;
        q.push(make_pair(x, y));
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (t[i][j] != -1 && t[i][j] <= T) b[i][j] = 'X';
    }
  }
  for (int i = 0; i < n; i++) {
    if (a[i] != b[i]) return false;
  }
  return true;
}
char TMP[1001000];
int main() {
  scanf("%d %d\n", &n, &m);
  string tmp = "";
  for (int i = 0; i < m + 2; i++) tmp += '.';
  a.push_back(tmp);
  for (int i = 0; i < n; i++) {
    string tmp;
    scanf("%s\n", TMP);
    tmp = TMP;
    a.push_back("." + tmp + ".");
  }
  a.push_back(tmp);
  n += 2;
  m += 2;
  precalc(a, dp);
  int ans = 0;
  int l = 1, r = min(n, m);
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (ok(mid)) {
      ans = mid;
      l = mid + 1;
    } else
      r = mid - 1;
  }
  vector<string> b;
  assign(a, dp, b, ans);
  vector<string> res;
  for (int i = 1; i + 1 < b.size(); i++) {
    string tmp = b[i];
    tmp.erase((int)tmp.size() - 1);
    tmp.erase(0, 1);
    res.push_back(tmp);
  }
  b = res;
  printf("%d\n", ans);
  for (int i = 0; i < b.size(); i++) {
    for (int j = 0; j < b[i].size(); j++) printf("%c", b[i][j]);
    printf("\n");
  }
  return 0;
}
