#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<vector<int>> board(n, vector<int>(m)), sum(n + 1, vector<int>(m + 1));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      char c;
      cin >> c;
      if (c == 'X') board[i][j] = sum[i + 1][j + 1] = 1;
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      sum[i][j] += sum[i - 1][j];
    }
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      sum[i][j] += sum[i][j - 1];
    }
  }
  auto can_paint = [&](int i, int j, int sz) -> bool {
    if (i + sz >= n || i - sz < 0 || j + sz >= m || j - sz < 0) return false;
    int e = 2 * sz + 1;
    return e * e == sum[i + sz + 1][j + sz + 1] - sum[i + sz + 1][j - sz] -
                        sum[i - sz][j + sz + 1] + sum[i - sz][j - sz];
  };
  int dx[] = {-1, 0, 1, 1, 1, 0, -1, -1};
  int dy[] = {1, 1, 1, 0, -1, -1, -1, 0};
  auto valid = [&](int i, int j) -> bool {
    return 0 <= i && i < n && 0 <= j && j < m;
  };
  auto check = [&](const vector<pair<int, int>> &paint, int T) -> bool {
    vector<vector<int>> bo(n, vector<int>(m, 1e9));
    queue<pair<int, int>> que;
    for (auto &p : paint) {
      bo[p.first][p.second] = 0;
      que.push(p);
    }
    while (!que.empty()) {
      auto p = que.front();
      que.pop();
      int t = bo[p.first][p.second];
      if (t + 1 > T) continue;
      for (int i = 0; i < 8; i++) {
        int x = p.first + dx[i], y = p.second + dy[i];
        if (!valid(x, y)) return false;
        if (t + 1 < bo[x][y]) {
          bo[x][y] = t + 1;
          que.emplace(x, y);
        }
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (board[i][j] != (bo[i][j] != 1e9)) return false;
      }
    }
    return true;
  };
  auto make_paint = [&](int sz) -> vector<pair<int, int>> {
    vector<pair<int, int>> paint;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (can_paint(i, j, sz)) {
          paint.emplace_back(i, j);
        }
      }
    }
    return paint;
  };
  auto ok = [&](int sz) -> bool { return check(make_paint(sz), sz); };
  int l = 0, r = (min(n, m) - 1) / 2 + 1;
  while (l < r - 1) {
    int mid = (l + r) / 2;
    if (ok(mid)) {
      l = mid;
    } else {
      r = mid;
    }
  }
  cout << l << endl;
  vector<string> ans(n, string(m, '.'));
  auto paint = make_paint(l);
  for (auto &p : paint) {
    ans[p.first][p.second] = 'X';
  }
  for (auto &s : ans) {
    cout << s << '\n';
  }
}
