#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n = 0, m = 0;
  cin >> n >> m;
  vector<vector<char> > d(n + 2, vector<char>(m + 2));
  vector<vector<int> > am(n + 2, vector<int>(m + 2));
  vector<vector<int> > u(n + 2, vector<int>(m + 2));
  int totam = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      cin >> d[i][j];
      am[i][j] =
          (d[i][j] == 'X') + am[i - 1][j] + am[i][j - 1] - am[i - 1][j - 1];
      totam += (d[i][j] == 'X');
    }
  int l = 0, r = min((n - 1) / 2, (m - 1) / 2);
  int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
  int dy[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
  while (l < r) {
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++) {
        u[i][j] = 0;
      }
    int mid = (l + r + 1) / 2;
    vector<pair<int, int> > pos;
    int cd = mid * 2 + 1;
    for (int i = cd; i <= n; i++)
      for (int j = cd; j <= m; j++)
        if (am[i][j] - am[i - cd][j] - am[i][j - cd] + am[i - cd][j - cd] ==
            cd * cd) {
          pos.push_back(make_pair(i - mid, j - mid));
          u[i - cd + 1][j - cd + 1]++;
          u[i - cd + 1][j + 1]--;
          u[i + 1][j - cd + 1]--;
          u[i + 1][j + 1]++;
        }
    stack<pair<pair<int, int>, int> > s;
    int have = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        u[i][j] = u[i][j] + u[i - 1][j] + u[i][j - 1] - u[i - 1][j - 1];
        have += (u[i][j] > 0);
      }
    }
    if (have == totam)
      l = mid;
    else
      r = mid - 1;
  }
  cout << l << '\n';
  int cur = l * 2 + 1;
  vector<vector<int> > ans(n + 1, vector<int>(m + 1));
  for (int i = cur; i <= n; i++)
    for (int j = cur; j <= m; j++)
      if (am[i][j] - am[i - cur][j] - am[i][j - cur] + am[i - cur][j - cur] ==
          cur * cur)
        ans[i - l][j - l] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) cout << (ans[i][j] ? 'X' : '.');
    cout << '\n';
  }
  return 0;
}
