import sys
from collections import defaultdict, deque
import bisect
from heapq import *
from math import factorial, ceil, floor
from collections import defaultdict
import itertools
sys.setrecursionlimit(200000)
input = sys.stdin.readline

# N, M, = map(int, input().split())
# N = int(input())
# L = [int(v) for v in input().split()]
# L = [[int(v) for v in input().split()] for _ in range(N)]
# L = [int(input()) for _ in range(N)]
# L = [list(input().strip())  for _ in range(N)]
# S = input().strip()

N, M, = map(int, input().split())
L = [list(input().strip())  for _ in range(N)]

ans = 0
for _i in range(N):
    for _j in range(M):
        for _k in range(N):
            for _l in range(M):
                if _i == _k and _j == _l:
                    continue

                m = [[10 ** 18 for _ in range(M)] for _ in range(N)]
                def bfs(i, j, c):
                    if m[i][j] <= c:
                        return
                    if L[i][j] == "#":
                        return

                    m[i][j] = c
                    if L[i][j] == "G":
                        return
                    if i + 1 < N:
                        bfs(i + 1, j, c + 1)
                    if i - 1 >= 0:
                        bfs(i - 1, j, c + 1)
                    if j + 1 < M:
                        bfs(i, j + 1, c + 1)
                    if j - 1 >= 0:
                        bfs(i, j - 1, c + 1)

                if L[_i][_j] == "#" or L[_k][_l] == "#":
                    continue

                L[_i][_j] = "S"
                L[_k][_l] = "G"

                bfs(_i, _j, 0)
                if m[_k][_j] != 10 ** 18:
                    ans = max(ans, m[_k][_l])
                
                L[_i][_j] = "."
                L[_k][_l] = "."

print(ans)
