#include <bits/stdc++.h>
using namespace std;
const long long N = 1e6 + 10;
long long n, m, ans, cnt, tot;
vector<vector<char> > ch(N), out(N);
vector<vector<long long> > sum(N);
bool check(long long mid) {
  long long cnt[n + 2][m + 2];
  memset(cnt, 0, sizeof(cnt));
  for (long long i = 2 * mid + 1; i <= n; i++) {
    for (long long j = 2 * mid + 1; j <= m; j++) {
      if (ch[i][j] == 'X' &&
          (sum[i][j] + sum[i - 2 * mid - 1][j - 2 * mid - 1] -
           sum[i][j - 2 * mid - 1] - sum[i - 2 * mid - 1][j]) ==
              (2 * mid + 1) * (2 * mid + 1))
        cnt[i + 1][j + 1]++, cnt[i - 2 * mid][j - 2 * mid]++,
            cnt[i + 1][j - 2 * mid]--, cnt[i - 2 * mid][j + 1]--;
    }
  }
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= m; j++) {
      cnt[i][j] = cnt[i - 1][j] + cnt[i][j - 1] - cnt[i - 1][j - 1] + cnt[i][j];
      if ((cnt[i][j] > 0 && ch[i][j] == '.') ||
          (cnt[i][j] <= 0 && ch[i][j] == 'X'))
        return 0;
    }
  }
  return 1;
}
void solve() {
  cin >> n >> m;
  for (long long i = 0; i <= n + 1; i++) {
    ch[i].resize(m + 2);
    out[i].resize(m + 2);
    sum[i].resize(m + 2);
    for (long long j = 1; j <= m && i <= n && i; j++)
      out[i][j] = '.', cin >> ch[i][j];
  }
  for (long long j = 1; j <= m; j++) {
    cnt = 0;
    for (long long i = 1; i <= n; i++) {
      if (ch[i][j] == 'X') cnt++;
      sum[i][j] = sum[i][j - 1] + cnt;
    }
  }
  long long s = 1, e = min(n / 2, m / 2);
  while (s <= e) {
    long long mid = (s + e) / 2;
    if (check(mid)) {
      s = mid + 1;
      ans = mid;
    } else
      e = mid - 1;
  }
  cout << ans << '\n';
  cnt = (2 * ans + 1) * (2 * ans + 1);
  for (long long i = 2 * ans + 1; i <= n; i++) {
    for (long long j = 2 * ans + 1; j <= m; j++) {
      if (ch[i][j] == 'X' &&
          (sum[i][j] + sum[i - 2 * ans - 1][j - 2 * ans - 1] -
           sum[i][j - 2 * ans - 1] - sum[i - 2 * ans - 1][j]) == cnt)
        out[i - ans][j - ans] = 'X';
    }
  }
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= m; j++) {
      cout << out[i][j];
    }
    cout << '\n';
  }
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  clock_t start, end;
  start = clock();
  cout << fixed << setprecision(8);
  while (t--) {
    solve();
  }
  end = clock();
  double time_taken = double(end - start) / double(CLOCKS_PER_SEC);
}
