#include <bits/stdc++.h>
using namespace std;
const int BIG = (int)(1e9);
void solve();
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  solve();
  return 0;
}
int nbLig, nbCol;
vector<vector<int>> tab;
vector<vector<int>> pref;
vector<vector<int>> *coucou;
inline int getSomme(int l1, int c1, int l2, int c2) {
  auto &p = (*coucou);
  int tot = p[l2][c2];
  if (l1) tot -= p[l1 - 1][c2];
  if (c1) tot -= p[l2][c1 - 1];
  if (l1 && c1) tot += p[l1 - 1][c1 - 1];
  return tot;
}
void genPref(vector<vector<int>> &tt, vector<vector<int>> &pp) {
  for (int lig = (0); lig < (nbLig); ++lig)
    for (int col = (0); col < (nbCol); ++col) {
      pp[lig][col] = tt[lig][col];
      if (lig) pp[lig][col] += pp[lig - 1][col];
      if (col) pp[lig][col] += pp[lig][col - 1];
      if (lig && col) pp[lig][col] -= pp[lig - 1][col - 1];
    }
}
vector<vector<int>> pp;
bool fonctionne(int tps, bool afficher) {
  coucou = &pref;
  auto grille = tab;
  for (int lig = (0); lig < (nbLig); ++lig)
    for (int col = (0); col < (nbCol); ++col) {
      int ligDeb = lig - tps, colDeb = col - tps;
      int ligFin = lig + tps, colFin = col + tps;
      bool b = true;
      if (ligDeb < 0 || colDeb < 0 || ligFin >= nbLig || colFin >= nbCol)
        b = false;
      if (b && getSomme(ligDeb, colDeb, ligFin, colFin) == 0)
        grille[lig][col] = 1;
      else
        grille[lig][col] = 0;
    }
  genPref(grille, pp);
  coucou = &pp;
  for (int lig = (0); lig < (nbLig); ++lig)
    for (int col = (0); col < (nbCol); ++col)
      if (tab[lig][col] == 0) {
        int ligDeb = max(0, lig - tps), colDeb = max(0, col - tps);
        int ligFin = min(nbLig - 1, lig + tps),
            colFin = min(nbCol - 1, col + tps);
        if (getSomme(ligDeb, colDeb, ligFin, colFin) == 0) return false;
      }
  if (afficher) {
    for (int lig = (0); lig < (nbLig); ++lig) {
      for (int col = (0); col < (nbCol); ++col) {
        if (grille[lig][col] == 1)
          cout << "X";
        else
          cout << ".";
      }
      cout << "\n";
    }
  }
  return true;
}
void solve() {
  cin >> nbLig >> nbCol;
  tab.assign(nbLig, vector<int>(nbCol));
  pref = tab;
  for (int lig = (0); lig < (nbLig); ++lig)
    for (int col = (0); col < (nbCol); ++col) {
      char c;
      cin >> c;
      if (c == '.')
        tab[lig][col] = 1;
      else
        tab[lig][col] = 0;
    }
  genPref(tab, pref);
  pp = pref;
  int lo = 0, ri = (nbLig + 1) * (nbCol + 1);
  while (lo < ri) {
    int mid = (lo + ri + 1) / 2;
    if (fonctionne(mid, false))
      lo = mid;
    else
      ri = mid - 1;
  }
  cout << lo << "\n";
  fonctionne(lo, true);
}
