#include <bits/stdc++.h>
using namespace std;
char mmap[20][50];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int bfs(pair<int, int> source, pair<int, int> dest) {
  int visited[20][20] = {};
  queue<pair<pair<int, int>, int> > q;
  q.push(make_pair(source, 0));
  visited[source.first][source.second] = 1;
  while (!q.empty()) {
    pair<pair<int, int>, int> cur = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      int nexty = cur.first.first + dy[i];
      int nextx = cur.first.second + dx[i];
      if (nexty == dest.first && nextx == dest.second) return cur.second + 1;
      if (mmap[nexty][nextx] == '.' && visited[nexty][nextx] == 0) {
        q.push(make_pair(make_pair(nexty, nextx), cur.second + 1));
        visited[nexty][nextx] = 1;
      }
    }
  }
  return -1;
}
int main() {
  int h, w;
  scanf("%d %d", &h, &w);
  for (int i = 0; i < h; i++) scanf("%s", mmap[i]);
  int res = -1;
  for (int i = 0; i < h / 2; i++)
    for (int j = 0; j < w; j++)
      for (int k = h / 2; k < h; k++)
        for (int l = 0; l < w; l++)
          if (mmap[i][j] == '.' && mmap[k][l] == '.')
            res = max(res, bfs(make_pair(i, j), make_pair(k, l)));
  printf("%d\n", res);
  return 0;
}
