#include <bits/stdc++.h>
using namespace std;
template <class T>
void dbs(string str, T t) {
  cerr << str << " : " << t << "\n";
}
template <class T, class... S>
void dbs(string str, T t, S... s) {
  int idx = str.find(',');
  cerr << str.substr(0, idx) << " : " << t << ",";
  dbs(str.substr(idx + 1), s...);
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
template <class T>
void debug(T a, T b) {
  cerr << "[";
  for (T i = a; i != b; ++i) {
    if (i != a) cerr << ", ";
    cerr << *i;
  }
  cerr << "]\n";
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  vector<vector<int>> level(n, vector<int>(m, -1));
  queue<pair<int, int>> q;
  vector<string> bfsGrid;
  auto bfs = [n, m, &level, &q](vector<string> &grid, char expandChar,
                                char newChar, int maxLevel) {
    level.assign(n, vector<int>(m, -1));
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < m; ++j) {
        level[i][j] = -1;
        if (grid[i][j] != 'X') {
          continue;
        }
        bool ok = false;
        for (int ii = i - 1; ii <= i + 1; ++ii) {
          for (int jj = j - 1; jj <= j + 1; ++jj) {
            if (ii < 0 or ii >= n or jj < 0 or jj >= m) {
              ok = true;
              break;
            }
            if (grid[ii][jj] == '.') {
              ok = true;
              break;
            }
          }
        }
        if (ok) {
          q.push({i, j});
          level[i][j] = 0;
        }
      }
    }
    while (!q.empty()) {
      auto p = q.front();
      q.pop();
      int x = p.first, y = p.second;
      if (level[x][y] >= maxLevel) {
        continue;
      }
      grid[x][y] = newChar;
      for (int ii = x - 1; ii <= x + 1; ++ii) {
        for (int jj = y - 1; jj <= y + 1; ++jj) {
          if (ii >= 0 and ii < n and jj >= 0 and jj < m) {
            if (grid[ii][jj] == expandChar and level[ii][jj] == -1) {
              level[ii][jj] = level[x][y] + 1;
              q.push({ii, jj});
            }
          }
        }
      }
    }
  };
  vector<string> grid(n);
  for (int i = 0; i < n; ++i) {
    cin >> grid[i];
  }
  int low = 0, high = max(n, m);
  while (low < high) {
    int mid = (low + high + 1) >> 1;
    bfsGrid = grid;
    bfs(bfsGrid, 'X', '.', mid);
    bfs(bfsGrid, '.', 'X', mid + 1);
    bool ok = true;
    for (int i = 0; i < n; ++i) {
      if (bfsGrid[i] != grid[i]) {
        ok = false;
        break;
      }
    }
    if (!ok) {
      high = mid - 1;
    } else {
      low = mid;
    }
  }
  cout << low << "\n";
  bfs(grid, 'X', '.', low);
  for (int i = 0; i < n; ++i) {
    cout << grid[i] << "\n";
  }
  return 0;
}
