Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

type Position = []int

func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

func main() {
	scanner.Split(bufio.ScanWords)

	for {
		h := nextInt()
		w := nextInt()

		if h == 0 && w == 0 {
			break
		}
		positions := map[string]Position{}
		for i := 0; i < h; i++ {
			row := nextString()
			for j := 0; j < w; j++ {
				pos := []int{i, j}
				positions[row[j:(j+1)]] = pos
			}
		}
		// fmt.Println(positions)

		x := 0
		y := 0
		totalDistance := 0

		queryRow := nextString()
		for i := 0; i < len(queryRow); i++ {
			q := queryRow[i:(i + 1)]

			nextPos := positions[q]
			xNext := nextPos[0]
			yNext := nextPos[1]

			totalDistance += abs(xNext-x) + abs(yNext-y) + 1
			x = xNext
			y = yNext
			// fmt.Println(nextPos, totalDistance)
		}
		fmt.Println(totalDistance)
	}
}

Python: import sys
input=sys.stdin.readline
while True:
	rr=[]
	h,w=map(int,input().split())
	if h==w==0:
		break
	else:
		for i in range(h):
			r=input().rstrip()
			rr.append(r)
		s=input().rstrip()
		ans=0
		now=[0,0]
		for i in range(len(s)):
			for j in range(h):
				for k in range(w):
					if rr[j][k]==s[i]:
						ans+=abs(now[0]-j)+abs(now[1]-k)
						now=[j,k]
		print(ans+len(s))

Python: while 1:
    H, W = map(int, input().split())
    if H == 0:
        break
    P = {}
    for i in range(H):
        r = input()
        for j, c in enumerate(r):
            P[c] = (i, j)
    S = input()
    ans = len(S)
    px = 0; py = 0
    for c in S:
        x, y = P[c]
        ans += abs(x - px) + abs(y - py)
        px = x; py = y
    print(ans)

Python: while True:
    h,w = map(int,input().split())
    if(h+w == 0):
        break
    li = list()
    for i in range(h):
        s = input()
        li.append(s)
    d = dict()
    for i,l in enumerate(li):
        for j,char in enumerate(l):
            d[char] = (i,j)
    target = input()
    ans = 0
    now = (0,0)
    for t in target:
        next = d[t]
        ans += abs(now[0]-next[0]) + abs(now[1]-next[1])
        now=next
    print(ans + len(target))

Python: while 1:
    h, w = map(int, input().split())
    if h == 0:
        break
    d = {}
    for i in range(h):
        s = input()
        for j in range(w):
            if s[j] != '_':
                d[s[j]] = [j, i]
    s = input()
    cnt = 0
    for i in range(len(s)):
        if i == 0:
            cnt += d[s[i]][0] + d[s[i]][1] + 1
        else:
            cnt += abs(d[s[i]][0]-d[s[i-1]][0]) + abs(d[s[i]][1]-d[s[i-1]][1]) + 1
    print(cnt)

Python: while True:
    h, w = map(int, input().split())
    if h == w == 0:
        break
    mp = {}
    for r in range(h):
        s = input()
        for c in range(w):
            mp[s[c]] = [r, c]
    s = input()
    now = [0, 0]
    ans = 0
    for i in range(len(s)):
        to = mp[s[i]]
        ans += abs(now[0] - to[0]) + abs(now[1] - to[1]) + 1
        now = to
    print(ans)

Python: #!/usr/bin/python3

import array
from fractions import Fraction
import functools
import itertools
import math
import os
import sys


def main():
    while True:
        H, W = read_ints()
        if (H, W) == (0, 0):
            break
        R = [inp() for _ in range(H)]
        S = inp()
        print(solve(H, W, R, S))


def solve(H, W, R, S):
    pos_map = {}
    for y in range(H):
        for x in range(W):
            c = R[y][x]
            if c != '_':
                pos_map[c] = (x, y)

    ans = 0
    cx = 0
    cy = 0
    for c in S:
        x, y = pos_map[c]
        ans += abs(x - cx) + abs(y - cy) + 1
        cx, cy = x, y

    return ans


