#include <bits/stdc++.h>
using pii = std::pair<int, int>;
using pll = std::pair<long long, long long>;
std::vector<std::vector<int>> map;
std::vector<std::vector<int>> dp;
void precomp(int n, int m) {
  dp.assign(n + 1, std::vector<int>(m + 1, 0));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      dp[i][j] = map[i][j] + dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1];
    }
  }
}
int sum(int t, int b, int l, int r) {
  return dp[b][r] - dp[t - 1][r] - dp[b][l - 1] + dp[t - 1][l - 1];
}
const int maxn = 1e6 + 10;
struct stree {
  int active[maxn * 4];
  int sum[maxn * 4];
  void reset() {
    std::memset(active, 0, sizeof(active));
    std::memset(sum, 0, sizeof(sum));
  }
  void update(int c, int l, int r) {
    if (active[c] > 0) {
      sum[c] = r - l;
    } else if (r - l > 1) {
      sum[c] = sum[2 * c] + sum[2 * c + 1];
    } else {
      sum[c] = 0;
    }
  }
  void set(int ql, int qr, int add, int c = 1, int l = 0, int r = maxn) {
    if (ql <= l && r <= qr) {
      active[c] += add;
      update(c, l, r);
      return;
    }
    int m = (l + r) / 2;
    if (ql < m) set(ql, qr, add, c * 2, l, m);
    if (qr > m) set(ql, qr, add, c * 2 + 1, m, r);
    update(c, l, r);
  }
  int value() { return sum[1]; }
} tree;
struct rect {
  int t, b;
  int l, r;
  int i, j;
};
struct event {
  bool is_add;
  int x;
  int t, b;
  bool operator<(const event& other) const {
    if (x == other.x) return is_add < other.is_add;
    return x < other.x;
  }
};
std::vector<event> help[maxn][2];
void fsort(std::vector<event>& v) {
  int max = 0;
  for (int i = 0; i < (int)v.size(); i++) {
    help[v[i].x][v[i].is_add].push_back(v[i]);
    max = std::max(max, v[i].x);
  }
  int idx = 0;
  for (int i = 0; i <= max; i++) {
    for (int s = 0; s <= 1; s++) {
      for (auto& ev : help[i][s]) v[idx++] = ev;
      help[i][s].clear();
    }
  }
}
int sum_area(const std::vector<rect>& rs) {
  std::vector<event> evs(rs.size() * 2);
  int max = 0;
  for (int i = 0; i < (int)rs.size(); i++) {
    evs[2 * i] = {1, rs[i].l, rs[i].t, rs[i].b};
    evs[2 * i + 1] = {0, rs[i].r, rs[i].t, rs[i].b};
    max = std::max(rs[i].b, max);
  }
  if (evs.empty()) return 0;
  fsort(evs);
  int last_x = evs[0].x;
  int area = 0;
  for (auto& ev : evs) {
    area += tree.value() * (ev.x - last_x);
    tree.set(ev.t, ev.b, ev.is_add ? 1 : -1, 1, 0, max);
    last_x = ev.x;
  }
  return area;
}
std::vector<rect> getrects(int r, int n, int m) {
  std::vector<rect> res;
  for (int i = r + 1; i <= n - r; i++) {
    for (int j = r + 1; j <= m - r; j++) {
      if (sum(i - r, i + r, j - r, j + r) == (2 * r + 1) * (2 * r + 1)) {
        rect rr;
        rr.t = i - r;
        rr.b = i + r + 1;
        rr.l = j - r;
        rr.r = j + r + 1;
        rr.i = i;
        rr.j = j;
        res.push_back(rr);
      }
    }
  }
  return res;
}
rect gr(int t, int b, int l, int r) {
  rect rs;
  rs.t = t;
  rs.b = b;
  rs.l = l;
  rs.r = r;
  return rs;
}
int main() {
  std::ios::sync_with_stdio(false);
  int n, m;
  std::cin >> n >> m;
  map.assign(n + 1, std::vector<int>(m + 1, 0));
  for (int i = 1; i <= n; i++) {
    std::string in;
    std::cin >> in;
    for (int j = 1; j <= m; j++) {
      map[i][j] = in[j - 1] == 'X';
    }
  }
  precomp(n, m);
  int l = 0;
  int r = std::max(n, m) + 1;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    auto rs = getrects(mid, n, m);
    if (sum_area(rs) == dp[n][m])
      l = mid;
    else
      r = mid;
  }
  std::cout << l << "\n";
  auto rs = getrects(l, n, m);
  map.assign(n + 1, std::vector<int>(m + 1, 0));
  for (auto& r : rs) {
    map[r.i][r.j] = 1;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      std::cout << (map[i][j] ? 'X' : '.');
    }
    std::cout << "\n";
  }
  return 0;
}
