#include <bits/stdc++.h>
using namespace std;
int ans = 0;
int tmpAns = 40000000;
int H, W;
bool visited[20][20];
vector<vector<int>> maze;
void dfs(int x, int y, int cnt) {
  if (x < 0) return;
  if (y < 0) return;
  if (x > H - 1) return;
  if (y > W - 1) return;
  if (visited[x][y]) return;
  if (maze[x][y] == '#') return;
  if (maze[x][y] == 'G') {
    tmpAns = min(tmpAns, cnt);
    return;
  }
  visited[x][y] = true;
  dfs(x - 1, y, cnt + 1);
  dfs(x + 1, y, cnt + 1);
  dfs(x, y - 1, cnt + 1);
  dfs(x, y + 1, cnt + 1);
}
void reset() {
  for (int i = 0; i < 20; i++) {
    for (int j = 0; j < 20; j++) {
      visited[i][j] = false;
    }
  }
  tmpAns = 40000000;
}
int main() {
  cin >> H >> W;
  maze.resize(H);
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      char c;
      cin >> c;
      maze[i].push_back(c);
    }
  }
  vector<int> ansv;
  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 (i == k && j == l) continue;
          if (maze[i][j] == '#') continue;
          if (maze[k][l] == '#') continue;
          maze[i][j] = 'S';
          maze[k][l] = 'G';
          dfs(i, j, 0);
          ansv.push_back(tmpAns);
          reset();
          maze[i][j] = '.';
          maze[k][l] = '.';
        }
      }
    }
  }
  sort(ansv.begin(), ansv.end(), greater<int>());
  cout << ansv[0] << endl;
  return 0;
}
