from collections import deque
R,C = map(int,input().split())
state = []
mx=0
for i in range(R):
    state.append(input())
for sy in range(R):
    for sx  in range(C):
        st=state[sy][sx]
        if st =="#":
            continue
        for gy  in range(R):
            for gx in range(C):
                gl = state[gy][gx]
                if gl =="#":
                    continue
                else:    
                    start = [sy,sx]
                    goal = [gy,gx]
                    step = []
                    for i in range(R):
                        step.append([float('inf') for i in range(C)])
                    step[start[0]][start[1]] = 0
                    Q = deque()
                    Q.append((start[0],start[1]))
                    while Q:
                        y,x = Q.pop()
                        if y+1 < R:
                            if step[y+1][x] == float('inf') and state[y+1][x] == '.':
                                step[y+1][x] = step[y][x]+1
                                Q.appendleft((y+1,x))
                        if x+1 < C:
                            if step[y][x+1] == float('inf') and state[y][x+1] == '.':
                                step[y][x+1] = step[y][x]+1
                                Q.appendleft((y,x+1))
                        if y-1 >= 0:
                            if step[y-1][x] == float('inf') and state[y-1][x] == '.':
                                step[y-1][x] = step[y][x]+1
                                Q.appendleft((y-1,x))
                        if x-1 >= 0:
                            if step[y][x-1] == float('inf') and state[y][x-1] == '.':
                                step[y][x-1] = step[y][x]+1
                                Q.appendleft((y,x-1))
                    print(step[goal[0]][goal[1]])
                    from collections import deque
                    R,C = map(int,input().split())
                    start = list(map(int,input().split()))
                    goal = list(map(int,input().split()))
                    start = [start[0]-1,start[1]-1]
                    goal = [goal[0]-1,goal[1]-1]
                    state = []
                    for i in range(R):
                        state.append(input())
                    step = []
                    for i in range(R):
                        step.append([float('inf') for i in range(C)])
                    step[start[0]][start[1]] = 0
                    Q = deque()
                    Q.append((start[0],start[1]))
                    while Q:
                        y,x = Q.pop()
                        if y+1 < R:
                            if step[y+1][x] == float('inf') and state[y+1][x] == '.':
                                step[y+1][x] = step[y][x]+1
                                Q.appendleft((y+1,x))
                        if x+1 < C:
                            if step[y][x+1] == float('inf') and state[y][x+1] == '.':
                                step[y][x+1] = step[y][x]+1
                                Q.appendleft((y,x+1))
                        if y-1 >= 0:
                            if step[y-1][x] == float('inf') and state[y-1][x] == '.':
                                step[y-1][x] = step[y][x]+1
                                Q.appendleft((y-1,x))
                        if x-1 >= 0:
                            if step[y][x-1] == float('inf') and state[y][x-1] == '.':
                                step[y][x-1] = step[y][x]+1
                                Q.appendleft((y,x-1))
                    if mx < step[goal[0]][goal[1]]:
                        mx =step[goal[0]][goal[1]]
print(mx)

