magialist=[]

datalist=[]

h,w=list(map(int,input().split()))

for i in range(10):

    add=list(map(int,input().split()))

    magialist.append(add)



for j in range(h):

    add1=list(map(int,input().split()))

    datalist.extend(add1)

 

storelist=[[[[] for i in range(10)] for i in range(10)] for i in range(10)]

    #わーシャルフロイド法

for i in range(10):

    for j in range(10):

            a=min(magialist[i][j],magialist[i][0]+magialist[0][j])

            storelist[0][i][j]=a

k=1

while k<=9:

    for i in range(10):

        for j in range(10):

                a=min(storelist[k-1][i][j],storelist[k-1][i][k]+storelist[k-1][k][j])

                storelist[k][i][j]=a

    k+=1



uselist=[]

                                                                                        

for j in range(10):

    uselist.append(storelist[9][j][1])

ans=0

for nums in range(h*w):

        if datalist[nums]==-1:

            continue

        else:

            for j in range(10):

                if datalist[nums]==j:

                    ans+=uselist[j]

print(ans)
h,w=list(map(int,input().split()))

d=[list(map(int,input().split())) for _ in range(10)]

e=[list(map(int,input().split())) for _ in range(h)]

def warshall_floyd(d,n):

    #d[i][j]=i番目の頂点からj番目の頂点までの最短距離

    for k in range(n+1):

        for i in range(n+1):

            for j in range(n+1):

                d[i][j] = min(d[i][j],d[i][k] + d[k][j])

    return d

d=warshall_floyd(d,9)

ans=0

for i in range(h):

    for j in range(w):

        if e[i][j]>=0:

            ans+=d[e[i][j]][1]

print(ans)
magialist = []
datalist = []
h, w = list(map(int, input().split()))
for i in range(10):
    add = list(map(int, input().split()))
    magialist.append(add)
for j in range(h):
    add1 = list(map(int, input().split()))
    datalist.extend(add1)
storelist = [[[[] for i in range(10)] for i in range(10)] for i in range(10)]
# わーシャルフロイド法
for i in range(10):
    for j in range(10):
        a = min(magialist[i][j], magialist[i][0] + magialist[0][j])
        storelist[0][i][j] = a
k = 1
while k <= 9:
    for i in range(10):
        for j in range(10):
            a = min(
                storelist[k - 1][i][j], storelist[k - 1][i][k] + storelist[k - 1][k][j]
            )
            storelist[k][i][j] = a
    k += 1
uselist = []
for j in range(10):
    uselist.append(storelist[9][j][1])
ans = 0
for nums in range(h * w):
    if datalist[nums] == -1:
        continue
    else:
        for j in range(10):
            if datalist[nums] == j:
                ans += uselist[j]
print(ans)

h, w = list(map(int, input().split()))
d = [list(map(int, input().split())) for _ in range(10)]
e = [list(map(int, input().split())) for _ in range(h)]


def warshall_floyd(d, n):
    # d[i][j]=i番目の頂点からj番目の頂点までの最短距離
    for k in range(n + 1):
        for i in range(n + 1):
            for j in range(n + 1):
                d[i][j] = min(d[i][j], d[i][k] + d[k][j])
    return d


d = warshall_floyd(d, 9)
ans = 0
for i in range(h):
    for j in range(w):
        if e[i][j] >= 0:
            ans += d[e[i][j]][1]
print(ans)

-magialist = [] -datalist = [] -for i in range(10): -    add = list(map(int, input().split())) -    magialist.append(add) -for j in range(h): -    add1 = list(map(int, input().split())) -    datalist.extend(add1) -storelist = [[[[] for i in range(10)] for i in range(10)] for i in range(10)] -# わーシャルフロイド法 -for i in range(10): -    for j in range(10): -        a = min(magialist[i][j], magialist[i][0] + magialist[0][j]) -        storelist[0][i][j] = a -k = 1 -while k <= 9: -    for i in range(10): -        for j in range(10): -            a = min( -                storelist[k - 1][i][j], storelist[k - 1][i][k] + storelist[k - 1][k][j] -            ) -            storelist[k][i][j] = a -    k += 1 -uselist = [] -for j in range(10): -    uselist.append(storelist[9][j][1]) +d = [list(map(int, input().split())) for _ in range(10)] +e = [list(map(int, input().split())) for _ in range(h)] + + +def warshall_floyd(d, n): +    # d[i][j]=i番目の頂点からj番目の頂点までの最短距離 +    for k in range(n + 1): +        for i in range(n + 1): +            for j in range(n + 1): +                d[i][j] = min(d[i][j], d[i][k] + d[k][j]) +    return d + + +d = warshall_floyd(d, 9) -for nums in range(h * w): -    if datalist[nums] == -1: -        continue -    else: -        for j in range(10): -            if datalist[nums] == j: -                ans += uselist[j] +for i in range(h): +    for j in range(w): +        if e[i][j] >= 0: +            ans += d[e[i][j]][1]