#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  vector<vector<char>> arr(n + 2, vector<char>(m + 2));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> arr[i][j];
    }
  }
  for (int i = 0; i < n + 2; i++) {
    arr[i][0] = arr[i][m + 1] = '.';
  }
  for (int i = 0; i < m + 2; i++) {
    arr[0][i] = arr[n + 1][i] = '.';
  }
  n += 2, m += 2;
  int l = 0, r = n + m + 1;
  vector<vector<int>> dist(n, vector<int>(m));
  queue<pair<int, int>> q;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (arr[i][j] == '.') {
        q.emplace(i, j);
        dist[i][j] = 0;
      } else {
        dist[i][j] = INF;
      }
    }
  }
  while (!q.empty()) {
    int i = q.front().first, j = q.front().second;
    q.pop();
    vector<pair<int, int>> t = {{1, 0},  {1, 1},   {0, 1},  {-1, 1},
                                {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
    for (auto &add : t) {
      int ni = i + add.first, nj = j + add.second;
      if (ni >= 0 && ni < n && nj >= 0 && nj < m && dist[ni][nj] == INF) {
        dist[ni][nj] = dist[i][j] + 1;
        q.emplace(ni, nj);
      }
    }
  }
  while (r - l > 1) {
    int t = (l + r) / 2;
    vector<vector<int>> dist_start(n, vector<int>(m));
    deque<pair<int, int>> q;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (arr[i][j] == 'X' && dist[i][j] > t) {
          q.emplace_back(i, j);
          dist_start[i][j] = 0;
        } else {
          dist_start[i][j] = INF;
        }
      }
    }
    while (!q.empty()) {
      int i = q.front().first, j = q.front().second;
      q.pop_front();
      vector<pair<int, int>> t = {{1, 0},  {1, 1},   {0, 1},  {-1, 1},
                                  {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
      for (auto &add : t) {
        int ni = i + add.first, nj = j + add.second;
        if (ni >= 0 && ni < n && nj >= 0 && nj < m &&
            dist_start[ni][nj] == INF) {
          dist_start[ni][nj] = dist_start[i][j] + 1;
          q.emplace_back(ni, nj);
        }
      }
    }
    bool flag = true;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (arr[i][j] == 'X' && dist_start[i][j] > t) {
          flag = false;
        }
      }
    }
    if (flag) {
      l = t;
    } else {
      r = t;
    }
  }
  cout << l << '\n';
  for (int i = 1; i < n - 1; i++) {
    for (int j = 1; j < m - 1; j++) {
      if (arr[i][j] == 'X' && dist[i][j] > l) {
        cout << 'X';
      } else {
        cout << '.';
      }
    }
    cout << '\n';
  }
  return 0;
}
