#include <bits/stdc++.h>
using namespace std;
long long n, m, z, x, c, s, maxs = -1, max2 = -1, a[51][51], vis[21][21], x2,
                            y2;
pair<long long, long long> b[501];
void dfs(long long x, long long y, long long sum) {
  if (a[x][y] == -1 || x < 1 || x > n || y < 1 || y > m || vis[x][y] == 1)
    return;
  else {
    vis[x][y] = 1;
    if (maxs < sum) {
      maxs = sum;
      x2 = x;
      y2 = y;
    }
    dfs(x + 1, y, sum + 1);
    dfs(x - 1, y, sum + 1);
    dfs(x, y + 1, sum + 1);
    dfs(x, y - 1, sum + 1);
  }
}
void dfs2(long long x, long long y, long long sum) {
  if (x == x2 && y == y2) max2 = min(max2, sum);
  if (x < 1 || x > n || y < 1 || y > m || a[x][y] == -1) return;
  if (sum < vis[x][y]) {
    vis[x][y] = sum;
    dfs2(x + 1, y, sum++);
    dfs2(x - 1, y, sum++);
    dfs2(x, y + 1, sum++);
    dfs2(x, y - 1, sum++);
  }
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  ;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      char cc;
      cin >> cc;
      if (cc == '#')
        a[i][j] = -1;
      else {
        z++;
        b[z] = {i, j};
      }
    }
  }
  dfs(b[1].first, b[1].second, 0);
  for (int x1 = 1; x1 <= n; x1++)
    for (int x2 = 1; x2 <= m; x2++) vis[x1][x2] = 10000000001;
  dfs2(b[1].first, b[1].second, 0);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (vis[i][j] != 10000000001) max2 = max(max2, vis[i][j]);
  cout << max2 << "\n";
}
