#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}
template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto& x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  vector<string> s(n);
  for (string& ss : s) {
    cin >> ss;
  }
  vector<vector<int>> pref(n, vector<int>(m, 0));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      pref[i][j] =
          (i >= 1 ? pref[i - 1][j] : 0) + (j >= 1 ? pref[i][j - 1] : 0) -
          (i >= 1 && j >= 1 ? pref[i - 1][j - 1] : 0) + (s[i][j] == 'X');
    }
  }
  auto getSum = [&](int l1, int r1, int l2, int r2) -> int {
    return pref[l2][r2] - (r1 >= 1 ? pref[l2][r1 - 1] : 0) -
           (l1 >= 1 ? pref[l1 - 1][r2] : 0) +
           (l1 >= 1 && r1 >= 1 ? pref[l1 - 1][r1 - 1] : 0);
  };
  auto isValid = [&](int x, int y) -> bool {
    return x >= 0 && x < n && y >= 0 && y < m;
  };
  auto possible = [&](int len) -> int {
    int K = len / 2;
    queue<pair<int, int>> Q;
    int total_xs = 0;
    vector<vector<int>> D(n, vector<int>(m, -1));
    vector<string> answer(n, string(m, '.'));
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (s[i][j] == 'X') {
          total_xs++;
          if (i - K >= 0 && i + K < n && j - K >= 0 && j + K < m) {
            int64_t s = getSum(i - K, j - K, i + K, j + K);
            if (s == len * (int64_t)len) {
              answer[i][j] = 'X';
              Q.push({i, j});
              D[i][j] = 0;
            }
          }
        }
      }
    }
    vector<pair<int, int>> dirs;
    for (int dx = -1; dx <= 1; dx++) {
      for (int dy = -1; dy <= 1; dy++) {
        if (dx == 0 && dy == 0) {
          continue;
        }
        dirs.push_back({dx, dy});
      }
    }
    while (!Q.empty()) {
      auto from = Q.front();
      Q.pop();
      for (auto dir : dirs) {
        int nx = from.first + dir.first;
        int ny = from.second + dir.second;
        pair<int, int> to = {nx, ny};
        if (isValid(nx, ny) && D[nx][ny] == -1) {
          D[nx][ny] = D[from.first][from.second] + 1;
          Q.push(to);
        }
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (s[i][j] == 'X') {
          if (D[i][j] > K) {
            return false;
          }
        } else {
          if (D[i][j] <= K) {
            return false;
          }
        }
      }
    }
    return true;
    ;
  };
  vector<int> possible_answers;
  for (int i = 1; i <= min(n, m); i += 2) {
    possible_answers.push_back(i);
  }
  int lo = 0, hi = (int)possible_answers.size() - 1, minLen = (int)1e9;
  while (lo <= hi) {
    int mid = (lo + hi) / 2;
    int ok = possible(possible_answers[mid]);
    if (ok) {
      minLen = possible_answers[mid];
      lo = mid + 1;
    } else {
      hi = mid - 1;
    }
  }
  int K = minLen / 2;
  vector<string> answer(n, string(m, '.'));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (s[i][j] == 'X') {
        if (i - K >= 0 && i + K < n && j - K >= 0 && j + K < m) {
          int64_t s = getSum(i - K, j - K, i + K, j + K);
          if (s == minLen * (int64_t)minLen) {
            answer[i][j] = 'X';
          }
        }
      }
    }
  }
  cout << K << endl;
  for (string ss : answer) {
    cout << ss << "\n";
  }
  return 0;
}
