#include <bits/stdc++.h>
using namespace std;
const char BURN = 'X';
const char EMPTY = '.';
int N, M;
vector<string> forest;
vector<int *> forest_sums;
vector<string> start_grid;
vector<int *> start_sums;
int query_sums(const vector<int *> &sums, int r1, int r2, int c1, int c2) {
  return sums[r2][c2] - sums[r2][c1] - sums[r1][c2] + sums[r1][c1];
}
void build_rectangle_sums(const vector<string> &grid, vector<int *> &sums) {
  for (int i = 0; i < N; i++)
    for (int j = 0; j < M; j++)
      sums[i + 1][j + 1] =
          sums[i + 1][j] + sums[i][j + 1] - sums[i][j] + (grid[i][j] == BURN);
}
void build_start_grid(int T) {
  start_grid.assign(N, string(M, EMPTY));
  for (int i = 0; i < N; i++)
    for (int j = 0; j < M; j++) {
      int r1 = i - T;
      int r2 = i + T + 1;
      int c1 = j - T;
      int c2 = j + T + 1;
      if (0 <= r1 && r1 < r2 && r2 <= N && 0 <= c1 && c1 < c2 && c2 <= M) {
        long long area = (long long)(r2 - r1) * (c2 - c1);
        if (query_sums(forest_sums, r1, r2, c1, c2) == area)
          start_grid[i][j] = BURN;
      }
    }
}
bool possible(int T) {
  build_start_grid(T);
  build_rectangle_sums(start_grid, start_sums);
  for (int i = 0; i < N; i++)
    for (int j = 0; j < M; j++)
      if (forest[i][j] == BURN) {
        int r1 = max(i - T, 0);
        int r2 = min(i + T + 1, N);
        int c1 = max(j - T, 0);
        int c2 = min(j + T + 1, M);
        if (query_sums(start_sums, r1, r2, c1, c2) == 0) return false;
      }
  return true;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> N >> M;
  forest.resize(N);
  for (string &row : forest) cin >> row;
  forest_sums.assign(N + 1, nullptr);
  start_sums.assign(N + 1, nullptr);
  for (int i = 0; i <= N; i++) {
    forest_sums[i] = new int[M + 1]();
    start_sums[i] = new int[M + 1]();
  }
  build_rectangle_sums(forest, forest_sums);
  int low = 0, high = min(N, M) / 2;
  while (low < high) {
    int mid = (low + high + 1) / 2;
    if (possible(mid))
      low = mid;
    else
      high = mid - 1;
  }
  cout << low << '\n';
  build_start_grid(low);
  for (string &row : start_grid) cout << row << '\n';
}
