#include <bits/stdc++.h>
using namespace std;
const long double PI = 4 * atan((long double)1);
const int mod = 1e9 + 7;
const int nax = 0;
struct info {
  int x, y, times;
};
int n, m;
vector<vector<int>> pref, vis, res, ans, a;
int baris[] = {0, 0, 1, -1, 1, -1, 1, -1};
int kolom[] = {1, -1, 0, 0, 1, 1, -1, -1};
void init() {
  pref.resize(n + 5, vector<int>(m + 5));
  res.resize(n + 5, vector<int>(m + 5));
  a.resize(n + 5, vector<int>(m + 5));
  vis.resize(n + 5, vector<int>(m + 5));
  ans.resize(n + 5, vector<int>(m + 5));
}
int is_burn(int x, int y, int sz) {
  if (x - sz <= 0 || x + sz > n || y - sz <= 0 || y + sz > m) {
    return -1;
  }
  int ret = pref[x + sz][y + sz] - pref[x - sz - 1][y + sz] -
            pref[x + sz][y - sz - 1] + pref[x - sz - 1][y - sz - 1];
  return ret;
}
void bfs(int lim) {
  queue<info> q;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (res[i][j]) {
        q.push({i, j, 0});
        vis[i][j] = 1;
      }
    }
  }
  while (!q.empty()) {
    auto cur = q.front();
    q.pop();
    ans[cur.x][cur.y] = 1;
    if (cur.times + 1 <= lim) {
      for (int i = 0; i < 8; i++) {
        int nx = cur.x + baris[i];
        int ny = cur.y + kolom[i];
        if (!vis[nx][ny]) {
          vis[nx][ny] = 1;
          q.push({nx, ny, cur.times + 1});
        }
      }
    }
  }
}
void cler() {
  for (int i = 0; i < n + 5; i++) {
    for (int j = 0; j < m + 5; j++) {
      vis[i][j] = 0;
      res[i][j] = 0;
      ans[i][j] = 0;
    }
  }
}
int que(int sz) {
  cler();
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (is_burn(i, j, sz) == (2 * sz + 1) * (2 * sz + 1)) {
        res[i][j] = 1;
      }
    }
  }
  bfs(sz);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (ans[i][j] != a[i][j]) {
        return 0;
      }
    }
  }
  return 1;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> m;
  init();
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      char c;
      cin >> c;
      if (c == 'X') {
        a[i][j] = 1;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      pref[i][j] =
          pref[i][j - 1] + pref[i - 1][j] - pref[i - 1][j - 1] + a[i][j];
    }
  }
  int l = 0, r = 5e5, ret = 0;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (que(mid)) {
      l = mid + 1;
      ret = max(ret, mid);
    } else {
      r = mid - 1;
    }
  }
  cout << ret << '\n';
  que(ret);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (res[i][j]) {
        cout << "X";
      } else {
        cout << ".";
      }
    }
    cout << '\n';
  }
}
