#include <bits/stdc++.h>
using namespace std;
int fx[] = {-1, +1, -1, +1, +1, -1, +0, -0};
int fy[] = {-1, +1, +1, -1, +0, -0, +1, -1};
char **grid;
int **dist;
int n, m;
vector<vector<int> > dp[8];
int rec(int x, int y, int dir) {
  if (x >= n || y >= m || x < 0 || y < 0 || grid[x][y] == '.') return -1;
  int &ret = dp[dir][x][y];
  if (ret != -1) return ret;
  int a = x + fx[dir];
  int b = y + fy[dir];
  ret = 1 + rec(a, b, dir);
  return ret;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < 8; i++)
    dp[i] = vector<vector<int> >(n, vector<int>(m, -1));
  grid = (char **)malloc(n * sizeof(char *));
  dist = (int **)malloc(n * sizeof(int *));
  for (int i = 0; i < n; i++) {
    grid[i] = (char *)malloc((m + 1) * sizeof(char));
    dist[i] = (int *)malloc((m + 1) * sizeof(int));
    scanf("%s", grid[i]);
    for (int j = 0; j < m; j++) dist[i][j] = -1;
  }
  queue<pair<int, int> > q;
  int mini = INT_MAX;
  vector<pair<int, pair<int, int> > > vt;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int c = 0;
      if (grid[i][j] == '.') continue;
      for (int k = 0; k < 8; k++) {
        int x = i + fx[k];
        int y = j + fy[k];
        if (x < 0 || y < 0 || x >= n || y >= m || grid[x][y] != 'X') c++;
      }
      if (c) {
        q.push({i, j});
        dist[i][j] = 0;
      }
    }
  }
  while (!q.empty()) {
    auto top = q.front();
    int i = top.first;
    int j = top.second;
    q.pop();
    for (int k = 0; k < 8; k++) {
      int x = i + fx[k];
      int y = j + fy[k];
      if (x < 0 || y < 0 || x >= n || y >= m || grid[x][y] != 'X') continue;
      if (dist[x][y] == -1) {
        dist[x][y] = dist[i][j] + 1;
        q.push({x, y});
      }
    }
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (dist[i][j] != -1) vt.push_back({dist[i][j], {i, j}});
  sort(vt.rbegin(), vt.rend());
  int l = 0, r = vt[0].first;
  while (l <= r) {
    int mid = (l + r) / 2;
    vector<int> dist2[n];
    for (int i = 0; i < n; i++) dist2[i] = vector<int>(m, -1);
    for (int i = 0; i < vt.size(); i++) {
      if (vt[i].first >= mid) {
        q.push({vt[i].second.first, vt[i].second.second});
        dist2[vt[i].second.first][vt[i].second.second] = 0;
      }
    }
    while (!q.empty()) {
      auto top = q.front();
      int i = top.first;
      int j = top.second;
      q.pop();
      for (int k = 0; k < 8; k++) {
        int x = i + fx[k];
        int y = j + fy[k];
        if (x < 0 || y < 0 || x >= n || y >= m) continue;
        if (dist2[x][y] == -1) {
          dist2[x][y] = dist2[i][j] + 1;
          q.push({x, y});
        }
      }
    }
    int f = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        char ch = '.';
        if (dist2[i][j] != -1 && dist2[i][j] <= mid) ch = 'X';
        if (grid[i][j] != ch) f = 1;
      }
    }
    if (f == 0)
      l = mid + 1;
    else
      r = mid - 1;
  }
  l--;
  cout << l << endl;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (dist[i][j] >= l)
        printf("X");
      else
        printf(".");
    }
    printf("\n");
  }
  delete grid;
  delete dist;
  return 0;
}
