#include <bits/stdc++.h>
using namespace std;
const int mx = 1e6;
int n, m;
string grid[mx + 1];
vector<vector<long long>> psum;
vector<int> dx = {1, 0, -1, 0, 1, 1, -1, -1};
vector<int> dy = {0, 1, 0, -1, 1, -1, 1, -1};
bool ok(int a, int b) {
  if (a >= 0 && a < n && b >= 0 && b < m) {
    return true;
  }
  return false;
}
bool good(long long t) {
  vector<vector<bool>> visited(n, vector<bool>(m));
  queue<pair<int, int>> bfs;
  for (int q = 0; q < n; q++) {
    for (int w = 0; w < m; w++) {
      int tm = t - 1;
      if (!ok(q - tm, w - tm) || !ok(q + tm, w + tm)) {
        continue;
      }
      int lq = q + tm;
      int lw = w + tm;
      int uq = q - tm;
      int uw = w - tm;
      long long val = psum[lq + 1][lw + 1] -
                      (psum[lq + 1][uw] + psum[uq][lw + 1] - psum[uq][uw]);
      if (val == (2 * tm + 1) * (2 * tm + 1) && !visited[q][w]) {
        visited[q][w] = true;
        bfs.push({q, w});
      }
    }
  }
  for (int q = 2; q <= t; q++) {
    int sz = bfs.size();
    for (int w = 0; w < sz; w++) {
      auto t = bfs.front();
      bfs.pop();
      int x, y;
      tie(x, y) = t;
      for (int e = 0; e < 8; e++) {
        int nx = x + dx[e];
        int ny = y + dy[e];
        if (!visited[nx][ny]) {
          visited[nx][ny] = true;
          bfs.push({nx, ny});
        }
      }
    }
  }
  for (int q = 0; q < n; q++) {
    for (int w = 0; w < m; w++) {
      if (grid[q][w] == '.' && visited[q][w] == true) {
        return false;
      }
      if (grid[q][w] == 'X' && visited[q][w] == false) {
        return false;
      }
    }
  }
  return true;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  psum.resize(n + 1, vector<long long>(m + 1));
  for (int q = 0; q < n; q++) {
    cin >> grid[q];
  }
  for (int q = 1; q <= n; q++) {
    for (int w = 1; w <= m; w++) {
      psum[q][w] = psum[q - 1][w] + psum[q][w - 1] - psum[q - 1][w - 1] +
                   (grid[q - 1][w - 1] == 'X' ? 1 : 0);
    }
  }
  int lo = 1, hi = 1e6;
  while (hi - lo > 1) {
    int mid = (lo + hi) / 2;
    if (good(mid)) {
      lo = mid;
    } else {
      hi = mid;
    }
  }
  vector<vector<bool>> ans(n, vector<bool>(m));
  for (int q = 0; q < n; q++) {
    for (int w = 0; w < m; w++) {
      int tm = lo - 1;
      if (!ok(q - tm, w - tm) || !ok(q + tm, w + tm)) {
        continue;
      }
      int lq = q + tm;
      int lw = w + tm;
      int uq = q - tm;
      int uw = w - tm;
      long long val = psum[lq + 1][lw + 1] -
                      (psum[lq + 1][uw] + psum[uq][lw + 1] - psum[uq][uw]);
      if (val == (2 * tm + 1) * (2 * tm + 1) && !ans[q][w]) {
        ans[q][w] = true;
      }
    }
  }
  cout << (lo - 1) << "\n";
  for (int q = 0; q < n; q++) {
    for (int w = 0; w < m; w++) {
      cout << (ans[q][w] == true ? 'X' : '.');
    }
    cout << "\n";
  }
  return 0;
}
