#include <bits/stdc++.h>
using namespace std;
int main() {
  int h, w;
  cin >> h >> w;
  vector<vector<char>> s(h + 2, vector<char>(w + 2));
  int i, j;
  for (i = 0; i <= h + 1; i++) {
    for (j = 0; j <= w + 1; j++) {
      if (i == 0 || j == 0 || i == h + 1 || j == w + 1)
        s.at(i).at(j) = '#';
      else
        cin >> s.at(i).at(j);
    }
  }
  int si = -1;
  int sj = -1;
  for (i = 1; i <= h; i++) {
    for (j = 1; j <= w; j++) {
      if (s.at(i).at(j) == '.') {
        si = i;
        sj = j;
        break;
      }
    }
    if (si != -1) break;
  }
  vector<vector<int>> tm1(h + 2, vector<int>(w + 2, 10000));
  queue<int> q;
  queue<int> q2;
  q.push((si - 1) * w + sj);
  tm1.at(si).at(sj) = 0;
  int dis = 1;
  while (!q.empty()) {
    while (!q.empty()) {
      int now = q.front();
      q.pop();
      int nowj = now % w;
      int nowi;
      if (nowj == 0) {
        nowj = w;
        nowi = now / w;
      } else {
        nowi = now / w + 1;
      }
      if (s.at(nowi - 1).at(nowj) == '.' &&
          tm1.at(nowi - 1).at(nowj) == 10000 && nowi > 1) {
        tm1.at(nowi - 1).at(nowj) = dis;
        q2.push((nowi - 2) * w + nowj);
      }
      if (s.at(nowi + 1).at(nowj) == '.' &&
          tm1.at(nowi + 1).at(nowj) == 10000 && nowi < h) {
        tm1.at(nowi + 1).at(nowj) = dis;
        q2.push((nowi)*w + nowj);
      }
      if (s.at(nowi).at(nowj - 1) == '.' &&
          tm1.at(nowi).at(nowj - 1) == 10000 && nowj > 1) {
        tm1.at(nowi).at(nowj - 1) = dis;
        q2.push((nowi - 1) * w + nowj - 1);
      }
      if (s.at(nowi).at(nowj + 1) == '.' &&
          tm1.at(nowi).at(nowj + 1) == 10000 && nowj < w) {
        tm1.at(nowi).at(nowj + 1) = dis;
        q2.push((nowi - 1) * w + nowj + 1);
      }
    }
    dis++;
    while (!q2.empty()) {
      q.push(q2.front());
      q2.pop();
    }
  }
  int dmax = -1;
  for (i = 1; i <= h; i++) {
    for (j = 1; j <= w; j++) {
      if (tm1.at(i).at(j) > dmax && tm1.at(i).at(j) != 10000) {
        dmax = tm1.at(i).at(j);
        si = i;
        sj = j;
      }
    }
  }
  vector<vector<int>> tm2(h + 2, vector<int>(w + 2, 10000));
  queue<int> q21;
  queue<int> q22;
  q21.push((si - 1) * w + sj);
  tm2.at(si).at(sj) = 0;
  dis = 1;
  while (!q21.empty()) {
    while (!q21.empty()) {
      int now = q21.front();
      q21.pop();
      int nowj = now % w;
      int nowi;
      if (nowj == 0) {
        nowj = w;
        nowi = now / w;
      } else {
        nowi = now / w + 1;
      }
      if (s.at(nowi - 1).at(nowj) == '.' &&
          tm2.at(nowi - 1).at(nowj) == 10000) {
        tm2.at(nowi - 1).at(nowj) = dis;
        q22.push((nowi - 2) * w + nowj);
      }
      if (s.at(nowi + 1).at(nowj) == '.' &&
          tm2.at(nowi + 1).at(nowj) == 10000) {
        tm2.at(nowi + 1).at(nowj) = dis;
        q22.push((nowi)*w + nowj);
      }
      if (s.at(nowi).at(nowj - 1) == '.' &&
          tm2.at(nowi).at(nowj - 1) == 10000) {
        tm2.at(nowi).at(nowj - 1) = dis;
        q22.push((nowi - 1) * w + nowj - 1);
      }
      if (s.at(nowi).at(nowj + 1) == '.' &&
          tm2.at(nowi).at(nowj + 1) == 10000) {
        tm2.at(nowi).at(nowj + 1) = dis;
        q22.push((nowi - 1) * w + nowj + 1);
      }
    }
    dis++;
    while (!q22.empty()) {
      q21.push(q22.front());
      q22.pop();
    }
  }
  int ans = -1;
  for (i = 1; i <= h; i++) {
    for (j = 1; j <= w; j++) {
      if (tm2.at(i).at(j) > ans && tm2.at(i).at(j) != 10000)
        ans = tm2.at(i).at(j);
    }
  }
  cout << ans << endl;
}
