import numpy as np
from scipy.ndimage.filters import minimum_filter, maximum_filter
import copy


def calcstep(maze, s, g):
    h, w = len(maze), len(maze[0])
    maze[s[0]] = maze[s[0]][:s[1]] + 's' + maze[s[0]][s[1]+1:]
    maze[g[0]] = maze[g[0]][:g[1]] + 'g' + maze[g[0]][g[1]+1:]
    
    cost = np.zeros((h, w), dtype=int) + 999
    done = np.zeros((h, w), dtype=bool)
    barrier = np.zeros((h, w), dtype=bool)

    g = np.array([[0, 1, 0],
                  [1, 1, 1],
                  [0, 1, 0]])

    for i in range(h):
        maze[i] = list(maze[i])
        for j in range(w):
            if maze[i][j] == 's':
                start = (i, j)
                cost[i, j] = 0
                done[i, j] = True
            if maze[i][j] == 'g':
                goal = (i, j)
            if maze[i][j] == '#':
                barrier[i, j] = True

    for i in range(1, 999):

        done_next = maximum_filter(done, footprint=g) * ~done

        cost_next = minimum_filter(cost, footprint=g) * done_next
        cost_next[done_next] += 1
        cost[done_next] = cost_next[done_next]
        cost[barrier] = 999
        done[done_next] = done_next[done_next]
        done[barrier] = False

        if done[goal[0], goal[1]] == True:
            break
    return i



h, w = map(int, input().split())
maze = [input() for _ in range(h)]


res = -1
for i in range(h):
    for j in range(w):
        if(maze[i][j] == '#'): continue
        
        for k in range(h):
            for l in range(w):
                if(maze[k][l] == '#'): continue
                if(i==k and j==l): continue
                tmpmaze = copy.deepcopy(maze)
                steps = calcstep(tmpmaze, [i, j], [k, l])
                if(steps > res):
                    res = steps
print(res)