import copy

def maze_maker():
    h,w = map(int,input().split())
    maze = []
    for i in range(h):
        maze.append(list(input()))
    return maze,h,w

def search():
    now = [[i1,j1,0]]
    while True:
        for i in now[:]:
            #上
            if i[0] == 0:
                pass
            else:
                if meiro[i[0]-1][i[1]] == "G":
                    return i[2]+1
                if meiro[i[0]-1][i[1]] == ".":
                    now.append([i[0]-1,i[1],i[2]+1])
                    meiro[i[0]-1][i[1]] = i[2]+1
            #下
            if i[0] == h-1:
                pass
            else:
                if meiro[i[0]+1][i[1]] == "G":
                    return i[2]+1
                if meiro[i[0]+1][i[1]] == ".":
                    now.append([i[0]+1,i[1],i[2]+1])
                    meiro[i[0]+1][i[1]] = i[2]+1
            #左
            if i[1] == 0:
                pass
            else:
                if meiro[i[0]][i[1]-1] == "G":
                    return i[2]+1
                if meiro[i[0]][i[1]-1] == ".":
                    now.append([i[0],i[1]-1,i[2]+1])
                    meiro[i[0]][i[1]-1] = i[2]+1
            #右
            if i[1] == w-1:
                pass
            else:
                if meiro[i[0]][i[1]+1] == "G":
                    return i[2]+1
                if meiro[i[0]][i[1]+1] == ".":
                    now.append([i[0],i[1]+1,i[2]+1])
                    meiro[i[0]][i[1]+1] = i[2]+1
            now.remove(i)

saitankeiro = 0
maze,h,w = maze_maker()
for i1 in range(h):
    for j1 in range(w):
        if maze[i1][j1] != "#":
            for i2 in range(h):
                for j2 in range(w):
                    if maze[i2][j2] != "#" and i1 != i2 and j1 != j2:
                        meiro = copy.deepcopy(maze)
                        meiro[i1][j1] = "S"
                        meiro[i2][j2] = "G"
                        saitankeiro = max(saitankeiro,search())
print(saitankeiro)