#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace std;
const int dx[8] = {1, 1, 1, 0, 0, -1, -1, -1};
const int dy[8] = {1, -1, 0, 1, -1, -1, 1, 0};
template <class T>
T gcd(T a, T b) {
  return ((b == 0) ? a : gcd(b, a % b));
}
int n, m;
vector<string> s;
vector<string> g;
vector<vector<int> > dist, dist1;
bool valid(int i, int j) { return (min(i, j) >= 0 && i < n && j < m); }
bool check(int t) {
  queue<pair<int, int> > q;
  for (int i = 0; i < (int)(n); i++)
    for (int j = 0; j < (int)(m); j++) {
      g[i][j] = '.';
      dist1[i][j] = 0;
      if (dist[i][j] > t) {
        g[i][j] = 'X';
        dist1[i][j] = t + 1;
        q.push(make_pair(i, j));
      }
    }
  while (!q.empty()) {
    pair<int, int> cur = q.front();
    q.pop();
    for (int i = 0; i < (int)(8); i++) {
      int x = cur.first + dx[i], y = cur.second + dy[i];
      if (valid(x, y) && dist1[x][y] < (dist1[cur.first][cur.second] - 1)) {
        dist1[x][y] = dist1[cur.first][cur.second] - 1;
        q.push(make_pair(x, y));
      }
    }
  }
  for (int i = 0; i < (int)(n); i++)
    for (int j = 0; j < (int)(m); j++) {
      if (s[i][j] == '.') {
        if (dist1[i][j] > 0) return 0;
      } else if (dist1[i][j] <= 0)
        return 0;
    }
  return 1;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  s.resize(n);
  for (int i = 0; i < (int)(n); i++) cin >> s[i];
  string a = "";
  for (int j = 0; j < (int)(m); j++) a += '.';
  g.resize(n);
  for (int i = 0; i < (int)(n); i++) g[i] = a;
  dist.resize(n);
  dist1.resize(n);
  vector<int> b(m, 0);
  for (int i = 0; i < (int)(n); i++) dist[i] = dist1[i] = b;
  queue<pair<int, int> > q;
  for (int i = 0; i < (int)(n); i++)
    for (int j = 0; j < (int)(m); j++) {
      if (s[i][j] == '.') {
        dist[i][j] = 0;
        q.push(make_pair(i, j));
      } else
        dist[i][j] = 1e9;
    }
  while (!q.empty()) {
    pair<int, int> cur = q.front();
    q.pop();
    for (int i = 0; i < (int)(8); i++) {
      int x = cur.first + dx[i], y = cur.second + dy[i];
      if (valid(x, y) && dist[x][y] == 1e9) {
        dist[x][y] = dist[cur.first][cur.second] + 1;
        q.push(make_pair(x, y));
      }
    }
  }
  for (int i = 0; i < (int)(n); i++)
    for (int j = 0; j < (int)(m); j++) {
      dist[i][j] = min({dist[i][j], i + 1, j + 1, (n - i), (m - j)});
    }
  int l = 0, r = min(n, m) + 1;
  while (l < r) {
    int mid = (l + r + 1) / 2;
    if (check(mid))
      l = mid;
    else
      r = mid - 1;
  }
  cout << l << endl;
  assert(check(l));
  for (int i = 0; i < (int)(n); i++) cout << g[i] << '\n';
  return 0;
}
