#include <bits/stdc++.h>
using namespace std;
int main() {
  int mod = 1000000007;
  int h, w, ans = 1;
  scanf("%d %d", &h, &w);
  int d[h][w];
  for (int i = 0; i < h; ++i)
    for (int j = 0; j < w; ++j) d[i][j] = 0;
  for (int i = 0; i < h; ++i)
    for (int j = 0; j < w; ++j) {
      char s[w];
      scanf("%s", s);
      for (int k = 0; k < w; ++k)
        if (s[k] == '#') d[i][k] = 1;
    }
  int p[h * w][h * w];
  for (int i = 0; i < h * w; ++i)
    for (int j = 0; j < w * h; ++j) p[i][j] = 1000;
  for (int i = 0; i < h * w; ++i)
    for (int j = 0; j < h * w; ++j)
      for (int k = 0; k < h * w; ++k) {
        int ih = i / w, iw = i % w, jh = j / w, jw = j % w, kh = k / w,
            kw = k % w;
        if ('.' == d[ih][iw] == d[jh][jw] && ih == jh && abs(iw - jw) == 1)
          p[i][j] = 1;
        p[j][i] = 1;
        if ('.' == d[ih][iw] == d[jh][jw] && iw == jw && abs(ih - jh) == 1)
          p[i][j] = 1;
        p[j][i] = 1;
        if ('.' == d[ih][iw] == d[kh][kw] && ih == kh && abs(iw - kw) == 1)
          p[i][k] = 1;
        p[k][i] = 1;
        if ('.' == d[ih][iw] == d[kh][kw] && iw == kw && abs(ih - kh) == 1)
          p[i][k] = 1;
        p[k][i] = 1;
        if ('.' == d[jh][jw] == d[kh][kw] && jh == kh && abs(jw - kw) == 1)
          p[j][k] = 1;
        p[k][j] = 1;
        if ('.' == d[jh][jw] == d[kh][kw] && jw == kw && abs(jh - kh) == 1)
          p[j][k] = 1;
        p[k][j] = 1;
        p[j][k] = min(p[j][k], p[j][i] + p[i][k]);
      }
  for (int i = 0; i < h * w; ++i)
    for (int j = 0; j < w * h; ++j)
      if (p[i][h] != 1000) ans = max(ans, p[i][h]);
  printf("%d\n", ans);
  return 0;
}
