#include <bits/stdc++.h>
using namespace std;
void re(int& x);
template <class T, class... Ts>
void re(T& t, Ts&... ts);
int rows, cols;
char s[1100000];
void solve() {
  re(rows, cols);
  vector<string> board;
  for (int i = 0; i < rows; i++) {
    scanf("%s", s);
    board.push_back(s);
  }
  vector<vector<int> > sq(rows + 10, vector<int>(cols + 10, 0));
  for (int i = 1; i <= rows; i++) {
    for (int j = 1; j <= cols; j++) {
      if (board[i - 1][j - 1] == 'X')
        sq[i][j] = min(sq[i - 1][j - 1], min(sq[i - 1][j], sq[i][j - 1])) + 1;
      else
        sq[i][j] = 0;
    }
  }
  int st = 0, ed = min(rows, cols);
  while (st < ed) {
    int bs = (st + ed + 1) / 2;
    int md = 2 * bs + 1;
    vector<vector<int> > cover(rows + 10, vector<int>(cols + 10, 0));
    for (int i = 1; i <= rows; i++) {
      for (int j = 1; j <= cols; j++) {
        if (sq[i][j] >= md) {
          cover[i][j]++;
          cover[i - md][j]--;
          cover[i][j - md]--;
          cover[i - md][j - md]++;
        }
      }
    }
    for (int i = rows; i >= 1; i--) {
      for (int j = cols; j >= 1; j--) {
        cover[i][j] += cover[i + 1][j] + cover[i][j + 1] - cover[i + 1][j + 1];
      }
    }
    bool ok = true;
    for (int i = 1; i <= rows && ok; i++) {
      for (int j = 1; j <= cols; j++)
        if (board[i - 1][j - 1] == 'X' && cover[i][j] <= 0) {
          ok = false;
          break;
        }
    }
    if (ok)
      st = bs;
    else
      ed = bs - 1;
  }
  printf("%d\n", st);
  int md = 2 * st + 1;
  vector<string> outboard = board;
  for (int i = 1; i <= rows; i++) {
    for (int j = 1; j <= cols; j++) {
      if (i + st <= rows && j + st <= cols && sq[i + st][j + st] >= md)
        printf("X");
      else
        printf(".");
    }
    printf("\n");
  }
}
int main() { solve(); }
void re(int& x) { scanf("%d", &x); }
template <class T, class... Ts>
void re(T& t, Ts&... ts) {
  re(t);
  re(ts...);
}
