#include <bits/stdc++.h>
using namespace std;
inline long long mod(long long n, long long m) {
  long long ret = n % m;
  if (ret < 0) ret += m;
  return ret;
}
long long gcd(long long a, long long b) {
  return (b == 0LL ? a : gcd(b, a % b));
}
long long exp(long long a, long long b, long long m) {
  if (b == 0LL) return 1LL;
  if (b == 1LL) return mod(a, m);
  long long k = mod(exp(a, b / 2, m), m);
  if (b & 1LL) {
    return mod(a * mod(k * k, m), m);
  } else
    return mod(k * k, m);
}
int n, m;
vector<vector<int> > pre;
vector<vector<int> > mat;
int qtd(int li, int lj, int ri, int rj) {
  if (li <= 0 or lj <= 0 or ri > n or rj > m) return 0;
  int val =
      pre[ri][rj] - pre[ri][lj - 1] - pre[li - 1][rj] + pre[li - 1][lj - 1];
  return val;
}
int ok(int t, int flag = 0) {
  vector<vector<int> > M(n + 1, vector<int>(m + 1, 0));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (qtd(i - t, j - t, i + t, j + t) == (2 * t + 1) * (2 * t + 1)) {
        M[i][j] = 1;
      }
    }
  }
  if (flag == 1) {
    cout << t << "\n";
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        if (M[i][j] == 1)
          cout << "X";
        else
          cout << ".";
      }
      cout << "\n";
    }
    exit(0);
  }
  vector<vector<int> > pre2(n + 2, vector<int>(m + 2, 0));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (M[i][j] == 1) {
        pre2[i - t][j - t]++;
        pre2[i - t][j + t + 1]--;
        pre2[i + t + 1][j - t]--;
        pre2[i + t + 1][j + t + 1]++;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      pre2[i][j] += pre2[i - 1][j] + pre2[i][j - 1] - pre2[i - 1][j - 1];
      if (mat[i][j] == 1 and pre2[i][j] == 0) return false;
    }
  }
  return true;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n >> m;
  mat.resize(n + 10, vector<int>(m + 10));
  pre.resize(n + 10, vector<int>(m + 10, 0));
  for (int i = 0; i <= n + 1; i++) {
    for (int j = 0; j <= m + 1; j++) {
      mat[i][j] = pre[i][j] = 0;
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      char c;
      cin >> c;
      mat[i][j] = (c == 'X' ? 1 : 0);
      pre[i][j] = mat[i][j];
      pre[i][0] = 0;
      pre[0][j] = 0;
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      pre[i][j] += (pre[i - 1][j] + pre[i][j - 1] - pre[i - 1][j - 1]);
    }
  }
  int l = 0, r = min(n, m);
  int ans = 0;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (ok(mid)) {
      ans = mid;
      l = mid + 1;
    } else
      r = mid - 1;
  }
  ok(ans, 1);
}
