#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAXN = 1000005;
int n, m;
string s[MAXN];
vector<int> in[MAXN], G[MAXN];
int query1(int row1, int col1, int row2, int col2) {
  if (row1 <= 0 || row2 > n || col1 <= 0 || col2 > m) return 1;
  return in[row2][col2] - in[row2][col1 - 1] - in[row1 - 1][col2] +
         in[row1 - 1][col1 - 1];
}
int query2(int row1, int col1, int row2, int col2) {
  row2 = min(row2, n);
  col2 = min(col2, m);
  row1 = max(row1, 1);
  col1 = max(col1, 1);
  if (row1 <= 0 || row2 > n || col1 <= 0 || col2 > m) return 0;
  return G[row2][col2] - G[row2][col1 - 1] - G[row1 - 1][col2] +
         G[row1 - 1][col1 - 1];
}
bool check(int r) {
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (s[i - 1][j - 1] == 'O') continue;
      int val = query1(i - r, j - r, i + r, j + r);
      if (val >= 1)
        G[i][j] = 0;
      else
        G[i][j] = 1;
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      G[i][j] = G[i][j] + G[i - 1][j] + G[i][j - 1] - G[i - 1][j - 1];
    }
  }
  bool bad = false;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (s[i - 1][j - 1] == 'X') {
        int val = query2(i - r, j - r, i + r, j + r);
        if (val == 0) bad = true;
      }
    }
  }
  if (bad) return false;
  return true;
}
void backtrack(int r) {
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (s[i - 1][j - 1] == 'O') {
        cout << ".";
        continue;
      }
      int val = query1(i - r, j - r, i + r, j + r);
      if (val >= 1)
        G[i][j] = 0;
      else
        G[i][j] = 1;
      if (G[i][j] == 0)
        cout << ".";
      else
        cout << "X";
    }
    cout << '\n';
  }
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  while (cin >> n >> m) {
    for (int i = 0; i < n; ++i) cin >> s[i];
    for (int i = 0; i <= n; ++i) {
      G[i].clear();
      G[i].resize(m + 5);
      in[i].clear();
      in[i].resize(m + 5);
    }
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= m; ++j) {
        if (s[i - 1][j - 1] == 'X')
          in[i][j] = 0;
        else
          in[i][j] = 1;
      }
    }
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= m; ++j) {
        in[i][j] = in[i - 1][j] + in[i][j - 1] + in[i][j] - in[i - 1][j - 1];
      }
    }
    int l, r, mid;
    l = 0;
    r = min(n, m);
    while (l <= r) {
      mid = (l + r) >> 1;
      if (check(mid)) {
        l = mid + 1;
      } else {
        r = mid - 1;
      }
    }
    cout << r << '\n';
    backtrack(r);
  }
  return 0;
}
