def bfs():
      global count
      now=que.popleft()
      now_step=steps[now[0]][now[1]]
      for dx,dy in (0,-1),(1,0),(0,1),(-1,0):
            ny=now[0]+dy
            nx=now[1]+dx
            if 0<=ny<=H-1 and 0<=nx<=W-1:
                  if maze[ny][nx]=="G":
                        count=now_step+1
                        break
                  elif maze[ny][nx]==".":
                        if ([ny,nx] in que)==False:
                              que.append([ny,nx])
                              steps[ny][nx]=now_step+1
                  else:
                        pass

import copy
from collections import deque

H,W=map(int,input().split())
maze=[]
for i in range(H):
   S=input()
   S=[c for c in S]
   maze.append(S)

maze_copy=copy.deepcopy(maze)
max_step=0

for s_i in range(H):
   for s_j in range(W):
      if maze[s_i][s_j]=="#":
         continue

      for g_i in range(H):
         for g_j in range(W):
            if maze[g_i][g_j]=="#":
               continue
            
            maze=copy.deepcopy(maze_copy)
            que=deque([[s_i,s_j]])
            maze[s_i][s_j]="S"
            maze[g_i][g_j]="G"
            steps=[[-1]*W for c in range(H)]
            steps[s_i][s_j]=0
            count=0

            if (s_i,s_j)==(g_i,g_j):
               max_step=max(max_step,0)

            else:
               while(count==0):
                  bfs()
               max_step=max(max_step,count)

print(max_step)