#include <bits/stdc++.h>
using namespace std;
int inf;
const double eps = 1e-8;
const double pi = acos(-1.0);
template <class T>
int chkmin(T &a, T b) {
  return a > b ? a = b, 1 : 0;
}
template <class T>
int chkmax(T &a, T b) {
  return a < b ? a = b, 1 : 0;
}
template <class T>
T sqr(T a) {
  return a * a;
}
template <class T>
T mmin(T a, T b) {
  return a < b ? a : b;
}
template <class T>
T mmax(T a, T b) {
  return a > b ? a : b;
}
template <class T>
T aabs(T a) {
  return a < 0 ? -a : a;
}
template <class T>
int dcmp(T a, T b) {
  return a > b;
}
template <int *a>
int cmp_a(int first, int second) {
  return a[first] < a[second];
}
struct __INIT__ {
  __INIT__() { memset(&inf, 0x3f, sizeof(inf)); }
} __INIT___;
namespace io {
const int SIZE = (1 << 21) + 1;
char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c,
                                       qu[55];
int f, qr;
inline void flush() {
  fwrite(obuf, 1, oS - obuf, stdout);
  oS = obuf;
}
inline void putc(char first) {
  *oS++ = first;
  if (oS == oT) flush();
}
inline bool read(signed &first) {
  for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                              (iS == iT ? EOF : *iS++))
                            : *iS++);
       c < '0' || c > '9';
       c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    if (c == '-')
      f = -1;
    else if (c == EOF)
      return 0;
  for (first = 0; c <= '9' && c >= '0';
       c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    first = first * 10 + (c & 15);
  first *= f;
  return 1;
}
inline bool read(long long &first) {
  for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                              (iS == iT ? EOF : *iS++))
                            : *iS++);
       c < '0' || c > '9';
       c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    if (c == '-')
      f = -1;
    else if (c == EOF)
      return 0;
  for (first = 0; c <= '9' && c >= '0';
       c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    first = first * 10 + (c & 15);
  first *= f;
  return 1;
}
inline bool read(char &first) {
  while ((first = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                               (iS == iT ? EOF : *iS++))
                            : *iS++)) == ' ' ||
         first == '\n' || first == '\r')
    if (first == EOF) return 0;
  return 1;
}
inline bool read(char *first) {
  while ((*first = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                                (iS == iT ? EOF : *iS++))
                             : *iS++)) == '\n' ||
         *first == ' ' || *first == '\r')
    if (*first == EOF) return 0;
  while (!(*first == '\n' || *first == ' ' || *first == '\r'))
    *(++first) = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                              (iS == iT ? EOF : *iS++))
                           : *iS++);
  *first = 0;
  return 1;
}
template <typename A, typename... B>
inline bool read(A &first, B &...second) {
  return read(first) && read(second...);
}
inline bool write(signed first) {
  if (!first) putc('0');
  if (first < 0) putc('-'), first = -first;
  while (first) qu[++qr] = first % 10 + '0', first /= 10;
  while (qr) putc(qu[qr--]);
  return 0;
}
inline bool write(long long first) {
  if (!first) putc('0');
  if (first < 0) putc('-'), first = -first;
  while (first) qu[++qr] = first % 10 + '0', first /= 10;
  while (qr) putc(qu[qr--]);
  return 0;
}
inline bool write(char first) {
  putc(first);
  return 0;
}
inline bool write(const char *first) {
  while (*first) {
    putc(*first);
    ++first;
  }
  return 0;
}
inline bool write(char *first) {
  while (*first) {
    putc(*first);
    ++first;
  }
  return 0;
}
template <typename A, typename... B>
inline bool write(A first, B... second) {
  return write(first) || write(second...);
}
struct Flusher_ {
  ~Flusher_() { flush(); }
} io_flusher_;
}  // namespace io
using io ::putc;
using io ::read;
using io ::write;
vector<int> a[1000005];
vector<int> b[1000005];
vector<int> c[1000005];
signed main() {
  int n, m;
  read(n, m);
  for (int i = 0; i <= n + 1; ++i) a[i].resize(m + 2);
  for (int i = 0; i <= n + 1; ++i) b[i].resize(m + 2);
  for (int i = 0; i <= n + 1; ++i) c[i].resize(m + 2);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) {
      char c;
      read(c);
      a[i][j] = (c == 'X');
      b[i][j] = 1 - a[i][j] + b[i][j - 1] + b[i - 1][j] - b[i - 1][j - 1];
    }
  int l = 0, r = mmin(n, m), mid;
  while (l <= r) {
    mid = (l + r) >> 1;
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= m; ++j) c[i][j] = 0;
    for (int i = 1; i + mid * 2 <= n; ++i) {
      for (int j = 1; j + mid * 2 <= m; ++j) {
        if (!(b[i + mid * 2][j + mid * 2] - b[i - 1][j + mid * 2] -
              b[i + mid * 2][j - 1] + b[i - 1][j - 1])) {
          ++c[i][j];
          --c[i + mid * 2 + 1][j];
          --c[i][j + mid * 2 + 1];
          ++c[i + mid * 2 + 1][j + mid * 2 + 1];
        }
      }
    }
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= m; ++j)
        c[i][j] += c[i - 1][j] + c[i][j - 1] - c[i - 1][j - 1];
    bool ok = 1;
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= m; ++j)
        if (a[i][j] && !(c[i][j])) ok = 0;
    if (ok)
      l = mid + 1;
    else
      r = mid - 1;
  }
  write(l - 1, '\n');
  mid = l - 1;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) c[i][j] = 0;
  for (int i = 1; i + mid * 2 <= n; ++i) {
    for (int j = 1; j + mid * 2 <= m; ++j) {
      if (!(b[i + mid * 2][j + mid * 2] - b[i - 1][j + mid * 2] -
            b[i + mid * 2][j - 1] + b[i - 1][j - 1])) {
        c[i + mid][j + mid] = 1;
      }
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j)
      if (c[i][j])
        write('X');
      else
        write('.');
    write('\n');
  }
  return 0;
}
