#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int h, w;
  cin >> h >> w;
  vector<vector<char>> s(h, vector<char>(w));
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) cin >> s[i][j];
  auto bfs = [&](int sx, int sy) -> tuple<int, int, int> {
    queue<tuple<int, int, int>> q;
    q.push({sx, sy, 0});
    vector<vector<bool>> visit(h, vector<bool>(w, false));
    vector<pair<int, int>> dirs = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}};
    int retx, rety, retd;
    while (!q.empty()) {
      auto [x, y, d] = q.front();
      q.pop();
      if (visit[y][x]) continue;
      visit[y][x] = true;
      retx = x;
      rety = y;
      retd = d;
      for (auto [dx, dy] : dirs) {
        int nx = dx + x;
        int ny = dy + y;
        if (nx < 0 || nx >= w || ny < 0 || ny >= h) continue;
        if (s[ny][nx] == '#') continue;
        q.push({nx, ny, d + 1});
      }
    }
    return {retx, rety, retd};
  };
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      if (s[y][x] == '.') {
        auto [sx, sy, d] = bfs(x, y);
        auto [gx, gy, ans] = bfs(sx, sy);
        cout << ans << endl;
        return 0;
      }
    }
  }
  exit(1);
  return 0;
}
