#include <bits/stdc++.h>
using namespace std;
const int INF = 100000000;
vector<vector<char>> maze(2000, vector<char>(2000));
int N, M;
int sx, sy;
int gx, gy;
int d[2000][2000];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int bfs() {
  queue<pair<int, int>> que;
  for (int i = 0; i < N; i++)
    for (int j = 0; j < M; j++) d[i][j] = INF;
  que.push(pair<int, int>(sx, sy));
  d[sx][sy] = 0;
  while (que.size()) {
    pair<int, int> p = que.front();
    que.pop();
    if (p.first == gx && p.second == gy) break;
    for (int i = 0; i < 4; i++) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if (0 <= nx && nx < N && 0 <= ny && ny < M && maze[nx][ny] != '#' &&
          d[nx][ny] == INF) {
        que.push(pair<int, int>(nx, ny));
        d[nx][ny] = d[p.first][p.second] + 1;
      }
    }
  }
  return d[gx][gy];
}
int main() {
  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      cin >> maze.at(i).at(j);
    }
  }
  priority_queue<int> a;
  for (sx = 0; sx < N; sx++) {
    for (sy = 0; sy < M; sy++) {
      for (gx = 0; gx < N; gx++) {
        for (gy = 0; gy < M; gy++) {
          int tmp = bfs();
          if (tmp < INF) a.push(tmp);
        }
      }
    }
  }
  int res = a.top();
  cout << res << endl;
}
