import copy
h,w=map(int,input().split())
maze = [['#']+list(input())+['#'] for i in range(h)]
maze.insert(0,['#']*(w+2))
maze.append(['#']*(w+2))

def get_max_length_from_start(maze,x,y):
    max_length_list=[]
    tmp_maze=copy.deepcopy(maze)
    searching = [[y,x]]
    length=0
    while len(searching)!=0:
        next_searching_list=[]
        for point in searching:
            tmp_maze[point[0]][point[1]]=length
            mawari = [[point[0]+1,point[1]],[point[0]-1,point[1]],[point[0],point[1]+1],[point[0],point[1]-1]]
            next_searching = list(filter(lambda x:tmp_maze[x[0]][x[1]]==".",mawari))
            if len(next_searching)==0:
                max_length_list.append(length)
                continue
            next_searching_list += next_searching
        searching = list(map(list, set(map(tuple, next_searching_list))))
        length+=1
    return sorted(max_length_list)

true_max_length_list=[]
for y in range(1,h+1):
    for x in range(1,w+1):
        if maze[y][x]=='#':
            continue
        max_length_list=get_max_length_from_start(maze,x,y)
        if len(max_length_list)>=2:
            max_length=max_length_list[-1]+max_length_list[-2]
        else:
            max_length=max_length_list[0]
        true_max_length_list.append(max_length)

print(min(true_max_length_list))