#include <bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << H;
  debug_out(T...);
}
template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &item) {
  out << '(' << item.first << ", " << item.second << ')';
  return out;
}
template <typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
  for (const auto &item : v) out << item << ' ';
  return out;
}
ll n, m;
vector<pair<ll, ll> > G;
vector<vector<ll> > Mars, SP;
vector<string> V, Ans;
bool square(ll x1, ll y1, ll x2, ll y2) {
  ll q = SP[x1][y1] - SP[x1][y2 - 1] - SP[x2 - 1][y1] + SP[x2 - 1][y2 - 1];
  return q == (x1 - x2 + 1) * (y1 - y2 + 1);
}
void gather(ll d) {
  G.clear();
  for (int x = 1; x <= n; x++) {
    for (int y = 1; y <= m; y++) {
      if (x - d < 1 || y - d < 1 || x + d > n || y + d > m) continue;
      if (square(x + d, y + d, x - d, y - d)) {
        G.push_back({x, y});
      }
    }
  }
}
bool check(int mijl) {
  gather(mijl);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      Mars[i][j] = 0;
    }
  }
  for (auto i : G) {
    int x = i.first;
    int y = i.second;
    Mars[x - mijl][y - mijl]++;
    Mars[x - mijl][y + mijl + 1]--;
    Mars[x + mijl + 1][y - mijl]--;
    Mars[x + mijl + 1][y + mijl + 1]++;
  }
  for (int j = 1; j <= m; j++) {
    for (int i = 1; i <= n; i++) {
      Mars[i][j] += Mars[i - 1][j];
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      Mars[i][j] += Mars[i][j - 1];
    }
  }
  bool ok = true;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (V[i][j] == 'X' && Mars[i][j] == 0) {
        ok = false;
      }
      if (V[i][j] == '.') assert(Mars[i][j] == 0);
    }
  }
  return ok;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> m;
  V.push_back("");
  for (int i = 1; i <= n; i++) {
    string s;
    cin >> s;
    V.push_back("");
    V[i] = "." + s + ".";
  }
  for (int i = 0; i <= n + 1; i++) {
    Mars.push_back(vector<ll>());
    SP.push_back(vector<ll>());
    Ans.push_back("");
    for (int j = 0; j <= m + 1; j++)
      Mars[i].push_back(0), SP[i].push_back(0), Ans[i] += '.';
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      SP[i][j] =
          SP[i - 1][j] + SP[i][j - 1] - SP[i - 1][j - 1] + (V[i][j] == 'X');
    }
  }
  ll st = 0, dr = max(n, m);
  while (st <= dr) {
    ll mijl = (st + dr) / 2;
    bool ok = check(mijl);
    if (ok) {
      st = mijl + 1;
    } else {
      dr = mijl - 1;
    }
  }
  if (check(st)) {
    cout << st << '\n';
    for (auto i : G) Ans[i.first][i.second] = 'X';
    for (int i = 1; i <= n; i++, cout << '\n')
      for (int j = 1; j <= m; j++) cout << Ans[i][j];
  } else if (check(dr)) {
    cout << dr << '\n';
    for (auto i : G) Ans[i.first][i.second] = 'X';
    for (int i = 1; i <= n; i++, cout << '\n')
      for (int j = 1; j <= m; j++) cout << Ans[i][j];
  } else {
    assert(false);
  }
  return 0;
}
