import sys input = sys.stdin.readline from collections import Counter N,H,W=list(map(int,input().split())) MAP=[input().strip() for i in range(H)] x0,y0=list(map(int,input().split())) x0-=1 y0-=1 S=Counter() for i in range(N): x,y,t=list(map(int,input().split())) x-=1 y-=1 S[(x,y,t)]+=1 DP=[[-1<<60]*W for i in range(H)] DP[x0][y0]=0 for tt in range(1010): NDP=[[-1<<60]*W for i in range(H)] for i in range(H): for j in range(W): if DP[i][j]>=0: for x,y in [(i,j),(i+1,j),(i-1,j),(i,j-1),(i,j+1)]: if 0<=x