#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<pair<int, int> > adj[20][20];
int bfs(pair<int, int> start, pair<int, int> end) {
  int dist[n][m];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      dist[i][j] = -1;
    }
  }
  queue<pair<int, int> > qu;
  qu.push(start);
  dist[start.first][start.second] = 0;
  while (!qu.empty()) {
    pair<int, int> u = qu.front();
    qu.pop();
    if (u == end) break;
    for (auto i : adj[u.first][u.second]) {
      if (dist[i.first][i.second] == -1) {
        qu.push(i);
        dist[i.first][i.second] = dist[u.first][u.second] + 1;
      }
    }
  }
  return dist[end.first][end.second];
}
int main() {
  cin >> n >> m;
  vector<string> a(n);
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (i > 0 && a[i - 1][j] != '#') {
        adj[i][j].push_back(pair<int, int>(i - 1, j));
      }
      if (j > 0 && a[i][j - 1] != '#') {
        adj[i][j].push_back(pair<int, int>(i, j - 1));
      }
      if (i < n - 1 && a[i + 1][j] != '#') {
        adj[i][j].push_back(pair<int, int>(i + 1, j));
      }
      if (j < m - 1 && a[i][j + 1] != '#') {
        adj[i][j].push_back(pair<int, int>(i, j + 1));
      }
    }
  }
  vector<pair<int, int> > pos = {pair<int, int>(0, 0), pair<int, int>(0, m - 1),
                                 pair<int, int>(n - 1, 0),
                                 pair<int, int>(n - 1, m - 1)};
  int ans = 0;
  for (int i = 0; i < pos.size(); i++) {
    for (int j = i + 1; j < pos.size(); j++) {
      ans = max(ans, bfs(pos[i], pos[j]));
    }
  }
  cout << ans;
}
