#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[20][20] = {};
void fanc(int x, int y, int c, int ai) {
  d[x][y] = 1;
  for (int i = 0; i < 4; i++) {
    if (ai == 0 && i == 2) {
      continue;
    } else if (ai == 2 && i == 0) {
      continue;
    } else if (ai == 1 && i == 3) {
      continue;
    } else if (ai == 3 && i == 1) {
      continue;
    } else if (x + dx[i] >= 0 && y + dy[i] >= 0 && x + dx[i] < H &&
               y + dy[i] < W && d[x + dx[i]][y + dy[i]] == 0) {
      if (field[x + dx[i]][y + dy[i]] == 'G') {
        if (ans > c + 1) ans = c + 1;
        return;
      } else if (x + dx[i] >= 0 && y + dy[i] >= 0 && x + dx[i] < H &&
                 y + dy[i] < W) {
        if (field[dx[i] + x][dy[i] + y] == '.') {
          fanc(x + dx[i], y + dy[i], c + 1, i);
        }
      }
    }
  }
  return;
}
int main(void) {
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> field[i][j];
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (i - 1 >= 0 && j - 1 >= 0 && j + 1 < W && i + 1 < H) {
        int cc = 0;
        for (int k = 0; k < 4; k++) {
          int xx = i + dx[i];
          int yy = j + dy[i];
          if (field[xx][yy] == '.') cc++;
        }
        if (cc == 4) field[i][j] = '#';
      }
    }
  }
  bool f[20][20] = {};
  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 && field[i][j] == '.' &&
              field[ii][jj] == '.') {
            ans = 1e9;
            for (int k = 0; k < H; k++)
              for (int kk = 0; kk < W; kk++) d[k][kk] = 0;
            field[ii][jj] = 'G';
            fanc(i, j, 0, 100);
            if (ans > rans) rans = ans;
            field[ii][jj] = '.';
          }
        }
      }
    }
  }
  cout << rans << endl;
  return 0;
}
