#include <bits/stdc++.h>
using namespace std;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vc<vc<T>>;
template <class T>
void mkuni(vector<T>& v) {
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
}
template <class T>
void print(T x, int suc = 1) {
  cout << x;
  if (suc == 1)
    cout << '\n';
  else
    cout << ' ';
}
template <class T>
void print(const vector<T>& v, int suc = 1) {
  for (int i = 0; i < v.size(); i++)
    print(v[i], i == (int)(v.size()) - 1 ? suc : 2);
}
const int INF = 0x3f3f3f3f;
const int maxn = 1e6 + 7;
string maze[maxn];
string ans[maxn];
string cur[maxn];
int n, m;
bool check(int T) {
  if (T * 2 >= n || T * 2 >= m) return false;
  vector<vector<int>> dp(n + 1, vector<int>(m + 1));
  vector<vector<int>> mark(n + 1, vector<int>(m + 1));
  dp[0][0] = (maze[0][0] == 'X');
  for (int i = 1; i < n; i++) dp[i][0] = dp[i - 1][0] + (maze[i][0] == 'X');
  for (int i = 1; i < m; i++) dp[0][i] = dp[0][i - 1] + (maze[0][i] == 'X');
  for (int i = 0; i < n; i++) {
    cur[i] = "";
    for (int j = 0; j < m; j++) cur[i] += '.';
  }
  for (int i = 1; i < n; i++)
    for (int j = 1; j < m; j++)
      dp[i][j] =
          -dp[i - 1][j - 1] + dp[i][j - 1] + dp[i - 1][j] + (maze[i][j] == 'X');
  for (int i = T; i < n - T; i++) {
    for (int j = T; j < m - T; j++) {
      if (maze[i][j] == 'X') {
        int rx = i + T, ry = j + T;
        int lx = i - T, ly = j - T;
        int sq = dp[rx][ry] - (ly == 0 ? 0 : dp[rx][ly - 1]) -
                 (lx == 0 ? 0 : dp[lx - 1][ry]) +
                 ((lx == 0 || ly == 0) ? 0 : dp[lx - 1][ly - 1]);
        if (sq == (2 * T + 1) * (2 * T + 1)) {
          cur[i][j] = 'X';
          mark[lx][ly]++;
          mark[rx + 1][ry + 1]++;
          mark[lx][ry + 1]--;
          mark[rx + 1][ly]--;
        }
      }
    }
  }
  vector<vector<int>> pre(n, vector<int>(m));
  pre[0][0] = mark[0][0];
  for (int i = 1; i < n; i++) pre[i][0] = pre[i - 1][0] + mark[i][0];
  for (int i = 1; i < m; i++) pre[0][i] = pre[0][i - 1] + mark[0][i];
  for (int i = 1; i < n; i++)
    for (int j = 1; j < m; j++)
      pre[i][j] =
          -pre[i - 1][j - 1] + pre[i - 1][j] + pre[i][j - 1] + mark[i][j];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (maze[i][j] == 'X' && !pre[i][j]) return false;
    }
  }
  return true;
}
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> maze[i];
  int L = 0, R = min(n, m) - 1;
  int op = 0;
  while (L <= R) {
    int mid = L + R >> 1;
    if (check(mid)) {
      L = mid + 1;
      op = mid;
      for (int i = 0; i < n; i++) ans[i] = cur[i];
    } else
      R = mid - 1;
  }
  cout << op << endl;
  for (int i = 0; i < n; i++) cout << ans[i] << '\n';
}
