#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 20;
int H, W;
void DP(vector<vector<char>> S, int h, int w, int count, int *min) {
  if (S[h][w] == 'G') {
    if (*min > count) *min = count;
  } else {
    S[h][w] = '#';
    if (h > 0 && S[h - 1][w] != '#') DP(S, h - 1, w, count + 1, min);
    if (h < H - 1 && S[h + 1][w] != '#') DP(S, h + 1, w, count + 1, min);
    if (w > 0 && S[h][w - 1] != '#') DP(S, h, w - 1, count + 1, min);
    if (w < W - 1 && S[h][w + 1] != '#') DP(S, h, w + 1, count + 1, min);
  }
}
int main() {
  cin >> H >> W;
  vector<vector<char>> S(H, vector<char>(W));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) cin >> S[i][j];
  }
  int ans = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      for (int k = 0; k < H; k++) {
        for (int l = 0; l < W; l++) {
          if ((S[i][j] != '#') && (S[k][l] != '#') &&
              (!((i == k) && (j == l)))) {
            char tmp = S[k][l];
            S[k][l] = 'G';
            int min = INF;
            DP(S, i, j, 0, &min);
            S[k][l] = tmp;
            if (ans < min) ans = min;
          }
        }
      }
    }
  }
  cout << ans;
}
