import heapq n=int(input()) h=[list(map(int,input().split())) for i in range(n)] s=[input() for i in range(n)] a=[list(map(int,input().split())) for i in range(n-1)] b=[list(map(int,input().split())) for i in range(n)] q=int(input()) rce=[tuple(map(int,input().split())) for i in range(n)] INF=int(4e18) def to(i,j): return i*n+j mx=[-INF]*n*n for t in range(2): g=[[] for i in range(n*n+1)] for i in range(n-1): for j in range(n): p=h[i][j]+h[i+1][j] l=-p-a[i][j]*abs(p) r=-p+a[i][j]*abs(p) if (i+j)%2==t: g[to(i,j)].append((to(i+1,j),-l)) g[to(i+1,j)].append((to(i,j),r)) else: g[to(i,j)].append((to(i+1,j),r)) g[to(i+1,j)].append((to(i,j),-l)) for i in range(n): for j in range(n-1): p=h[i][j]+h[i][j+1] l=-p-b[i][j]*abs(p) r=-p+b[i][j]*abs(p) if (i+j)%2==t: g[to(i,j)].append((to(i,j+1),-l)) g[to(i,j+1)].append((to(i,j),r)) else: g[to(i,j)].append((to(i,j+1),r)) g[to(i,j+1)].append((to(i,j),-l)) for i in range(n): for j in range(n): if s[i][j]=='=' or s[i][j]=='-': if (i+j)%2==t: g[n*n].append((to(i,j),0)) else: g[to(i,j)].append((n*n,0)) if s[i][j]=='=' or s[i][j]=='+': if (i+j)%2==t: g[to(i,j)].append((n*n,0)) else: g[n*n].append((to(i,j),0)) dist=[INF]*(n*n+1) dist[n*n]=0 hq=[(0,n*n)] heapq.heapify(hq) while hq: d,i=heapq.heappop(hq) if d!=dist[i]: continue for ti,td in g[i]: if dist[i]+td