#include <bits/stdc++.h>
using namespace std;
int n, m;
int total_num = 0;
int* pool;
int** grid;
int* prefix_sum_pool;
int** prefix_sum;
int* prefix2_pool;
int** prefix2;
int get_sum(int minR, int minC, int maxR, int maxC) {
  return prefix_sum[maxR][maxC] - (minR == 0 ? 0 : prefix_sum[minR - 1][maxC]) -
         (minC == 0 ? 0 : prefix_sum[maxR][minC - 1]) +
         (minR == 0 || minC == 0 ? 0 : prefix_sum[minR - 1][minC - 1]);
}
bool works(int time) {
  int len = 2 * time + 1;
  for (int i = 0; i < n * m; i++) {
    prefix2_pool[i] = 0;
  }
  prefix_sum[0][0] = grid[0][0];
  for (int i = 1; i < m; i++) {
    prefix_sum[0][i] = prefix_sum[0][i - 1] + grid[0][i];
  }
  for (int i = 1; i < n; i++) {
    prefix_sum[i][0] = prefix_sum[i - 1][0] + grid[i][0];
  }
  for (int i = 1; i < n; i++) {
    for (int j = 1; j < m; j++) {
      prefix_sum[i][j] = prefix_sum[i - 1][j] + prefix_sum[i][j - 1] -
                         prefix_sum[i - 1][j - 1] + grid[i][j];
    }
  }
  vector<pair<int, int>> centers;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int minR = i - time;
      int minC = j - time;
      int maxR = i + time;
      int maxC = j + time;
      if (minR < 0 || minC < 0 || maxR >= n || maxC >= m) continue;
      int sum = get_sum(minR, minC, maxR, maxC);
      if (sum == len * len) {
        centers.push_back({i, j});
        for (int i = minR; i <= maxR; i++) {
          prefix2[i][minC] += 1;
          if (maxC + 1 < m) prefix2[i][maxC + 1] -= 1;
        }
      }
    }
  }
  int count = 0;
  for (int i = 0; i < n; i++) {
    int running_sum = 0;
    for (int j = 0; j < m; j++) {
      running_sum += prefix2[i][j];
      if (running_sum > 0) count++;
    }
  }
  return count == total_num;
}
void gen_config(int time) {
  bool* thing = new bool[n * m];
  bool** thing2 = new bool*[n];
  for (int i = 0; i < n; i++) {
    thing2[i] = thing + i * m;
  }
  for (int i = 0; i < n * m; i++) {
    thing[i] = false;
  }
  int len = 2 * time + 1;
  for (int i = 0; i < n * m; i++) {
    prefix2_pool[i] = 0;
  }
  prefix_sum[0][0] = grid[0][0];
  for (int i = 1; i < m; i++) {
    prefix_sum[0][i] = prefix_sum[0][i - 1] + grid[0][i];
  }
  for (int i = 1; i < n; i++) {
    prefix_sum[i][0] = prefix_sum[i - 1][0] + grid[i][0];
  }
  for (int i = 1; i < n; i++) {
    for (int j = 1; j < m; j++) {
      prefix_sum[i][j] = prefix_sum[i - 1][j] + prefix_sum[i][j - 1] -
                         prefix_sum[i - 1][j - 1] + grid[i][j];
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int minR = i - time;
      int minC = j - time;
      int maxR = i + time;
      int maxC = j + time;
      if (minR < 0 || minC < 0 || maxR >= n || maxC >= m) continue;
      int sum = get_sum(minR, minC, maxR, maxC);
      if (sum == len * len) {
        thing2[i][j] = true;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cout << (thing2[i][j] ? 'X' : '.');
    }
    cout << '\n';
  }
  delete[] thing;
  delete[] thing2;
}
int main() {
  cin >> n >> m;
  pool = new int[n * m];
  prefix_sum_pool = new int[n * m];
  prefix2_pool = new int[n * m];
  for (int i = 0; i < n * m; i++) {
    pool[i] = 0;
    prefix_sum_pool[i] = 0;
    prefix2_pool[i] = 0;
  }
  grid = new int*[n];
  prefix_sum = new int*[n];
  prefix2 = new int*[n];
  for (int i = 0; i < n; i++) {
    grid[i] = pool + i * m;
    prefix_sum[i] = prefix_sum_pool + i * m;
    prefix2[i] = prefix2_pool + i * m;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      char c = 0;
      cin >> c;
      grid[i][j] = c == 'X';
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      total_num += grid[i][j];
    }
  }
  int low = 0;
  int high = min(n / 2, m / 2) + 1;
  int last_worked = 0;
  while (low < high) {
    int mid = (low + high) / 2;
    if (works(mid)) {
      last_worked = mid;
      low = mid + 1;
    } else {
      high = mid;
    }
  }
  cout << last_worked << endl;
  gen_config(last_worked);
  delete[] pool;
  delete[] grid;
  delete[] prefix_sum;
  delete[] prefix_sum_pool;
  delete[] prefix2_pool;
  delete[] prefix2;
}
