# coding: utf-8



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)]

d = [float('inf')] * 10



def bellman_ford(s):

    d[s] = 0

    for i in range(10):

        for j in range(10):

            for k in range(10):

                if d[j] != float('inf') and d[k] > d[j] + c[k][j]:

                    d[k] = d[j] + c[k][j]



bellman_ford(1)

ans = 0



for i in A:

    for j in i:

        if j != -1:

            ans += d[j]



print(ans)

# print sum(sum(d[a] if a != -1 else 0 for a in aa) for aa in A)

# coding: utf-8



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)]

d = [float('inf')] * 10



d[1] = 0



for i in range(10):

    for j in range(10):

        for k in range(10):

            if d[j] != float('inf') and d[k] > d[j] + c[k][j]:

                d[k] = d[j] + c[k][j]



print(sum(sum(d[a] if a != -1 else 0 for a in aa) for aa in A))

# coding: utf-8
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)]
d = [float("inf")] * 10


def bellman_ford(s):
    d[s] = 0
    for i in range(10):
        for j in range(10):
            for k in range(10):
                if d[j] != float("inf") and d[k] > d[j] + c[k][j]:
                    d[k] = d[j] + c[k][j]


bellman_ford(1)
ans = 0
for i in A:
    for j in i:
        if j != -1:
            ans += d[j]
print(ans)
# print sum(sum(d[a] if a != -1 else 0 for a in aa) for aa in A)

# coding: utf-8
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)]
d = [float("inf")] * 10
d[1] = 0
for i in range(10):
    for j in range(10):
        for k in range(10):
            if d[j] != float("inf") and d[k] > d[j] + c[k][j]:
                d[k] = d[j] + c[k][j]
print(sum(sum(d[a] if a != -1 else 0 for a in aa) for aa in A))

- - -def bellman_ford(s): -    d[s] = 0 -    for i in range(10): -        for j in range(10): -            for k in range(10): -                if d[j] != float("inf") and d[k] > d[j] + c[k][j]: -                    d[k] = d[j] + c[k][j] - - -bellman_ford(1) -ans = 0 -for i in A: -    for j in i: -        if j != -1: -            ans += d[j] -print(ans) -# print sum(sum(d[a] if a != -1 else 0 for a in aa) for aa in A) +d[1] = 0 +for i in range(10): +    for j in range(10): +        for k in range(10): +            if d[j] != float("inf") and d[k] > d[j] + c[k][j]: +                d[k] = d[j] + c[k][j] +print(sum(sum(d[a] if a != -1 else 0 for a in aa) for aa in A))