#include <bits/stdc++.h>
using namespace std;
int const N = 1e6 + 10;
int n, m;
vector<string> s, Res;
vector<vector<int> > pre1, pre2;
char in[N];
void getRes(int t) {
  int tot = (t + t + 1) * (t + t + 1);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      Res[i][j] = '.';
      int i1 = i - t, j1 = j - t, i2 = i + t, j2 = j + t;
      if (i1 < 0 || j1 < 0 || i2 >= n || j2 >= m) continue;
      int sum = pre1[i2][j2];
      if (i1 > 0) sum -= pre1[i1 - 1][j2];
      if (j1 > 0) sum -= pre1[i2][j1 - 1];
      if (i1 > 0 && j1 > 0) sum += pre1[i1 - 1][j1 - 1];
      Res[i][j] = (sum == tot ? 'X' : '.');
    }
  }
}
bool check(int t) {
  getRes(t);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      pre2[i][j] = Res[i][j] == 'X';
      if (i > 0) pre2[i][j] += pre2[i - 1][j];
      if (j > 0) pre2[i][j] += pre2[i][j - 1];
      if (i > 0 && j > 0) pre2[i][j] -= pre2[i - 1][j - 1];
    }
  }
  int tot = (t + t + 1) * (t + t + 1);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int i1 = max(0, i - t), j1 = max(0, j - t), i2 = min(n - 1, i + t),
          j2 = min(m - 1, j + t);
      int sum = pre2[i2][j2];
      if (i1 > 0) sum -= pre2[i1 - 1][j2];
      if (j1 > 0) sum -= pre2[i2][j1 - 1];
      if (i1 > 0 && j1 > 0) sum += pre2[i1 - 1][j1 - 1];
      if (s[i][j] == 'X' && !sum) return false;
      if (s[i][j] == '.' && sum) return false;
    }
  }
  return true;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%s", in);
    s.push_back(string(in));
    Res.push_back(string(m, '.'));
    pre1.push_back(vector<int>(m, 0));
    pre2.push_back(vector<int>(m, 0));
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      pre1[i][j] = s[i][j] == 'X';
      if (i > 0) pre1[i][j] += pre1[i - 1][j];
      if (j > 0) pre1[i][j] += pre1[i][j - 1];
      if (i > 0 && j > 0) pre1[i][j] -= pre1[i - 1][j - 1];
    }
  }
  int mn = 0, mx = min(n, m), ans = 0;
  while (mn <= mx) {
    int mid = ((mn + mx) >> 1);
    if (check(mid)) {
      mn = mid + 1;
      ans = mid;
    } else {
      mx = mid - 1;
    }
  }
  getRes(ans);
  printf("%d\n", ans);
  for (int i = 0; i < n; i++) {
    printf("%s\n", Res[i].c_str());
  }
}
