#include <bits/stdc++.h>
using namespace std;
using ld = long double;
using ll = long long;
const ll INF = 10010010010;
using graph = vector<vector<ll>>;
const ll mod = 1000000007;
ll gcd(ll a, ll b) {
  if (a < b) swap(a, b);
  if (a % b == 0)
    return b;
  else
    return gcd(b, a % b);
}
ll H, W;
char maze[25][25];
ll dist[25][25];
const ll dx[4] = {1, 0, 0, -1};
const ll dy[4] = {0, 1, -1, 0};
ll sx, sy;
ll gx, gy;
ll bfs() {
  queue<pair<ll, ll>> que;
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++) dist[i][j] = INF;
  que.push(pair<ll, ll>(sx, sy));
  dist[sx][sy] = 0;
  while (que.size()) {
    pair<ll, ll> p = que.front();
    que.pop();
    if (p.first == gx && p.second == gy) break;
    for (int i = 0; i < 4; i++) {
      ll dh = p.first + dx[i];
      ll dw = p.second + dy[i];
      if (dh < 0 || dw < 0 || dh >= H || dw >= W) continue;
      if (dist[dh][dw] == INF && maze[dh][dw] != '#') {
        que.push(pair<ll, ll>(dh, dw));
        dist[dh][dw] = dist[p.first][p.second] + 1;
      }
    }
  }
  return dist[gx][gy];
}
int main() {
  cin >> H >> W;
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++) cin >> maze[i][j];
  ll ans = 0;
  ll x[4] = {0, 0, H - 1, H - 1};
  ll y[4] = {0, W - 1, 0, W - 1};
  for (int i = 0; i < 3; i++) {
    for (int j = i + 1; j < 4; j++) {
      sx = x[i];
      sy = y[i];
      gx = x[j];
      gy = y[j];
      ll cnt = bfs();
      ans = max(ans, cnt);
    }
  }
  cout << ans << endl;
}