###############################################################################
# AUXILIARY FUNCTIONS

DEBUG = 'DEBUG' in os.environ


def inp():
    return sys.stdin.readline().rstrip()


def read_int():
    return int(inp())


def read_ints():
    return [int(e) for e in inp().split()]


def dprint(*value, sep=' ', end='\n'):
    if DEBUG:
        print(*value, sep=sep, end=end)


if __name__ == '__main__':
    main()

Python: # coding=utf-8

###
### for python program
###

import sys
import math

# math class
class mymath:
    ### pi
    pi = 3.14159265358979323846264338

    ### Prime Number
    def pnum_eratosthenes(self, n):
        ptable = [0 for i in range(n+1)]
        plist = []

        for i in range(2, n+1):
            if ptable[i]==0:
                plist.append(i)
                for j in range(i+i, n+1, i):
                    ptable[j] = 1
        return plist

    def pnum_check(self, n):
        if (n==1):
            return False
        elif (n==2):
            return True
        else:
            for x in range(2,n):
                if(n % x==0):
                    return False
            return True

    ### GCD
    def gcd(self, a, b):
        if b == 0:
            return a
        return self.gcd(b, a%b)

    ### LCM
    def lcm(self, a, b):
        return (a*b)//self.gcd(a,b)

    ### Mat Multiplication
    def mul(self, A, B):
        ans = []
        for a in A:
            c = 0
            for j, row in enumerate(a):
                c += row*B[j]
            ans.append(c)
        return ans
    
    ### intチェック
    def is_integer(self, n):
        try:
            float(n)
        except ValueError:
            return False
        else:
            return float(n).is_integer()

    ### 幾何学問題用
    def dist(self, A, B):
        d = 0
        for i in range(len(A)):
            d += (A[i]-B[i])**2            
        d = d**(1/2)
        return d

    ### 絶対値
    def abs(self, n):
        if n >= 0:
            return n
        else:
            return -n

mymath = mymath()

### output class
class output:
    ### list
    def list(self, l):
        l = list(l)
        #print(" ", end="")
        for i, num in enumerate(l):
            print(num, end="")
            if i != len(l)-1:
                print(" ", end="")
        print()

output = output()

### input sample
#i = input()
#N = int(input())
#A, B, C = [x for x in input().split()]
#N, K = [int(x) for x in input().split()]
#inlist = [int(w) for w in input().split()]

#R = float(input())
#A.append(list(map(int,input().split())))

#for line in sys.stdin.readlines():
#    x, y = [int(temp) for temp in line.split()]

#abc list
#abc = [chr(ord('a') + i) for i in range(26)]

### output sample
# print("{0} {1} {2:.5f}".format(A//B, A%B, A/B))
# print("{0:.6f} {1:.6f}".format(R*R*math.pi,R*2*math.pi))
# print(" {}".format(i), end="")

def printA(A):
    N = len(A)
    for i, n in enumerate(A):
        print(n, end='')
        if i != N-1:
            print(' ', end='')
    print()

# リスト内包表記 ifあり
# [x-k if x != 0 else x for x in C]

# ソート
# N = N.sort()

# 10000個の素数リスト
# P = mymath.pnum_eratosthenes(105000)

def get_input():
    N = []
    while True:
        try:
            N.append(input())
            #N.append(int(input()))
            #N.append(float(input()))
        except EOFError:
            break
    return N

while True:
    H, W = [int(x) for x in input().split()]
    if H==0 and W==0:
        break
    R = []
    for i in range(H):
        R.append(input())
    S = input()

    count = 0
    x = 0
    y = 0
    for s in S:
        for i, row in enumerate(R):
            for j, r in enumerate(row):
                if s == r:
                    count += (mymath.abs(i-x)+mymath.abs(j-y)+1)
                    x = i
                    y = j
                    break

    print(count)