#include <bits/stdc++.h>
using namespace std;
int n, m;
string L = "";
vector<vector<int>> matrix(n, vector<int>(m));
vector<vector<int>> SumsForMatrix(n, vector<int>(m));
vector<string> ans(n, L);
vector<vector<int>> changeMatrix(n, vector<int>(m));
void fillSums() {
  SumsForMatrix[0][0] = matrix[0][0];
  for (int i = 1; i < n; i++)
    SumsForMatrix[i][0] = SumsForMatrix[i - 1][0] + matrix[i][0];
  for (int j = 1; j < m; j++)
    SumsForMatrix[0][j] = SumsForMatrix[0][j - 1] + matrix[0][j];
  for (int i = 1; i < n; i++)
    for (int j = 1; j < m; j++)
      SumsForMatrix[i][j] = SumsForMatrix[i - 1][j] + SumsForMatrix[i][j - 1] -
                            SumsForMatrix[i - 1][j - 1] + matrix[i][j];
}
int findSumInRectangle(int y1, int x1, int y3, int x3) {
  int x2, x4, y2, y4, side = x3 - x1 + 1;
  x2 = x3;
  y2 = y1;
  x4 = x1;
  y4 = y3;
  int top = 0, left = 0, up = 0;
  if (x1 != 0 and y1 != 0) top = SumsForMatrix[y1 - 1][x1 - 1];
  if (x4 != 0) left = SumsForMatrix[y4][x4 - 1];
  if (y2 != 0) up = SumsForMatrix[y2 - 1][x2];
  return SumsForMatrix[y3][x3] + top - left - up;
}
int check(int l) {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (!(i >= l and j >= l and i < n - l and j < m - l)) continue;
      if (findSumInRectangle(i - l, j - l, i + l, j + l) ==
          (2 * l + 1) * (2 * l + 1)) {
        ans[i][j] = 'X';
        changeMatrix[i - l][j - l] += 1;
        if (j + l + 1 < m) changeMatrix[i - l][j + l + 1] += -1;
        if (i + l + 1 < n) changeMatrix[i + l + 1][j - l] += -1;
        if (j + l + 1 < m and i + l + 1 < n)
          changeMatrix[i + l + 1][j + l + 1] += 1;
      }
    }
  }
  for (int i = 1; i < n; i++) changeMatrix[i][0] += changeMatrix[i - 1][0];
  for (int j = 1; j < m; j++) changeMatrix[0][j] += changeMatrix[0][j - 1];
  for (int i = 1; i < n; i++)
    for (int j = 1; j < m; j++)
      changeMatrix[i][j] += changeMatrix[i - 1][j] + changeMatrix[i][j - 1] -
                            changeMatrix[i - 1][j - 1];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if ((changeMatrix[i][j] > 0 and matrix[i][j] > 0) or
          (changeMatrix[i][j] <= 0 and matrix[i][j] <= 0))
        continue;
      else
        return 0;
    }
  }
  return 1;
}
void bin() {
  int l = 0, r = min(n, m) / 2, mid = 0, fl1, fl2;
  while (l <= r) {
    mid = (l + r) / 2;
    ans.assign(n, L);
    changeMatrix.assign(n, vector<int>(m, 0));
    fl1 = check(mid + 1);
    ans.assign(n, L);
    changeMatrix.assign(n, vector<int>(m, 0));
    fl2 = check(mid);
    if (!fl1 and fl2) {
      cout << mid << endl;
      for (auto h : ans) {
        cout << h << '\n';
      }
      break;
    }
    if (fl2)
      l = mid + 1;
    else
      r = mid - 1;
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  char s;
  cin >> n >> m;
  for (int i = 0; i < m; i++) L += '.';
  matrix.assign(n, vector<int>(m));
  SumsForMatrix.assign(n, vector<int>(m));
  ans.assign(n, L);
  changeMatrix.assign(n, vector<int>(m, 0));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> s;
      matrix[i][j] = (s == 'X');
    }
  }
  fillSums();
  bin();
}
