#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
const long long mod = 1e9 + 7;
const long long N = 1e6 + 5, K = 105;
const int si = (1 << 20);
const long long M = 50 + 5;
const double INFdb = 1e10;
int n, m;
vector<int> matrix[N];
vector<int> S[N];
vector<int> mask[N], tmp[N];
vector<bool> ans[N];
bool check(int x) {
  int delta = x * 2 + 1;
  for (int i = 0; i <= n + 1; ++i) {
    for (int j = 0; j <= m + 1; ++j) {
      mask[i][j] = 0;
      tmp[i][j] = 0;
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (i <= n - delta + 1 && j <= m - delta + 1) {
        int check1 = S[i + delta - 1][j + delta - 1] + S[i - 1][j - 1] -
                     S[i + delta - 1][j - 1] - S[i - 1][j + delta - 1];
        if (check1 == delta * delta) {
          mask[i][j] = 1;
        }
      }
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      tmp[i][j] =
          mask[i][j] + tmp[i - 1][j] + tmp[i][j - 1] - tmp[i - 1][j - 1];
    }
  }
  bool check2 = 1;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (matrix[i][j]) {
        int check1 = tmp[i][j];
        if (i > delta) check1 -= tmp[i - delta][j];
        if (j > delta) check1 -= tmp[i][j - delta];
        if (i > delta && j > delta) check1 += tmp[i - delta][j - delta];
        if (!check1) {
          check2 = 0;
        }
      }
    }
  }
  if (check2) return 1;
  return 0;
}
void init() {
  for (int i = 0; i < n + 5; ++i) {
    matrix[i].resize(m + 5);
    S[i].resize(m + 5);
    mask[i].resize(m + 5);
    tmp[i].resize(m + 5);
    ans[i].resize(m + 5);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(0);
  cin >> n >> m;
  init();
  for (int i = 1; i <= n; ++i) {
    string s;
    cin >> s;
    for (int j = 0; j < s.size(); ++j) {
      if (s[j] == 'X') matrix[i][j + 1] = 1;
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j)
      S[i][j] = matrix[i][j] + S[i - 1][j] + S[i][j - 1] - S[i - 1][j - 1];
  }
  int l = 0, r = (n + m) / 4;
  int res = (l + r) >> 1;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(mid)) {
      res = mid;
      l = mid + 1;
    } else
      r = mid - 1;
  }
  cout << res << "\n";
  check(res);
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (mask[i][j]) ans[i + res][j + res] = 1;
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (ans[i][j])
        cout << "X";
      else
        cout << ".";
    }
    cout << "\n";
  }
  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
  return 0;
}
