import heapq



H, W = list(map(int, input().split()))

C = []

for _ in range(10):

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

    C.append(c)



A = []

for _ in range(H):

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

    A.append(a)





def dijkstra(start, goal, n, edge):

    d = [float("inf")] * n

    d[start] = 0

    q = [(0, start)]

    heapq.heapify(q)



    while q:

        ci, i = heapq.heappop(q)

        if d[i] < ci:

            continue



        for j in range(n):

            if j != start and j != i:

                if d[j] > d[i] + C[i][j]:

                    d[j] = d[i] + C[i][j]

                    heapq.heappush(q, (d[j], j))



    return d[goal]





cost = [0] * 10

for i in range(10):

    c = dijkstra(i, 1, 10, C)

    cost[i] = c



ans = 0

for h in range(H):

    for w in range(W):

        if A[h][w] == -1:

            continue

        ans += cost[A[h][w]]

print(ans)

import sys



sys.setrecursionlimit(10 ** 6)

INF = float("inf")

MOD = 10 ** 9 + 7





def input():

    return sys.stdin.readline().strip()





def main():

    H, W = list(map(int, input().split()))

    C = []

    for _ in range(10):

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

        C.append(c)



    A = []

    for _ in range(H):

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

        A.append(a)



    dp = [[INF] * 10 for _ in range(10)]

    for i in range(10):

        for j in range(10):

            dp[i][j] = C[i][j]



    for k in range(10):

        for i in range(10):

            for j in range(10):

                tmp = dp[i][k] + dp[k][j]

                if tmp < dp[i][j]:

                    dp[i][j] = tmp



    ans = 0

    for h in range(H):

        for w in range(W):

            if A[h][w] == -1:

                continue

            else:

                ans += dp[A[h][w]][1]



    print(ans)





if __name__ == "__main__":

    main()

import heapq

H, W = list(map(int, input().split()))
C = []
for _ in range(10):
    c = list(map(int, input().split()))
    C.append(c)
A = []
for _ in range(H):
    a = list(map(int, input().split()))
    A.append(a)


def dijkstra(start, goal, n, edge):
    d = [float("inf")] * n
    d[start] = 0
    q = [(0, start)]
    heapq.heapify(q)
    while q:
        ci, i = heapq.heappop(q)
        if d[i] < ci:
            continue
        for j in range(n):
            if j != start and j != i:
                if d[j] > d[i] + C[i][j]:
                    d[j] = d[i] + C[i][j]
                    heapq.heappush(q, (d[j], j))
    return d[goal]


cost = [0] * 10
for i in range(10):
    c = dijkstra(i, 1, 10, C)
    cost[i] = c
ans = 0
for h in range(H):
    for w in range(W):
        if A[h][w] == -1:
            continue
        ans += cost[A[h][w]]
print(ans)

import sys

sys.setrecursionlimit(10**6)
INF = float("inf")
MOD = 10**9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    H, W = list(map(int, input().split()))
    C = []
    for _ in range(10):
        c = list(map(int, input().split()))
        C.append(c)
    A = []
    for _ in range(H):
        a = list(map(int, input().split()))
        A.append(a)
    dp = [[INF] * 10 for _ in range(10)]
    for i in range(10):
        for j in range(10):
            dp[i][j] = C[i][j]
    for k in range(10):
        for i in range(10):
            for j in range(10):
                tmp = dp[i][k] + dp[k][j]
                if tmp < dp[i][j]:
                    dp[i][j] = tmp
    ans = 0
    for h in range(H):
        for w in range(W):
            if A[h][w] == -1:
                continue
            else:
                ans += dp[A[h][w]][1]
    print(ans)


if __name__ == "__main__":
    main()

-import heapq +import sys -H, W = list(map(int, input().split())) -C = [] -for _ in range(10): -    c = list(map(int, input().split())) -    C.append(c) -A = [] -for _ in range(H): -    a = list(map(int, input().split())) -    A.append(a) +sys.setrecursionlimit(10**6) +INF = float("inf") +MOD = 10**9 + 7 -def dijkstra(start, goal, n, edge): -    d = [float("inf")] * n -    d[start] = 0 -    q = [(0, start)] -    heapq.heapify(q) -    while q: -        ci, i = heapq.heappop(q) -        if d[i] < ci: -            continue -        for j in range(n): -            if j != start and j != i: -                if d[j] > d[i] + C[i][j]: -                    d[j] = d[i] + C[i][j] -                    heapq.heappush(q, (d[j], j)) -    return d[goal] +def input(): +    return sys.stdin.readline().strip() -cost = [0] * 10 -for i in range(10): -    c = dijkstra(i, 1, 10, C) -    cost[i] = c -ans = 0 -for h in range(H): -    for w in range(W): -        if A[h][w] == -1: -            continue -        ans += cost[A[h][w]] -print(ans) +def main(): +    H, W = list(map(int, input().split())) +    C = [] +    for _ in range(10): +        c = list(map(int, input().split())) +        C.append(c) +    A = [] +    for _ in range(H): +        a = list(map(int, input().split())) +        A.append(a) +    dp = [[INF] * 10 for _ in range(10)] +    for i in range(10): +        for j in range(10): +            dp[i][j] = C[i][j] +    for k in range(10): +        for i in range(10): +            for j in range(10): +                tmp = dp[i][k] + dp[k][j] +                if tmp < dp[i][j]: +                    dp[i][j] = tmp +    ans = 0 +    for h in range(H): +        for w in range(W): +            if A[h][w] == -1: +                continue +            else: +                ans += dp[A[h][w]][1] +    print(ans) + + +if __name__ == "__main__": +    main()