#include <bits/stdc++.h>
using namespace std;
using int32 = int_fast32_t;
using int64 = int_fast64_t;
const int32 INF = 1e9;
const int32 MOD = 1e9 + 7;
const int64 LLINF = 1e18;
int32 di[4] = {0, 1, 0, -1};
int32 dj[4] = {1, 0, -1, 0};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int32 h, w;
  cin >> h >> w;
  vector<string> s(h);
  for (int64 i = 0; i < (h); ++i) cin >> s[i];
  int32 si, sj;
  for (int64 i = 0; i < (h); ++i)
    for (int64 j = 0; j < (w); ++j) {
      if (s[i][j] == '.') {
        si = i;
        sj = j;
        break;
      }
    }
  vector<vector<int32>> dist;
  queue<pair<int32, int32>> que;
  for (int64 _ = 0; _ < (2); ++_) {
    que.emplace(si, sj);
    dist = vector<vector<int32>>(h, vector<int32>(w, INF));
    dist[si][sj] = 0;
    while (!que.empty()) {
      int32 ci, cj;
      tie(ci, cj) = que.front();
      que.pop();
      for (int64 i = 0; i < (4); ++i) {
        int32 ni = ci + di[i];
        int32 nj = cj + dj[i];
        if (ni < 0 || h <= ni) continue;
        if (nj < 0 || w <= nj) continue;
        if (s[ni][nj] == '#') continue;
        if (dist[ni][nj] > dist[ci][cj] + 1) {
          dist[ni][nj] = dist[ci][cj] + 1;
          que.emplace(ni, nj);
        }
      }
    }
    int32 mx = 0;
    for (int64 i = 0; i < (h); ++i)
      for (int64 j = 0; j < (w); ++j) {
        if (dist[i][j] != INF && dist[i][j] > mx) {
          mx = dist[i][j];
          si = i;
          sj = j;
        }
      }
  }
  cout << (dist[si][sj]) << "\n";
  return 0;
}
