import sys

input = sys.stdin.readline



def floyd_warshall(dist):

    v = len(dist)

    for k in range(v):

        for i in range(v):

            for j in range(v):

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



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

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

floyd_warshall(c)

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

ans = 0

for row in A:

    for a in row:

        if a != -1:

            ans += c[a][1]

print(ans)

import sys

input = sys.stdin.readline



def floyd_warshall(dist):

    v = len(dist)

    for k in range(v):

        for i in range(v):

            for j in range(v):

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



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

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

floyd_warshall(c)

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

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 sys

input = sys.stdin.readline


def floyd_warshall(dist):
    v = len(dist)
    for k in range(v):
        for i in range(v):
            for j in range(v):
                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])


H, W = list(map(int, input().split()))
c = [list(map(int, input().split())) for _ in range(10)]
floyd_warshall(c)
A = [list(map(int, input().split())) for _ in range(H)]
ans = 0
for row in A:
    for a in row:
        if a != -1:
            ans += c[a][1]
print(ans)

import sys

input = sys.stdin.readline


def floyd_warshall(dist):
    v = len(dist)
    for k in range(v):
        for i in range(v):
            for j in range(v):
                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])


H, W = list(map(int, input().split()))
c = [list(map(int, input().split())) for _ in range(10)]
floyd_warshall(c)
A = [list(map(int, input().split())) for _ in range(H)]
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)

-for row in A: -    for a in row: -        if a != -1: -            ans += c[a][1] +for i in range(H): +    for j in range(W): +        if A[i][j] != -1: +            ans += c[A[i][j]][1]