#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5, mod = 1e9 + 7;
int t, mx[2][N], p[N], n, m;
vector<pair<int, int> > c;
vector<int> a[N];
bool ok(int x, int y) {
  if (x > n || y > m || min(x, y) < 1) return 0;
  return 1;
}
int sum(int i, int j, int x) {
  return a[i + x][j + x] - a[i - 1][j + x] - a[i + x][j - 1] + a[i - 1][j - 1];
}
bool check(int x) {
  c.clear();
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) p[j] = 0;
    for (int j = 1; j <= m; j++) {
      mx[1][j] = max(0, mx[0][j] - 1);
      if (ok(i + x, j + x) && sum(i, j, x) == 2 * x + 1 + x * x) {
        mx[1][j] = x + 1;
        c.push_back({i, j});
      }
      if (mx[1][j]) p[j]++, p[j + x + 1]--;
      mx[0][j] = mx[1][j];
    }
    for (int j = 1; j <= m; j++) {
      p[j] += p[j - 1];
      if (!p[j] && sum(i, j, 0)) return 0;
    }
  }
  return 1;
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  for (int j = 0; j <= m; j++) a[0].push_back(0);
  for (int i = 1; i <= n; i++) {
    a[i].push_back(0);
    for (int j = 1; j <= m; j++) {
      char c;
      cin >> c;
      a[i].push_back(0);
      a[i][j] = a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
      if (c == 'X') a[i][j]++;
    }
  }
  int l = 0, r = min((n + 1) / 2, (m + 1) / 2) - 1, ans = 0;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (check(2 * mid)) {
      ans = mid;
      l = mid + 1;
    } else
      r = mid - 1;
  }
  check(2 * ans);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) a[i][j] = 0;
  for (int i = 0; i < c.size(); i++) {
    a[c[i].first + ans][c[i].second + ans] = 1;
  }
  cout << ans << "\n";
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (a[i][j])
        cout << 'X';
      else
        cout << '.';
    }
    cout << "\n";
  }
}
