#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
constexpr int MOD = 1000000007;
void warshall_floyd(int n, vvi& dp) {
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
      }
    }
  }
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int h, w;
  cin >> h >> w;
  vvi maze(h, vi(w, 0));
  map<int, int> m_to_v;
  int v_num = 0;
  for (int i = 0; i < (int)(h); ++i) {
    for (int j = 0; j < (int)(w); ++j) {
      char c;
      cin >> c;
      if (c == '.') {
        maze[i][j] = 1;
        m_to_v[i * h + j] = v_num++;
      }
    }
  }
  vvi g(v_num, vi(v_num, INT_MAX / 3));
  for (int i = 0; i < (int)(h); ++i) {
    for (int j = 0; j < (int)(w); ++j) {
      if (maze[i][j] == 0) {
        continue;
      }
      if (i < h - 1 && maze[i + 1][j] == 1) {
        g[m_to_v[i * h + j]][m_to_v[(i + 1) * h + j]] = 1;
      }
      if (i > 0 && maze[i - 1][j] == 1) {
        g[m_to_v[i * h + j]][m_to_v[(i - 1) * h + j]] = 1;
      }
      if (j < w - 1 && maze[i][j + 1] == 1) {
        g[m_to_v[i * h + j]][m_to_v[i * h + j + 1]] = 1;
      }
      if (j > 0 && maze[i][j - 1] == 1) {
        g[m_to_v[i * h + j]][m_to_v[i * h + j - 1]] = 1;
      }
    }
  }
  warshall_floyd(v_num, g);
  int ans = 0;
  for (int i = 0; i < (int)(v_num); ++i) {
    for (int j = 0; j < (int)(v_num); ++j) {
      ans = max(ans, g[i][j]);
      cout << g[i][j] << " ";
    }
    cout << endl;
  }
  cout << ans << endl;
  return 0;
}
