#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T &x) {
  char c;
  bool nega = 0;
  while ((!isdigit(c = getchar())) && (c != '-'))
    ;
  if (c == '-') {
    nega = 1;
    c = getchar();
  }
  x = c - 48;
  while (isdigit(c = getchar())) x = x * 10 + c - 48;
  if (nega) x = -x;
}
template <typename T>
inline void writep(T x) {
  if (x > 9) writep(x / 10);
  putchar(x % 10 + 48);
}
template <typename T>
inline void write(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  writep(x);
  putchar(' ');
}
template <typename T>
inline void writeln(T x) {
  write(x);
  putchar('\n');
}
template <typename R, typename D>
inline void Min(R &a, D b) {
  if (a > b) a = b;
}
template <typename D, typename R>
inline void Max(D &a, R b) {
  if (a < b) a = b;
}
const int N = 200005;
int n, m;
vector<vector<int>> sum, sum2;
vector<vector<bool>> a;
int get(int x, int y, int z, int t) {
  return sum[z][t] - sum[x - 1][t] - sum[z][y - 1] + sum[x - 1][y - 1];
}
bool check(int x) {
  int sqsize = (x << 1) + 1;
  int second = sqsize * sqsize;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) sum2[i][j] = 0;
  }
  for (int i = sqsize; i <= n; ++i) {
    for (int j = sqsize; j <= m; ++j)
      if (get(i - sqsize + 1, j - sqsize + 1, i, j) == second) {
        ++sum2[i - sqsize + 1][j - sqsize + 1];
        ++sum2[i + 1][j + 1];
        --sum2[i - sqsize + 1][j + 1];
        --sum2[i + 1][j - sqsize + 1];
      }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      sum2[i + 1][j] += sum2[i][j];
      sum2[i][j] += sum2[i][j - 1];
      if (a[i][j] && !sum2[i][j]) {
        return 0;
      }
    }
  }
  return 1;
}
int main() {
  ios_base::sync_with_stdio(NULL);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> m;
  sum = vector<vector<int>>(n + 2, vector<int>(m + 2));
  sum2 = vector<vector<int>>(n + 2, vector<int>(m + 2));
  a = vector<vector<bool>>(n + 2, vector<bool>(m + 2));
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) {
      char x;
      cin >> x;
      if (x == 'X') {
        a[i][j] = 1;
      }
    }
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + a[i][j];
  int r = (min(m, n) + 1) / 2;
  int l = 0;
  while (r - l > 1) {
    int mid = l + r >> 1;
    if (check(mid))
      l = mid;
    else
      r = mid;
  }
  cout << l << '\n';
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) sum2[i][j] = 0;
  }
  int ll = l;
  l = (l << 1) + 1;
  r = l * l;
  for (int i = l; i <= n; ++i) {
    for (int j = l; j <= m; ++j)
      if (get(i - l + 1, j - l + 1, i, j) == r) {
        ++sum2[i - ll][j - ll];
      }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) cout << (sum2[i][j] ? 'X' : '.');
    cout << '\n';
  }
}
