#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > vis;
vector<string> second;
int n, m;
int main() {
  cin >> n >> m;
  second.resize(n);
  for (int i = 0; i < n; i++) cin >> second[i];
  int rmin = m, rsum[n][m], csum[n][m];
  memset(rsum, 0, sizeof(rsum));
  memset(csum, 0, sizeof(csum));
  for (int i = 0; i < n; i++) {
    int cur = 0, cnt = 0;
    for (int j = 0; j < m; j++) {
      if (second[i][j] == 'X') {
        rsum[i][j]++;
        cnt++;
      } else {
        if (cnt) rmin = min(rmin, cnt);
        cnt = 0;
      }
      if (j > 0) rsum[i][j] += rsum[i][j - 1];
    }
    if (cnt) rmin = min(rmin, cnt);
  }
  int cmin = n;
  for (int i = 0; i < m; i++) {
    int cur = 0, cnt = 0;
    for (int j = 0; j < n; j++) {
      if (second[j][i] == 'X')
        cnt++, csum[j][i]++;
      else {
        if (cnt) cmin = min(cmin, cnt);
        cnt = 0;
      }
      if (j > 0) csum[j][i] += csum[j - 1][i];
    }
    if (cnt) cmin = min(cmin, cnt);
  }
  int sm[n][m];
  memset(sm, 0, sizeof(sm));
  int mn = min(cmin, rmin);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (i - 1 >= 0 and j - 1 >= 0) sm[i][j] = sm[i - 1][j - 1];
      if (i - 1 >= 0) sm[i][j] += csum[i - 1][j];
      if (j - 1 >= 0) sm[i][j] += rsum[i][j - 1];
      if (second[i][j] == 'X') sm[i][j]++;
    }
  }
  int l = 0, h = n;
  int ans[n][m], an;
  vis.resize(n);
  for (int i = 0; i < n; i++) vis[i].resize(m);
  memset(ans, 0, sizeof(ans));
  vector<pair<int, int> > fn;
  while (l <= h) {
    int mid = (l + h) / 2;
    vector<pair<int, int> > cr;
    queue<array<int, 3> > qu;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (second[i][j] == '.') continue;
        int nx = i + 2 * mid, ny = j + 2 * mid;
        if (nx < n and ny < m) {
          int s1 = sm[nx][ny];
          if (i - 1 >= 0) s1 -= sm[i - 1][ny];
          if (j - 1 >= 0) s1 -= sm[nx][j - 1];
          if (i - 1 >= 0 and j - 1 >= 0) s1 += sm[i - 1][j - 1];
          if (s1 == (nx - i + 1) * (ny - j + 1)) {
            cr.push_back({i + mid, j + mid});
            qu.push({i + mid, j + mid, 0});
            vis[i + mid][j + mid] = 1;
          }
        }
      }
    }
    while (!qu.empty()) {
      array<int, 3> &cur = qu.front();
      int dx[] = {0, 0, 1, 1, 1, -1, -1, -1};
      int dy[] = {1, -1, 1, -1, 0, 1, -1, 0};
      for (int i = 0; i < 8; i++) {
        int nx = cur[0] + dx[i], ny = cur[1] + dy[i];
        if (nx < 0 or ny < 0 or nx >= n or ny >= m or vis[nx][ny] or
            cur[2] >= mid)
          continue;
        vis[nx][ny] = 1;
        qu.push({nx, ny, cur[2] + 1});
      }
      if (cur[2] >= mid) break;
      qu.pop();
    }
    int no = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (!vis[i][j] and second[i][j] == 'X') no = 1;
        vis[i][j] = 0;
      }
    }
    if (no)
      h = mid - 1;
    else {
      an = mid, l = mid + 1, fn = cr;
    }
  }
  cout << an << "\n";
  for (pair<int, int> &pr : fn) ans[pr.first][pr.second] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (ans[i][j])
        cout << "X";
      else
        cout << ".";
    }
    cout << "\n";
  }
}
