#include <bits/stdc++.h>
using namespace std;
struct pos {
  int tateh, yokoh, cost;
};
int main() {
  int tate, yoko;
  cin >> tate >> yoko;
  vector<string> mas(tate);
  for (int i = 0; i <= tate - 1; i++) {
    cin >> mas[i];
  }
  int kotae = 100000;
  vector<vector<int>> dis(tate, vector<int>(yoko));
  queue<pos> que;
  pos start;
  for (int k = 0; k <= 3; k++) {
    if (k == 0) {
      start = {0, 0, 0};
    } else if (k == 1) {
      start = {0, yoko, 0};
    } else if (k == 2) {
      start = {tate, 0, 0};
    } else {
      start = {tate, yoko, 0};
    }
    que.push(start);
    vector<int> tatemove{0, 0, 1, -1};
    vector<int> yokomove{1, -1, 0, 0};
    while (1) {
      pos now = que.front();
      que.pop();
      for (int i = 0; i <= 3; i++) {
        pos next;
        next.tateh = now.tateh + tatemove[i];
        next.yokoh = now.yokoh + yokomove[i];
        next.cost += 1;
        if (mas[next.tateh][next.yokoh] != '#' && next.tateh <= tate - 1 &&
            next.tateh >= 0 && next.yokoh >= 0 && next.yokoh <= yoko - 1) {
          que.push(next);
          dis[next.tateh][next.yokoh] = next.cost;
        }
      }
      if (que.empty()) {
        break;
      }
    }
    for (int i = 0; i <= tate - 1; i++) {
      for (int j = 0; j <= yoko - 1; j++) {
        if (kotae <= dis[i][j]) {
          kotae = dis[i][j];
        }
      }
    }
  }
  cout << kotae << endl;
  return 0;
}
