#include <bits/stdc++.h>
const std::pair<int32_t, int32_t> offset[8] = {
    {-1, 0}, {1, 0}, {0, 1}, {0, -1}, {-1, -1}, {1, 1}, {-1, 1}, {1, -1}};
bool valid(int32_t row, int32_t col, int32_t n, int32_t m) {
  return !(row < 0 || row >= n || col < 0 || col >= m);
}
bool match(std::string* field, int32_t n, int32_t m, int32_t** depth,
           int32_t depth_start) {
  std::queue<std::pair<int32_t, int32_t> > queue;
  int32_t** dist = new int32_t*[n];
  for (int32_t i = 0; i < n; i++) {
    dist[i] = new int32_t[m];
    for (int32_t j = 0; j < m; j++) {
      dist[i][j] = INT32_MAX;
      if (depth[i][j] >= depth_start) {
        dist[i][j] = 0;
        queue.emplace(i, j);
      }
    }
  }
  while (!queue.empty()) {
    std::pair<int32_t, int32_t> cur = queue.front();
    queue.pop();
    if (dist[cur.first][cur.second] == depth_start - 1) break;
    for (int32_t k = 0; k < 8; k++) {
      std::pair<int32_t, int32_t> new_cell = {cur.first + offset[k].first,
                                              cur.second + offset[k].second};
      if (!valid(new_cell.first, new_cell.second, n, m)) continue;
      if (dist[new_cell.first][new_cell.second] == INT32_MAX) {
        dist[new_cell.first][new_cell.second] = dist[cur.first][cur.second] + 1;
        queue.push(new_cell);
      }
    }
  }
  bool ok = true;
  for (int32_t i = 0; i < n; i++) {
    for (int32_t j = 0; j < m; j++) {
      if ((dist[i][j] != INT32_MAX && field[i][j] == '.') ||
          (dist[i][j] == INT32_MAX && field[i][j] == 'X'))
        ok = false;
    }
    delete dist[i];
  }
  delete dist;
  return ok;
}
int main() {
  int32_t n, m;
  std::cin >> n >> m;
  std::string* field = new std::string[n];
  for (int32_t i = 0; i < n; i++) {
    std::cin >> field[i];
  }
  int32_t** depth = new int32_t*[n];
  for (int32_t i = 0; i < n; i++) {
    depth[i] = new int32_t[m];
    for (int32_t j = 0; j < m; j++) depth[i][j] = INT32_MAX;
  }
  std::vector<std::pair<int32_t, int32_t> > cur_last;
  for (int32_t i = -1; i <= n; i++) {
    cur_last.emplace_back(i, -1);
    cur_last.emplace_back(i, m);
  }
  for (int32_t i = -1; i <= m; i++) {
    cur_last.emplace_back(-1, i);
    cur_last.emplace_back(n, i);
  }
  for (int32_t i = 0; i < n; i++)
    for (int32_t j = 0; j < m; j++)
      if (field[i][j] == '.') {
        cur_last.emplace_back(i, j);
        depth[i][j] = 0;
      }
  std::vector<std::pair<int32_t, int32_t> > next;
  while (!cur_last.empty()) {
    for (int32_t i = 0; i < cur_last.size(); i++) {
      for (int32_t j = 0; j < 8; j++) {
        std::pair<int32_t, int32_t> new_cell = {
            cur_last[i].first + offset[j].first,
            cur_last[i].second + offset[j].second};
        if (!valid(new_cell.first, new_cell.second, n, m)) continue;
        if (valid(cur_last[i].first, cur_last[i].second, n, m)) {
          if (depth[new_cell.first][new_cell.second] >
              depth[cur_last[i].first][cur_last[i].second] + 1) {
            depth[new_cell.first][new_cell.second] =
                depth[cur_last[i].first][cur_last[i].second] + 1;
            next.push_back(new_cell);
          }
        } else {
          if (depth[new_cell.first][new_cell.second] == INT32_MAX) {
            depth[new_cell.first][new_cell.second] = 1;
            next.push_back(new_cell);
          }
        }
      }
    }
    cur_last = next;
    next.clear();
  }
  int32_t left = 1, right = n + m + 100;
  while (right - left > 1) {
    int32_t mid = (left + right) / 2;
    if (match(field, n, m, depth, mid))
      left = mid;
    else
      right = mid;
  }
  int32_t depth_ans = left;
  std::cout << depth_ans - 1 << "\n";
  for (int32_t i = 0; i < n; i++) {
    for (int32_t j = 0; j < m; j++) {
      std::cout << (depth[i][j] >= depth_ans ? 'X' : '.');
    }
    std::cout << "\n";
  }
  return 0;
};
