import copy


def bfs(x, y, goal_h, goal_w, c, s):
    if x < 0 or y < 0 or x >= H or y >= W or s[x][y] == '#':
        return 10000
    if x == goal_h and y == goal_w:
        return c
    s[x][y] = '#'
    return min([bfs(next_x, next_y,goal_h, goal_w, c+1, copy.deepcopy(s))
                for next_x, next_y in ([x+1, y], [x-1, y], [x, y+1], [x, y-1])])
    
H, W = map(int, input().split())
S = []
cnt = []


for _ in range(H):
    S.append([i for i in input()])
    
for start_h in range(H):
    for start_w in range(W):
        if S[start_h][start_w] == '#':
            continue
        for goal_h in range(H):
            for goal_w in range(W):
                if S[goal_h][goal_w] == '#':
                    continue
                if start_h == goal_h or start_w == goal_w:
                    cnt.append(abs(start_h - goal_h) + abs(start_w - goal_w))
                    continue
                c = 0
                s = copy.deepcopy(S)
                s[start_h][start_w], s[goal_h][goal_w] = 'S', 'G'
                cnt.append(bfs(start_h, start_w, goal_h, goal_w, c, s))

print(max(cnt))