from collections import deque

n, m = map(int, input().split())
maps = [list(input()) for _ in range(n)]

def bfs(sx, sy, gx, gy):
    distance = [[float("inf")] * m for _ in range(n)]
    dx, dy = [1, 0, -1, 0], [0, 1, 0, -1]

    for i in range(n):
        for k in range(m):
            if maps[i][k] == "S":
                sx, sy = i, k

            if maps[i][k] == "G":
                gx, gy = i, k

    que = deque([])
    que.append((sx, sy))
    distance[sx][sy] = 0

    while que:
        p = que.popleft()
        if p[0] == gx and  p[1] == gy:
            break

        for i in range(4):
            nx, ny = p[0] + dx[i], p[1] + dy[i]
            if 0 <= nx < n and 0 <= ny < m and maps[nx][ny] != "#" and distance[nx][ny] == float("inf"):
                que.append((nx, ny))
                distance[nx][ny] = distance[p[0]][p[1]] + 1

    return distance[gx][gy]

s, g = [], []
ans = 0
for i in range(n):
    for j in range(m):
        s.append([i, j]), g.append([i, j])

for i in range(n * m):
    for j in range(n * m):
        if maps[s[i][0]][s[i][1]] == "." and maps[g[j][0]][g[j][1]] == ".":
            ans = max(ans, bfs(s[i][0], s[i][1], g[j][0], g[j][1]))

print(ans)
