import sys; input = sys.stdin.readline; from array import *; from collections import * R, C = map(int, input().split()); M = array('b') for _ in range(R): M.extend(map(ord, input().strip())) S = array('b', map(ord, input().strip()+'*')); N = len(S); D = array('i', [-1]*N*R*C); D[0] = 0; Q = deque([0]); K = ((1, 0), (0, 1), (-1, 0), (0, -1)); G = [set() for _ in range(R*C)] for r in range(R): for c in range(C): for dr, dc in K: rr, cc = r, c while R>rr>-1rr>-1