#include <bits/stdc++.h>
using namespace std;
string s[22];
int vis[22][22];
int n, m, ans;
int dfs(int x, int y, int x2, int y2, int d) {
  if (x == x2 && y == y2) {
    ans = min(ans, d);
    return 0;
  }
  if (x < 0 || x > n - 1 || y < 0 || y > m - 1 || s[x][y] == '#' ||
      vis[x][y] == 1)
    return 0;
  vis[x][y] = 1;
  int temp = 0;
  dfs(x, y + 1, x2, y2, d + 1);
  dfs(x, y - 1, x2, y2, d + 1);
  dfs(x + 1, y, x2, y2, d + 1);
  dfs(x - 1, y, x2, y2, d + 1);
  return 0;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int k, c = 0;
  cin >> n >> m;
  int dp[n + 1][m + 1];
  for (int i = 0; i <= n - 1; i++) cin >> s[i];
  for (int x1 = 1; x1 <= n; x1++) {
    for (int y1 = 1; y1 <= m; y1++) {
      for (int x2 = 1; x2 <= n; x2++) {
        for (int y2 = 1; y2 <= m; y2++) {
          for (int i = 0; i <= n; i++)
            for (int j = 0; j <= m; j++) dp[i][j] = 9999999;
          dp[x1][y1] = 0;
          for (int i = n; i >= 1; i--) {
            for (int j = 1; j <= m; j++) {
              if (s[i - 1][j - 1] == '#') continue;
              if (i > 1) dp[i][j] = min(dp[i][j], dp[i - 1][j] + 1);
              if (i < n) dp[i][j] = min(dp[i][j], dp[i + 1][j] + 1);
              if (j > 1) dp[i][j] = min(dp[i][j], dp[i][j - 1] + 1);
              if (j < m) dp[i][j] = min(dp[i][j], dp[i][j + 1] + 1);
            }
          }
          for (int i = 1; i <= n; i++) {
            for (int j = m; j >= 1; j--) {
              if (s[i - 1][j - 1] == '#') continue;
              if (i > 1) dp[i][j] = min(dp[i][j], dp[i - 1][j] + 1);
              if (i < n) dp[i][j] = min(dp[i][j], dp[i + 1][j] + 1);
              if (j > 1) dp[i][j] = min(dp[i][j], dp[i][j - 1] + 1);
              if (j < m) dp[i][j] = min(dp[i][j], dp[i][j + 1] + 1);
            }
          }
          for (int i = n; i >= 1; i--) {
            for (int j = m; j >= 1; j--) {
              if (s[i - 1][j - 1] == '#') continue;
              if (i > 1) dp[i][j] = min(dp[i][j], dp[i - 1][j] + 1);
              if (i < n) dp[i][j] = min(dp[i][j], dp[i + 1][j] + 1);
              if (j > 1) dp[i][j] = min(dp[i][j], dp[i][j - 1] + 1);
              if (j < m) dp[i][j] = min(dp[i][j], dp[i][j + 1] + 1);
            }
          }
          for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
              if (s[i - 1][j - 1] == '#') continue;
              if (i > 1) dp[i][j] = min(dp[i][j], dp[i - 1][j] + 1);
              if (i < n) dp[i][j] = min(dp[i][j], dp[i + 1][j] + 1);
              if (j > 1) dp[i][j] = min(dp[i][j], dp[i][j - 1] + 1);
              if (j < m) dp[i][j] = min(dp[i][j], dp[i][j + 1] + 1);
            }
          }
          for (int j = m; j >= 1; j--) {
            for (int i = 1; i <= n; i++) {
              if (s[i - 1][j - 1] == '#') continue;
              if (i > 1) dp[i][j] = min(dp[i][j], dp[i - 1][j] + 1);
              if (i < n) dp[i][j] = min(dp[i][j], dp[i + 1][j] + 1);
              if (j > 1) dp[i][j] = min(dp[i][j], dp[i][j - 1] + 1);
              if (j < m) dp[i][j] = min(dp[i][j], dp[i][j + 1] + 1);
            }
          }
          for (int j = 1; j <= m; j++) {
            for (int i = n; i >= 1; i--) {
              if (s[i - 1][j - 1] == '#') continue;
              if (i > 1) dp[i][j] = min(dp[i][j], dp[i - 1][j] + 1);
              if (i < n) dp[i][j] = min(dp[i][j], dp[i + 1][j] + 1);
              if (j > 1) dp[i][j] = min(dp[i][j], dp[i][j - 1] + 1);
              if (j < m) dp[i][j] = min(dp[i][j], dp[i][j + 1] + 1);
            }
          }
          for (int j = m; j >= 1; j--) {
            for (int i = n; i >= 1; i--) {
              if (s[i - 1][j - 1] == '#') continue;
              if (i > 1) dp[i][j] = min(dp[i][j], dp[i - 1][j] + 1);
              if (i < n) dp[i][j] = min(dp[i][j], dp[i + 1][j] + 1);
              if (j > 1) dp[i][j] = min(dp[i][j], dp[i][j - 1] + 1);
              if (j < m) dp[i][j] = min(dp[i][j], dp[i][j + 1] + 1);
            }
          }
          for (int j = 1; j <= m; j++) {
            for (int i = 1; i <= n; i++) {
              if (s[i - 1][j - 1] == '#') continue;
              if (i > 1) dp[i][j] = min(dp[i][j], dp[i - 1][j] + 1);
              if (i < n) dp[i][j] = min(dp[i][j], dp[i + 1][j] + 1);
              if (j > 1) dp[i][j] = min(dp[i][j], dp[i][j - 1] + 1);
              if (j < m) dp[i][j] = min(dp[i][j], dp[i][j + 1] + 1);
            }
          }
          if (dp[x2][y2] != 9999999) ans = max(ans, dp[x2][y2]);
        }
      }
    }
  }
  cout << ans;
}
