import collections

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

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

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



result = [0]*10

    

    

def way(n,e,i,p): #現在の数字、現在の必要魔力、始点、暫定最低魔力

    for j in range(0,10):

        if (n == j):

            continue

        if (n == 1):

            if (e < result[i]):

                result[i] = e

            break

        if (e +  c[n][j] < p):

            q = e + c[n][j]

            way(j,q,i,p)

        

for i in range(0,10):

    result[i] = c[i][1]

    way(i,0,i,result[i])

    



r = []

for w in range(0,10):

    t = 0

    for k in range(0,h):

        l = collections.Counter(a[k])

        t += l[w]

    r.append(t)



ans = 0

for z in range(0,10):

    ans += result[z] * r[z]

print (ans)

        

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

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

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



for k in range(10):

    for i in range(10):

        for j in range(10):

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

            

ans = 0

for i in range(h):

    for j in range(w):

        if (a[i][j]!=-1):

            ans += c[a[i][j]][1]

print(ans)
import collections

h, w = list(map(int, input().split()))
c = [list(map(int, input().split())) for _ in range(10)]
a = [list(map(int, input().split())) for _ in range(h)]
result = [0] * 10


def way(n, e, i, p):  # 現在の数字、現在の必要魔力、始点、暫定最低魔力
    for j in range(0, 10):
        if n == j:
            continue
        if n == 1:
            if e < result[i]:
                result[i] = e
            break
        if e + c[n][j] < p:
            q = e + c[n][j]
            way(j, q, i, p)


for i in range(0, 10):
    result[i] = c[i][1]
    way(i, 0, i, result[i])
r = []
for w in range(0, 10):
    t = 0
    for k in range(0, h):
        l = collections.Counter(a[k])
        t += l[w]
    r.append(t)
ans = 0
for z in range(0, 10):
    ans += result[z] * r[z]
print(ans)

h, w = list(map(int, input().split()))
c = [list(map(int, input().split())) for _ in range(10)]
a = [list(map(int, input().split())) for _ in range(h)]
for k in range(10):
    for i in range(10):
        for j in range(10):
            c[i][j] = min(c[i][j], c[i][k] + c[k][j])
ans = 0
for i in range(h):
    for j in range(w):
        if a[i][j] != -1:
            ans += c[a[i][j]][1]
print(ans)

-import collections - -result = [0] * 10 - - -def way(n, e, i, p):  # 現在の数字、現在の必要魔力、始点、暫定最低魔力 -    for j in range(0, 10): -        if n == j: -            continue -        if n == 1: -            if e < result[i]: -                result[i] = e -            break -        if e + c[n][j] < p: -            q = e + c[n][j] -            way(j, q, i, p) - - -for i in range(0, 10): -    result[i] = c[i][1] -    way(i, 0, i, result[i]) -r = [] -for w in range(0, 10): -    t = 0 -    for k in range(0, h): -        l = collections.Counter(a[k]) -        t += l[w] -    r.append(t) +for k in range(10): +    for i in range(10): +        for j in range(10): +            c[i][j] = min(c[i][j], c[i][k] + c[k][j]) -for z in range(0, 10): -    ans += result[z] * r[z] +for i in range(h): +    for j in range(w): +        if a[i][j] != -1: +            ans += c[a[i][j]][1]