#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<pair<int, int>> ans;
bool check(int d, vector<vector<int>> a, string *s) {
  int ps[n + 2][m + 2];
  ans.clear();
  memset(ps, 0, sizeof ps);
  for (int i = 1; i + d <= n + 1; i++)
    for (int j = 1; j + d <= m + 1; j++)
      if (a[i][j] - a[i + d][j] - a[i][j + d] + a[i + d][j + d] == d * d) {
        ans.push_back({i + d / 2, j + d / 2});
        ps[i][j]++;
        ps[i][j + d]--;
        ps[i + d][j]--;
        ps[i + d][j + d]++;
      }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      ps[i][j] += ps[i][j - 1] + ps[i - 1][j] - ps[i - 1][j - 1];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (ps[i + 1][j + 1] == 0 && s[i][j] == 'X') return false;
  return true;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  string s[n];
  for (int i = 0; i < n; i++) cin >> s[i];
  vector<vector<int>> a(n + 2, vector<int>(m + 2, 0));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) a[i + 1][j + 1] = s[i][j] == 'X';
  for (int i = n; i > 0; i--)
    for (int j = m; j > 0; j--)
      a[i][j] += a[i][j + 1] + a[i + 1][j] - a[i + 1][j + 1];
  int l = 0, r = 500;
  while (r - l > 1) {
    int mid = l + r >> 1;
    check(mid * 2 + 1, a, s) ? l = mid : r = mid;
  }
  cout << l << endl;
  check(l * 2 + 1, a, s);
  bool c[n][m];
  memset(c, 0, sizeof c);
  for (auto [x, y] : ans) c[x - 1][y - 1] = 1;
  for (int i = 0; i < n; i++, cout << '\n')
    for (int j = 0; j < m; j++) cout << (c[i][j] ? 'X' : '.');
}
