#include <bits/stdc++.h>
using namespace std;
template <class T>
bool smin(T &a, T b) {
  return a > b ? (a = b, true) : false;
}
template <class T>
bool smax(T &a, T b) {
  return a < b ? (a = b, true) : false;
}
vector<vector<int> > a, p, b;
int getf(int pa, int push_back, int c, int d) {
  int res = p[c][d];
  if (push_back > 0) {
    res -= p[c][push_back - 1];
  }
  if (pa > 0 && push_back > 0) {
    res += p[pa - 1][push_back - 1];
  }
  if (pa > 0) {
    res -= p[pa - 1][d];
  }
  return res;
}
int gets(int pa, int push_back, int c, int d) {
  int res = b[c][d];
  if (push_back > 0) {
    res -= b[c][push_back - 1];
  }
  if (pa > 0 && push_back > 0) {
    res += b[pa - 1][push_back - 1];
  }
  if (pa > 0) {
    res -= b[pa - 1][d];
  }
  return res;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout.precision(10);
  cout << fixed;
  int n, m;
  cin >> n >> m;
  a.resize(n, vector<int>(m));
  for (int i = 0; i < (int)(n); ++i) {
    for (int j = 0; j < (int)(m); ++j) {
      char c;
      cin >> c;
      a[i][j] = (c == 'X' ? 1 : 0);
    }
  }
  p = a;
  b = a;
  for (int i = 0; i < (int)(n); ++i) {
    for (int j = 0; j < (int)(m); ++j) {
      if (i > 0) {
        p[i][j] += p[i - 1][j];
      }
      if (j > 0) {
        p[i][j] += p[i][j - 1];
      }
      if (i > 0 && j > 0) {
        p[i][j] -= p[i - 1][j - 1];
      }
    }
  }
  int l = 0;
  int r = n * m + 1;
  while (l + 1 < r) {
    int mid = (l + r) / 2;
    for (int i = 0; i < (int)(n); ++i) {
      for (int j = 0; j < (int)(m); ++j) {
        b[i][j] = 0;
      }
    }
    for (int i = mid; i + mid < n; ++i) {
      for (int j = mid; j + mid < m; ++j) {
        int pa, push_back, c, d;
        pa = i - mid;
        push_back = j - mid;
        c = i + mid;
        d = j + mid;
        int need = (c - pa + 1) * (d - push_back + 1);
        if (need == getf(pa, push_back, c, d)) {
          b[i][j] = 1;
        }
      }
    }
    for (int i = 0; i < (int)(n); ++i) {
      for (int j = 0; j < (int)(m); ++j) {
        if (i > 0) {
          b[i][j] += b[i - 1][j];
        }
        if (j > 0) {
          b[i][j] += b[i][j - 1];
        }
        if (i > 0 && j > 0) {
          b[i][j] -= b[i - 1][j - 1];
        }
      }
    }
    bool ok = true;
    for (int i = 0; i < (int)(n); ++i) {
      for (int j = 0; j < (int)(m); ++j) {
        int c = min(n - 1, i + mid);
        int d = min(m - 1, j + mid);
        int pa = max(0, i - mid);
        int push_back = max(0, j - mid);
        bool t = gets(pa, push_back, c, d) > 0;
        ok &= (t == a[i][j]);
      }
    }
    if (ok) {
      l = mid;
    } else {
      r = mid;
    }
  }
  cout << l << '\n';
  int mid = l;
  for (int i = 0; i < (int)(n); ++i) {
    for (int j = 0; j < (int)(m); ++j) {
      b[i][j] = 0;
    }
  }
  for (int i = mid; i + mid < n; ++i) {
    for (int j = mid; j + mid < m; ++j) {
      int pa, push_back, c, d;
      pa = i - mid;
      push_back = j - mid;
      c = i + mid;
      d = j + mid;
      int need = (c - pa + 1) * (d - push_back + 1);
      if (need == getf(pa, push_back, c, d)) b[i][j] = 1;
    }
  }
  for (int i = 0; i < (int)(n); ++i) {
    for (int j = 0; j < (int)(m); ++j) {
      if (b[i][j]) {
        cout << "X";
      } else {
        cout << ".";
      }
    }
    cout << '\n';
  }
  return 0;
}
