#include <bits/stdc++.h>
using namespace std;
void map(vector<vector<long long>> s, vector<vector<long long>>& l, long long d,
         long long r, long long n) {
  n++;
  l[d][r] = n;
  for (long long i = -1; i < 2; i++) {
    for (long long j = -1; j < 2; j++) {
      if (i != 0 && j != 0) continue;
      long long nd = d + i, nr = r + j;
      if (s[nd][nr] != 0 || l[nd][nr] <= n) continue;
      map(s, l, nd, nr, n);
    }
  }
}
int main() {
  long long h;
  cin >> h;
  long long w;
  cin >> w;
  h += 2;
  w += 2;
  vector<vector<long long>> s(h, vector<long long>(w, 1));
  long long d, r;
  for (long long i = 1; i < h - 1; i++) {
    for (long long j = 1; j < w - 1; j++) {
      char c;
      cin >> c;
      if (c == '#') {
        s[i][j] = 1;
      } else {
        s[i][j] = 0;
        d = i;
        r = j;
      }
    }
  }
  vector<vector<long long>> l(h, vector<long long>(w, 1e18));
  map(s, l, d, r, 0);
  for (long long i = 0; i < h; i++) {
    for (long long j = 0; j < w; j++) {
      if (l[i][j] != 1e18 && l[d][r] < l[i][j]) {
        d = i;
        r = j;
      }
    }
  }
  l = vector<vector<long long>>(h, vector<long long>(w, 1e18));
  map(s, l, d, r, 0);
  for (long long i = 0; i < h; i++) {
    for (long long j = 0; j < w; j++) {
      if (l[i][j] != 1e18 && l[d][r] < l[i][j]) {
        d = i;
        r = j;
      }
    }
  }
  cout << l[d][r] - 1 << endl;
  return 0;
}
