#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
int main() {
  int h, w;
  cin >> h >> w;
  vector<string> s(h);
  for (int i = 0; i < h; i++) cin >> s[i];
  vector<pair<int, int>> p;
  for (int i = 0; i < h - 1; i++) {
    for (int j = 0; j < w - 1; j++) {
      if (s[i][j] != '#') {
        if (s[i + 1][j] != '#') {
          p.push_back(pair<int, int>(i + 1, j));
        }
        if (s[i][j + 1] != '#') {
          p.push_back(pair<int, int>(i, j + 1));
        }
      }
    }
  }
  int n = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '.') n++;
    }
  }
  int m = 0;
  m = p.size();
  Graph G(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    a = p[i].first;
    b = p[i].second;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  vector<int> dist(n, -1);
  queue<int> que;
  int max = 0;
  int start = 0;
  int goal = 0;
  for (int i = 0; i < n; i++) {
    dist[i] = i;
    que.push(i);
    while (!que.empty()) {
      int v = que.front();
      que.pop();
      for (int nv : G[v]) {
        if (dist[nv] != -1) continue;
        dist[nv] = dist[v] + 1;
        que.push(nv);
      }
    }
    for (int v = i; v != (i - 1 + n) % n; v = (v + 1) % n) {
      if (dist[v] > max) {
        max = dist[v];
      }
      cout << dist[v] << " ";
    }
    cout << endl;
  }
  cout << max << endl;
}
