# coding: utf-8

# Your code here!

H,W = [int(i) for i in input().split()]

c=[[int(i) for i in input().split()] for _ in range(10)] 

A=[[int(i) for i in input().split()] for _ in range(H)]





dist = c

n=10

#n: 頂点数

for k in range(n):

    for i in range(n):

        for j in range(n):

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





ans = 0

for i in range(H):

    for j in range(W):

        if A[i][j] != -1:

            ans += dist[A[i][j]][1]

print(ans)     



# coding: utf-8

# Your code here!

H,W = [int(i) for i in input().split()]

c=[[int(i) for i in input().split()] for _ in range(10)] 

A=[[int(i) for i in input().split()] for _ in range(H)]





dist = c

n=10

#n: 頂点数

for k,dk in enumerate(dist):

    for di in dist:

        for j in range(n):

            if di[j] > di[k]+ dk[j]:

                di[j] = di[k]+ dk[j]





ans = 0

for i in range(H):

    for j in range(W):

        if A[i][j] != -1:

            ans += dist[A[i][j]][1]

print(ans)     



# coding: utf-8
# Your code here!
H, W = [int(i) for i in input().split()]
c = [[int(i) for i in input().split()] for _ in range(10)]
A = [[int(i) for i in input().split()] for _ in range(H)]
dist = c
n = 10
# n: 頂点数
for k in range(n):
    for i in range(n):
        for j in range(n):
            dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
ans = 0
for i in range(H):
    for j in range(W):
        if A[i][j] != -1:
            ans += dist[A[i][j]][1]
print(ans)

# coding: utf-8
# Your code here!
H, W = [int(i) for i in input().split()]
c = [[int(i) for i in input().split()] for _ in range(10)]
A = [[int(i) for i in input().split()] for _ in range(H)]
dist = c
n = 10
# n: 頂点数
for k, dk in enumerate(dist):
    for di in dist:
        for j in range(n):
            if di[j] > di[k] + dk[j]:
                di[j] = di[k] + dk[j]
ans = 0
for i in range(H):
    for j in range(W):
        if A[i][j] != -1:
            ans += dist[A[i][j]][1]
print(ans)

-for k in range(n): -    for i in range(n): +for k, dk in enumerate(dist): +    for di in dist: -            dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]) +            if di[j] > di[k] + dk[j]: +                di[j] = di[k] + dk[j]