#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  int psa[n + 5][m + 5];
  char grid[n + 5][m + 5];
  int cnt = 0;
  for (int i = 0; i < n + 5; i++)
    for (int j = 0; j < m + 5; j++) psa[i][j] = 0;
  for (int i = 1; i <= n; i++) {
    string str;
    cin >> str;
    for (int j = 1; j <= m; j++) {
      grid[i][j] = str[j - 1];
      if (str[j - 1] == 'X') {
        psa[i][j] = 1;
        cnt++;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      psa[i][j] += psa[i - 1][j] + psa[i][j - 1] - psa[i - 1][j - 1];
    }
  }
  int l = 0;
  int r = n + m;
  while (r - l > 3) {
    int mid = (r - l) / 2 + l;
    int d[n + 5][m + 5];
    for (int i = 0; i < n + 5; i++)
      for (int j = 0; j < m + 5; j++) d[i][j] = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        if (i + mid <= n && j + mid <= m && i - mid >= 1 && j - mid >= 1) {
          int trees = psa[i + mid][j + mid] - psa[i + mid][j - mid - 1] -
                      psa[i - mid - 1][j + mid] + psa[i - mid - 1][j - mid - 1];
          int seek = (mid * 2 + 1) * (mid * 2 + 1);
          if (trees == seek) {
            d[i - mid][j - mid]++, d[i - mid][j + mid + 1]--,
                d[i + mid + 1][j - mid]--, d[i + mid + 1][j + mid + 1]++;
          }
        }
      }
    }
    int cur = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        d[i][j] += d[i - 1][j] + d[i][j - 1] - d[i - 1][j - 1];
        if (d[i][j] > 0) cur++;
      }
    }
    if (cur >= cnt)
      l = mid;
    else
      r = mid;
  }
  for (int mid = r; mid >= l; mid--) {
    int d[n + 5][m + 5];
    for (int i = 0; i < n + 5; i++)
      for (int j = 0; j < m + 5; j++) d[i][j] = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        if (i + mid <= n && j + mid <= m && i - mid >= 1 && j - mid >= 1) {
          int trees = psa[i + mid][j + mid] - psa[i + mid][j - mid - 1] -
                      psa[i - mid - 1][j + mid] + psa[i - mid - 1][j - mid - 1];
          int seek = (mid * 2 + 1) * (mid * 2 + 1);
          if (trees == seek) {
            d[i - mid][j - mid]++, d[i - mid][j + mid + 1]--,
                d[i + mid + 1][j - mid]--, d[i + mid + 1][j + mid + 1]++;
          }
        }
      }
    }
    int cur = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        d[i][j] += d[i - 1][j] + d[i][j - 1] - d[i - 1][j - 1];
        if (d[i][j] > 0) cur++;
      }
    }
    if (cur >= cnt) {
      cout << mid << '\n';
      for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
          if (grid[i][j] == 'X') {
            if (i + mid <= n && j + mid <= m && i - mid >= 1 && j - mid >= 1) {
              int trees = psa[i + mid][j + mid] - psa[i + mid][j - mid - 1] -
                          psa[i - mid - 1][j + mid] +
                          psa[i - mid - 1][j - mid - 1];
              int seek = (mid * 2 + 1) * (mid * 2 + 1);
              if (trees == seek)
                cout << 'X';
              else
                cout << '.';
            } else
              cout << '.';
          } else
            cout << '.';
        }
        cout << '\n';
      }
      break;
    }
  }
}
