#include <bits/stdc++.h>
using namespace std;
char field[22][22];
int H, W;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int ans = 1e9, rans = 0;
bool d[22][22] = {};
queue<pair<int, int>> q;
queue<int> qq;
int main(void) {
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> field[i][j];
    }
  }
  bool f[22][22] = {0};
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      for (int ii = 0; ii < H; ii++) {
        for (int jj = 0; jj < W; jj++) {
          if (i == ii && j == jj) continue;
          if (field[i][j] == '.' && field[ii][jj] == '.' && f[i][j] == 0) {
            ans = 1e9;
            for (int k = 0; k <= H; k++)
              for (int kk = 0; kk <= W; kk++) d[k][kk] = 0;
            f[i][j] = 1;
            f[ii][jj] = 1;
            field[ii][jj] = 'G';
            pair<int, int> tem;
            int c;
            q.push(pair<int, int>(i, j));
            qq.push(0);
            while (!q.empty()) {
              tem = q.front();
              c = qq.front();
              q.pop();
              qq.pop();
              d[tem.first][tem.second] = 1;
              int x = tem.first;
              int y = tem.second;
              for (int s = 0; s < 4; s++) {
                int xx = x + dx[s], yy = y + dy[s];
                if (xx >= 0 && yy >= 0 && xx < H && yy < W &&
                    field[xx][yy] == 'G' && d[xx][yy] == 0) {
                  if (ans > c + 1) ans = c + 1;
                } else if (xx >= 0 && yy >= 0 && xx < H && yy < W &&
                           field[xx][yy] == '.' && d[xx][yy] == 0) {
                  q.push(pair<int, int>(xx, yy));
                  qq.push(c + 1);
                }
              }
            }
            if (ans > rans) rans = ans;
            field[ii][jj] = '.';
          }
        }
      }
    }
  }
  cout << rans << endl;
  return 0;
}
