#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
const int M = 1e6 + 5;
const int mod = 1e9 + 7;
const int m1 = 1e9 + 7;
const int m2 = 1e9 + 9;
const int p1 = 402653189;
const int p2 = 1610612741;
const int LN = 32;
int powermodm(int x, int n, int M) {
  int result = 1;
  while (n > 0) {
    if (n % 2 == 1) result = (result * x) % M;
    x = (x * x) % M;
    n = n / 2;
  }
  return result;
}
int power(int _a, int _b) {
  int _r = 1;
  while (_b) {
    if (_b % 2 == 1) _r = (_r * _a);
    _b /= 2;
    _a = (_a * _a);
  }
  return _r;
}
int gcd(int a, int b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
int lcm(int a, int b) { return (max(a, b) / gcd(a, b)) * min(a, b); }
int dx[8] = {-1, 1, 0, 0};
int dy[8] = {0, 0, 1, -1};
int pw[LN], fact[N], invfact[N];
void pre() {
  fact[0] = 1;
  for (int i = 0; i < LN; i++) pw[i] = power(2, i);
  for (int i = 1; i < N; i++) fact[i] = (i * fact[i - 1]) % mod;
  for (int i = 0; i < N; i++) invfact[i] = powermodm(fact[i], mod - 2, mod);
}
int n, m;
vector<vector<char> > arr(M), temp(M);
vector<vector<int> > dp(M), rut(M), dist(M);
void init() {
  for (int i = 0; i <= n + 1; i++) {
    for (int j = 0; j <= m + 1; j++) {
      temp[i][j] = '.';
      dist[i][j] = 1e8;
      dp[i][j] = 1e8;
    }
  }
}
bool check() {
  for (int i = 0; i <= n + 1; i++) {
    for (int j = 0; j <= m + 1; j++) {
      if (arr[i][j] != temp[i][j]) return false;
    }
  }
  return true;
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int i, j;
  cin >> n >> m;
  for (i = 0; i <= n + 1; i++) {
    dp[i].resize(m + 2);
    arr[i].resize(m + 2);
    rut[i].resize(m + 2);
    temp[i].resize(m + 2);
    dist[i].resize(m + 2);
    for (j = 0; j <= m + 1; j++) {
      arr[i][j] = '.';
      dp[i][j] = 0;
      rut[i][j] = 1e8;
    }
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      cin >> arr[i][j];
    }
  }
  for (i = 1; i <= n + 1; i++) {
    for (j = 1; j <= m + 1; j++) {
      if (arr[i][j] == 'X')
        dp[i][j] = min(dp[i - 1][j - 1], min(dp[i - 1][j], dp[i][j - 1])) + 1;
      else
        dp[i][j] = 0;
      rut[i][j] = min(rut[i][j], dp[i][j]);
    }
  }
  for (i = 0; i <= n + 1; i++)
    for (j = 0; j <= m + 1; j++) dp[i][j] = 0;
  for (i = 1; i <= n + 1; i++) {
    for (j = m; j >= 0; j--) {
      if (arr[i][j] == 'X')
        dp[i][j] = min(dp[i - 1][j + 1], min(dp[i - 1][j], dp[i][j + 1])) + 1;
      else
        dp[i][j] = 0;
      rut[i][j] = min(rut[i][j], dp[i][j]);
    }
  }
  for (i = 0; i <= n + 1; i++)
    for (j = 0; j <= m + 1; j++) dp[i][j] = 0;
  for (i = n; i >= 0; i--) {
    for (j = 1; j <= m + 1; j++) {
      if (arr[i][j] == 'X')
        dp[i][j] = min(dp[i + 1][j - 1], min(dp[i + 1][j], dp[i][j - 1])) + 1;
      else
        dp[i][j] = 0;
      rut[i][j] = min(rut[i][j], dp[i][j]);
    }
  }
  for (i = 0; i <= n + 1; i++)
    for (j = 0; j <= m + 1; j++) dp[i][j] = 0;
  for (i = n; i >= 0; i--) {
    for (j = m; j >= 0; j--) {
      if (arr[i][j] == 'X')
        dp[i][j] = min(dp[i + 1][j + 1], min(dp[i + 1][j], dp[i][j + 1])) + 1;
      else
        dp[i][j] = 0;
      rut[i][j] = min(rut[i][j], dp[i][j]);
    }
  }
  int l = 1, r = min(n, m), ma = 0;
  while (l <= r) {
    int mid = (l + r) >> 1;
    init();
    for (i = 1; i <= n + 1; i++) {
      for (j = 1; j <= m + 1; j++) {
        if (rut[i][j] > mid)
          dist[i][j] = 0;
        else
          dist[i][j] =
              min(dist[i - 1][j - 1], min(dist[i - 1][j], dist[i][j - 1])) + 1;
        dp[i][j] = min(dp[i][j], dist[i][j]);
      }
    }
    for (i = 0; i <= n + 1; i++)
      for (j = 0; j <= m + 1; j++) dist[i][j] = 1e8;
    for (i = 1; i <= n + 1; i++) {
      for (j = m; j >= 0; j--) {
        if (rut[i][j] > mid)
          dist[i][j] = 0;
        else
          dist[i][j] =
              min(dist[i - 1][j + 1], min(dist[i - 1][j], dist[i][j + 1])) + 1;
        dp[i][j] = min(dp[i][j], dist[i][j]);
      }
    }
    for (i = 0; i <= n + 1; i++)
      for (j = 0; j <= m + 1; j++) dist[i][j] = 1e8;
    for (i = n; i >= 0; i--) {
      for (j = 1; j <= m + 1; j++) {
        if (rut[i][j] > mid)
          dist[i][j] = 0;
        else
          dist[i][j] =
              min(dist[i + 1][j - 1], min(dist[i + 1][j], dist[i][j - 1])) + 1;
        dp[i][j] = min(dp[i][j], dist[i][j]);
      }
    }
    for (i = 0; i <= n + 1; i++)
      for (j = 0; j <= m + 1; j++) dist[i][j] = 1e8;
    for (i = n; i >= 0; i--) {
      for (j = m; j >= 0; j--) {
        if (rut[i][j] > mid)
          dist[i][j] = 0;
        else
          dist[i][j] =
              min(dist[i + 1][j + 1], min(dist[i + 1][j], dist[i][j + 1])) + 1;
        dp[i][j] = min(dp[i][j], dist[i][j]);
      }
    }
    for (i = 0; i <= n + 1; i++) {
      for (j = 0; j <= m + 1; j++) {
        if (dp[i][j] <= mid) temp[i][j] = 'X';
      }
    }
    if (check()) {
      ma = max(ma, mid);
      l = mid + 1;
    } else
      r = mid - 1;
  }
  cout << ma << "\n";
  if (ma == 0) {
    for (i = 1; i <= n; i++) {
      for (j = 1; j <= m; j++) {
        cout << arr[i][j];
      }
      cout << "\n";
    }
    return 0;
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      if (rut[i][j] > ma)
        cout << "X";
      else
        cout << ".";
    }
    cout << "\n";
  }
  return 0;
}
