#include <bits/stdc++.h>
using namespace std;
const int NMAX = 1e6 + 5;
vector<vector<int>> v, sp, ini;
int N, M;
vector<pair<int, int>> trees;
vector<pair<int, int>> sol;
bool can_place(int x, int y, int lg) {
  int a = x + lg - 1, b = y + lg - 1;
  if (x > N || y > M) return 0;
  if (a > N || b > M) return 0;
  if (sp[a][b] - sp[x - 1][b] - sp[a][y - 1] + sp[x - 1][y - 1] < lg * lg)
    return 0;
  trees.push_back({x + lg / 2, y + lg / 2});
  return 1;
}
void save() { sol = trees; }
bool OK(int t) {
  int meh = 2 * t + 1;
  v = vector<vector<int>>(N + 3, vector<int>(M + 3, 0));
  trees.clear();
  for (int i = 1; i <= N - meh + 2; ++i)
    for (int j = 1; j <= M - meh + 2; ++j)
      if (can_place(i, j, t * 2 + 1)) {
        v[i][j] += 1;
        v[i][j + t * 2 + 1] -= 1;
        v[i + t * 2 + 1][j] -= 1;
        v[i + t * 2 + 1][j + t * 2 + 1] += 1;
      }
  for (int i = 1; i <= N; ++i) {
    for (int j = 1; j <= M; ++j) {
      v[i][j] += (v[i - 1][j] + v[i][j - 1] - v[i - 1][j - 1]);
      if (ini[i][j] && v[i][j] < 1) return 0;
    }
  }
  return 1;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> N >> M;
  sp = vector<vector<int>>(N + 3, vector<int>(M + 3, 0));
  ini = sp;
  for (int i = 1; i <= N; ++i) {
    string str;
    cin >> str;
    for (int j = 1; j <= M; ++j) {
      sp[i][j] = (str[j - 1] == 'X');
      ini[i][j] = sp[i][j];
      sp[i][j] += (sp[i - 1][j] + sp[i][j - 1] - sp[i - 1][j - 1]);
    }
  }
  int ans = 0;
  int step = 1;
  while (step < min(N, M)) step *= 2;
  while (step) {
    if (OK(ans + step)) {
      ans += step;
      save();
    }
    step /= 2;
  }
  cout << ans << '\n';
  if (ans == 0) {
    for (int i = 1; i <= N; ++i) {
      for (int j = 1; j <= M; ++j) {
        cout << (ini[i][j] ? 'X' : '.');
      }
      cout << '\n';
    }
  } else {
    sp = vector<vector<int>>(N + 3, vector<int>(M + 3, 0));
    for (auto pp : sol) sp[pp.first][pp.second] = 1;
    for (int i = 1; i <= N; ++i) {
      for (int j = 1; j <= M; ++j) {
        cout << (sp[i][j] ? 'X' : '.');
      }
      cout << '\n';
    }
  }
  return 0;
}
