from heapq import heappush, heappop INF = float('inf') x = [1, 0, 0, -1] y = [0, 1, -1, 0] H, W = map(int, input().split()) U, D, R, L, K, P = map(int, input().split()) cost = [D, R, L, U] sx, sy, gx, gy = map(int, input().split()) sx-=1;sy-=1;gx-=1;gy-=1 C = [list(input()) for _ in range(H)] adj = [list() for _ in range(H*W)] for i in range(H): for j in range(W): if C[i][j] == '#': continue else: for cc, xx, yy in zip(cost, x, y): cx, cy = i+xx, j+yy if 0<=cx