#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long n, m;
  cin >> n >> m;
  vector<vector<char>> v(n + 2, vector<char>(m + 2, '#'));
  int maxi = 0;
  int f = 4;
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= m; j++) {
      cin >> v[i][j];
    }
  }
  vector<pair<int, int>> g = {{1, 1}, {1, m}, {n, m}, {n, 1}};
  while (f--) {
    queue<pair<pair<int, int>, int>> bfs;
    vector<vector<bool>> vis;
    vis.assign(n + 2, vector<bool>(m + 2, 0));
    for (long long i = 0; i <= n + 1; i++) {
      vis[i][0] = 1;
      vis[i][m + 1] = 1;
    }
    for (long long j = 0; j <= m + 1; j++) {
      vis[0][j] = 1;
      vis[n + 1][j] = 1;
    }
    bfs.push({g[f], 0});
    vis[g[f].first][g[f].second] = 1;
    while (!bfs.empty()) {
      int i = bfs.front().first.first;
      int j = bfs.front().first.second;
      if (v[i][j + 1] == '.' && !vis[i][j + 1]) {
        bfs.push({{i, j + 1}, bfs.front().second + 1});
        vis[i][j + 1] = 1;
      }
      if (v[i][j - 1] == '.' && !vis[i][j - 1]) {
        bfs.push({{i, j - 1}, bfs.front().second + 1});
        vis[i][j - 1] = 1;
      }
      if (v[i + 1][j] == '.' && !vis[i + 1][j]) {
        bfs.push({{i + 1, j}, bfs.front().second + 1});
        vis[i + 1][j] = 1;
      }
      if (v[i - 1][j] == '.' && !vis[i - 1][j]) {
        bfs.push({{i - 1, j}, bfs.front().second + 1});
        vis[i - 1][j] = 1;
      }
      maxi = max(maxi, bfs.front().second);
      bfs.pop();
    }
  }
  cout << maxi;
}
