#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
vector<int> dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1};
int solve(const vector<string>& maze, int h, int w) {
  vector<vector<int>> d(h, vector<int>(w, INF));
  queue<pair<int, int>> que;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      d[i][j] = INF;
      if (maze[i][j] == '.' && que.size() == 0) {
        que.push(pair<int, int>(i, j));
        d[i][j] = 0;
      }
    }
  }
  while (que.size()) {
    pair<int, int> p = que.front();
    que.pop();
    for (int i = 0; i < 4; i++) {
      int nx = p.first + dx[i];
      int ny = p.second + dy[i];
      if (0 <= nx && nx < h && 0 <= ny && ny < w && maze[nx][ny] != '#' &&
          d[nx][ny] == INF) {
        que.push(pair<int, int>(nx, ny));
        d[nx][ny] = d[p.first][p.second] + 1;
      }
    }
  }
  vector<int> v(h * h * w * w, 0);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      v[d[i][j]]++;
    }
  }
  return *max_element((v).begin(), (v).end()) -
         *min_element((v).begin(), (v).end());
}
int main() {
  int h, w;
  cin >> h >> w;
  vector<string> maze(h);
  for (int i = 0; i < h; i++) {
    cin >> maze[i];
  }
  cout << solve(maze, h, w) << endl;
  return 0;
}
