#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
template <typename T>
bool uax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
bool uin(T& a, const T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& p) {
  in >> p.first >> p.second;
  return in;
}
template <typename T1, typename T2>
ostream& operator<<(ostream& out, pair<T1, T2>& p) {
  out << p.first << ' ' << p.second;
  return out;
}
const int INF = 1e9 + 7;
int n, m;
vector<vector<char>> a;
vector<vector<int>> d, nd;
vector<pair<int, int>> pe;
bool check1(int i, int j) {
  if (i >= 0 && i < n + 2 && j >= 0 && j < m + 2) {
    if (d[i][j] == INF) {
      return true;
    }
  }
  return false;
}
void bfs() {
  d.resize(n + 2, vector<int>(m + 2, INF));
  nd = d;
  queue<pair<int, int>> q;
  for (auto e : pe) {
    d[e.first][e.second] = 0;
    q.push(e);
  }
  while (!q.empty()) {
    auto v = q.front();
    q.pop();
    for (int i = -1; i <= 1; i++) {
      for (int j = -1; j <= 1; j++) {
        if (check1(v.first + i, v.second + j)) {
          auto p = make_pair(v.first + i, v.second + j);
          q.push(p);
          d[p.first][p.second] = d[v.first][v.second] + 1;
        }
      }
    }
  }
}
bool check2(int i, int j) {
  if (i >= 0 && i < n + 2 && j >= 0 && j < m + 2) {
    if (nd[i][j] == INF && a[i][j] != '.') {
      return true;
    }
  }
  return false;
}
bool good(int t) {
  queue<pair<int, int>> q;
  for (int i = 0; i < n + 2; i++) {
    for (int j = 0; j < m + 2; j++) {
      if (d[i][j] > t) {
        nd[i][j] = 0;
        q.push(make_pair(i, j));
      } else {
        nd[i][j] = INF;
      }
    }
  }
  while (!q.empty()) {
    auto v = q.front();
    q.pop();
    for (int i = -1; i <= 1; i++) {
      for (int j = -1; j <= 1; j++) {
        if (check2(v.first + i, v.second + j)) {
          auto p = make_pair(v.first + i, v.second + j);
          q.push(p);
          nd[p.first][p.second] = nd[v.first][v.second] + 1;
        }
      }
    }
  }
  for (int i = 0; i < n + 2; i++) {
    for (int j = 0; j < m + 2; j++) {
      if (a[i][j] == '.') continue;
      if (nd[i][j] > t) return false;
    }
  }
  return true;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  a.resize(n + 2, vector<char>(m + 2, '.'));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];
    }
  }
  for (int i = 0; i < n + 2; i++) {
    for (int j = 0; j < m + 2; j++) {
      if (a[i][j] == '.') pe.push_back(make_pair(i, j));
    }
  }
  bfs();
  int l = 0, r = 1e3 + 7, mid;
  while (r > l + 1) {
    mid = (l + r) / 2;
    if (good(mid)) {
      l = mid;
    } else
      r = mid;
  }
  cout << l << '\n';
  for (int i = 1; i < n + 1; i++) {
    for (int j = 1; j < m + 1; j++) {
      if (d[i][j] > l)
        cout << 'X';
      else
        cout << '.';
    }
    cout << '\n';
  }
}
