#include <bits/stdc++.h>
using namespace std;
long long a, xa, ya, b, xb, yb, h, w, xc, yc, xy[110][110] = {-1}, sx = -1,
                                              sy = -1, dx[4] = {1, 0, -1, 0},
                                              dy[4] = {0, 1, 0, -1};
string s, t;
pair<long long, long long> p;
char c[110][110];
queue<pair<long long, long long>> que;
long long bfs(long long x, long long y) {
  for (long long i = 0; i < h; i++) {
    for (long long j = 0; j < w; j++) {
      xy[i][j] = -1;
    }
  }
  que.push(pair<long long, long long>(x, y));
  xy[x][y] = 0;
  while (que.size()) {
    p = que.front();
    que.pop();
    for (long long i = 0; i < 4; i++) {
      long long nx = p.first + dx[i];
      long long ny = p.second + dy[i];
      if (0 <= nx && nx < h && 0 <= ny && ny < w && c[nx][ny] != '#' &&
          xy[nx][ny] == -1) {
        que.push(pair<long long, long long>(nx, ny));
        xy[nx][ny] = xy[p.first][p.second] + 1;
      }
    }
  }
  return p.first * w + p.second;
}
signed main() {
  cin >> h >> w;
  for (long long i = 0; i < h; i++) {
    for (long long j = 0; j < w; j++) {
      cin >> c[i][j];
      if (sx == -1 && sy == -1) {
        if (c[i][j] == '.') {
          sx = i;
          sy = j;
        }
      }
    }
  }
  a = bfs(sx, sy);
  xa = a / w;
  ya = a % w;
  b = bfs(xa, ya);
  xb = b / w;
  yb = b % w;
  xc = xa;
  yc = ya;
  while (1) {
    a = bfs(xb, yb);
    xa = a / w;
    ya = a % w;
    if (xc == xa && yc == ya) {
      cout << xy[xa][ya];
      return 0;
    }
    xc = xb;
    yc = yb;
    b = bfs(xa, ya);
    xb = b / w;
    yb = b % w;
    if (xc == xb && yc == yb) {
      cout << xy[xb][yb];
      return 0;
    }
    xc = xa;
    yc = ya;
  }
}
