R, C = list(map(int, input().split(" "))) sy, sx = list(map(int, input().split(" "))) gy, gx = list(map(int, input().split(" "))) field_list = [] position_list = [] for i in range(R): temp = input() temp_list = [] for j in temp: temp_list.append(j) field_list.append(temp_list) # print(field_list) def BFF(x, y, count): if x == gx-1 and y == gy-1: print(count) exit() for mx, my in [[1, 0], [-1, 0], [0, 1], [0, -1]]: if 0 <= x+mx < C and 0 <= y+my < R: if (x+mx, y+my) not in position_list: if field_list[y+my][x+mx] == ".": position_list.append((x+mx, y+my)) BFF(x+mx, y+my, count+1) position_list.append((sx-1, sy-1)) BFF(sx-1, sy-1, 0)