from scipy.sparse.csgraph import csgraph_from_dense,floyd_warshall

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

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

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

g=csgraph_from_dense(c,null_value=0)

d=floyd_warshall(g)

ans=0

for i in range(h):

  for j in range(w):

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

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

print((int(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)]

def warshall_floyd(d,n):

    #d[i][j]: iからjへの最短距離

    for k in range(n):

        for i in range(n):

            for j in range(n):

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

    return d

cost=warshall_floyd(c,10)

magic=0

for i in range(h):

  for j in range(w):

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

      magic+=cost[a[i][j]][1]

print(magic)
from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall

h, w = list(map(int, input().split()))
c = [list(map(int, input().split())) for i in range(10)]
a = [list(map(int, input().split())) for i in range(h)]
g = csgraph_from_dense(c, null_value=0)
d = floyd_warshall(g)
ans = 0
for i in range(h):
    for j in range(w):
        if a[i][j] != -1:
            ans += d[a[i][j]][1]
print((int(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)]


def warshall_floyd(d, n):
    # d[i][j]: iからjへの最短距離
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j], d[i][k] + d[k][j])
    return d


cost = warshall_floyd(c, 10)
magic = 0
for i in range(h):
    for j in range(w):
        if a[i][j] != -1:
            magic += cost[a[i][j]][1]
print(magic)

-from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall +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)] -h, w = list(map(int, input().split())) -c = [list(map(int, input().split())) for i in range(10)] -a = [list(map(int, input().split())) for i in range(h)] -g = csgraph_from_dense(c, null_value=0) -d = floyd_warshall(g) -ans = 0 + +def warshall_floyd(d, n): +    # d[i][j]: iからjへの最短距離 +    for k in range(n): +        for i in range(n): +            for j in range(n): +                d[i][j] = min(d[i][j], d[i][k] + d[k][j]) +    return d + + +cost = warshall_floyd(c, 10) +magic = 0 -            ans += d[a[i][j]][1] -print((int(ans))) +            magic += cost[a[i][j]][1] +print(magic)