#include <bits/stdc++.h>
using namespace std;
int bfs(int n, vector<vector<int>> M, int pcol, int N);
vector<int> colors, check;
int main() {
  int H, W;
  cin >> H >> W;
  vector<char> cond(H * W + 1);
  for (int i = 1; i <= H * W; i++) {
    cin >> cond[i];
  }
  vector<vector<int>> dist(H * W + 1);
  for (int i = 1; i <= H * W; i++) {
    dist[i].assign(H * W + 1, 1000000);
  }
  for (int i = 1; i <= H * W; i++) {
    if (cond[i] == '.') {
      dist[i][i] = 0;
      if (i % W != 0 && cond[i + 1] == '.') {
        dist[i][i + 1] = 1;
        dist[i + 1][i] = 1;
      }
      if (i + W <= H * W && cond[i + W] == '.') {
        dist[i][i + W] = 1;
        dist[i + W][i] = 1;
      }
    }
  }
  for (int k = 1; k <= H * W; k++) {
    for (int i = 1; i <= H * W; i++) {
      for (int j = 1; j <= H * W; j++) {
        dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
      }
    }
  }
  for (int i = 1; i <= H * W; i++) {
    for (int j = 1; j <= H * W; j++) {
      cout << dist[i][j] << " ";
    }
    cout << endl;
  }
  int ans = 0;
  for (int i = 1; i <= H * W; i++) {
    for (int j = 1; j <= H * W; j++) {
      if (cond[i] == '.' && cond[j] == '.') {
        ans = max(ans, dist[i][j]);
      }
    }
  }
  cout << ans << endl;
  return 0;
}
