#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Pii = pair<int, int>;
using Vi = vector<int>;
void run();
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(18);
  run();
  return 0;
}
int n, m;
vector<string> board;
vector<Vi> dists, dists2;
bool check(int maxTime, int toGet) {
  dists2.resize(n);
  for (int i = (0); i < (n); i++) dists2[i].assign(m, -1);
  queue<Pii> que;
  for (int i = (0); i < (n); i++)
    for (int j = (0); j < (m); j++) {
      if (board[i][j] == 'X' && dists[i][j] >= maxTime) {
        dists2[i][j] = maxTime;
        que.push({i, j});
        toGet--;
      }
    }
  while (!que.empty()) {
    int first = que.front().first, second = que.front().second;
    int d = dists2[first][second];
    que.pop();
    if (d == 1) continue;
    for (int a = (-1); a < (2); a++) {
      for (int b = (-1); b < (2); b++) {
        if (a != 0 || b != 0) {
          int i = first + a, j = second + b;
          if (board[i][j] != 'X') continue;
          if (dists2[i][j] != -1) continue;
          dists2[i][j] = d - 1;
          toGet--;
          que.push({i, j});
        }
      }
    }
  }
  return toGet == 0;
}
void run() {
  cin >> n >> m;
  board.push_back(string(m + 2, '.'));
  for (int i = (0); i < (n); i++) {
    string t;
    cin >> t;
    board.push_back("." + t + ".");
  }
  board.push_back(string(m + 2, '.'));
  n += 2;
  m += 2;
  dists.resize(n, Vi(m, -1));
  queue<Pii> que;
  for (int i = (0); i < (n); i++)
    for (int j = (0); j < (m); j++) {
      if (board[i][j] == '.') {
        dists[i][j] = 0;
        que.push({i, j});
      }
    }
  auto relax = [&](int i, int j, int d) {
    if (i < 0 || j < 0 || i >= n || j >= m) return;
    if (dists[i][j] == -1) {
      dists[i][j] = d;
      que.push({i, j});
    }
  };
  while (!que.empty()) {
    int first = que.front().first, second = que.front().second;
    int d = dists[first][second] + 1;
    que.pop();
    for (int a = (-1); a < (2); a++) {
      for (int b = (-1); b < (2); b++) {
        if (a != 0 || b != 0) {
          relax(first + a, second + b, d);
        }
      }
    }
  }
  int begin = 1, end = 0;
  int toGet = 0;
  for (int i = (0); i < (n); i++)
    for (int j = (0); j < (m); j++) {
      if (board[i][j] == 'X') {
        toGet++;
        end = max(dists[i][j] + 2, end);
      }
    }
  while (begin + 1 < end) {
    int mid = (begin + end) / 2;
    if (check(mid, toGet)) {
      begin = mid;
    } else {
      end = mid;
    }
  }
  int answer = begin;
  cout << answer - 1 << '\n';
  for (int i = (1); i < (n - 1); i++) {
    for (int j = (1); j < (m - 1); j++) {
      if (board[i][j] == 'X') {
        if (dists[i][j] >= answer) {
          cout << 'X';
        } else {
          cout << '.';
        }
      } else {
        cout << '.';
      }
    }
    cout << '\n';
  }
}
