#include <bits/stdc++.h>
using namespace std;
template <class c>
struct rge {
  c b, e;
};
template <class c>
rge<c> range(c i, c j) {
  return rge<c>{i, j};
}
template <class c>
auto dud(c* x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
  template <class c>
  debug& operator<<(const c&) {
    return *this;
  }
};
void solve() {
  int n, m;
  cin >> n >> m;
  vector<vector<char> > v(n + 1, vector<char>(m + 1));
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      cin >> v[i][j];
    }
  }
  vector<vector<int> > pf(n + 1, vector<int>(m + 1));
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      pf[i][j] = pf[i - 1][j] + pf[i][j - 1] - pf[i - 1][j - 1];
      pf[i][j] += ((v[i][j] == 'X') ? 1 : 0);
    }
  }
  auto get = [&](int x, int y, int x1, int y1) {
    return (pf[x1][y1] - pf[x1][y - 1] - pf[x - 1][y1] + pf[x - 1][y - 1]);
  };
  auto check = [&](int side) {
    vector<vector<int> > add(n + 1, vector<int>(m + 1));
    for (int i = 1; i + 2 * side <= n; ++i) {
      for (int j = 1; j + 2 * side <= m; ++j) {
        long long area = (2 * side + 1) * (2 * side + 1);
        if (get(i, j, i + 2 * side, j + 2 * side) == area) {
          ++add[i][j];
          if (i + 2 * side < n) --add[i + 2 * side + 1][j];
          if (j + 2 * side < m) --add[i][j + 2 * side + 1];
          if (i + 2 * side < n && j + 2 * side < m)
            ++add[i + 2 * side + 1][j + 2 * side + 1];
        }
      }
    }
    vector<vector<int> > pf1(n + 1, vector<int>(m + 1));
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= m; ++j) {
        pf1[i][j] =
            pf1[i - 1][j] + pf1[i][j - 1] - pf1[i - 1][j - 1] + add[i][j];
        int a = !!pf1[i][j], b = ((v[i][j] == 'X') ? 1 : 0);
        if (a ^ b) return 0;
      }
    }
    return 1;
  };
  int l = 0, r = min(n, m), mid, ans = 1;
  while (l <= r) {
    mid = l + r >> 1;
    if (check(mid))
      ans = mid, l = mid + 1;
    else
      r = mid - 1;
  }
  cout << ans << '\n';
  int side = ans;
  vector<vector<char> > res(n + 1, vector<char>(m + 1, '.'));
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (i + 2 * side <= n && j + 2 * side <= m) {
        long long area = (2 * side + 1) * (2 * side + 1);
        if (get(i, j, i + 2 * side, j + 2 * side) == area)
          res[i + side][j + side] = 'X';
      }
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) cout << res[i][j];
    cout << '\n';
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int q = 1;
  while (q--) solve();
}
