#include <bits/stdc++.h>
using namespace std;
using ull = unsigned long long;
using ll = long long;
const int N = 2005;
int n, m;
vector<vector<char>> a;
vector<vector<int>> c, u;
bool check2(int i, int j, int i1, int j2) {
  if (i < 1 || j < 1 || i1 < 1 || j2 < 1) return 0;
  if (i > n || j > m || i1 > n || j2 > m) return 0;
  return c[i1][j2] - c[i - 1][j2] - c[i1][j - 1] + c[i - 1][j - 1] ==
         (i1 - i + 1) * (j2 - j + 1);
}
bool check(int vl) {
  for (int i = 0; i < n + 2; ++i) {
    for (int j = 0; j < m + 2; ++j) {
      u[i][j] = 0;
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (a[i][j] == 'X' && check2(i - vl, j - vl, i + vl, j + vl)) {
        ++u[i - vl][j - vl];
        --u[i - vl][j + vl + 1];
        --u[i + vl + 1][j - vl];
        ++u[i + vl + 1][j + vl + 1];
      }
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      u[i][j] += u[i - 1][j] + u[i][j - 1] - u[i - 1][j - 1];
      if (a[i][j] == 'X' && u[i][j] == 0) {
        return 0;
      }
      if (a[i][j] == '.' && u[i][j] != 0) {
        abort();
      }
    }
  }
  return 1;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  cin >> n >> m;
  a.resize(n + 2, vector<char>(m + 2, 0));
  c.resize(n + 2, vector<int>(m + 2, 0));
  u.resize(n + 2, vector<int>(m + 2, 0));
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      cin >> a[i][j];
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      c[i][j] = c[i - 1][j] + c[i][j - 1] - c[i - 1][j - 1] + (a[i][j] == 'X');
    }
  }
  int l = 0, r = max(n, m), mid;
  while (r - l > 1) {
    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) {
      if (a[i][j] == 'X' && check2(i - l, j - l, i + l, j + l)) {
        cout << 'X';
      } else {
        cout << '.';
      }
    }
    cout << '\n';
  }
  return 0;
}
