Joisino the magical girl has decided to turn every single digit that exists on this world into 1.  Rewriting a digit i with j (0≤i,j≤9) costs c_{i,j} MP (Magic Points).  She is now standing before a wall. The wall is divided into HW squares in H rows and W columns, and at least one square contains a digit between 0 and 9 (inclusive).  You are given A_{i,j} that describes the square at the i-th row from the top and j-th column from the left, as follows:    * If A_{i,j}≠-1, the square contains a digit A_{i,j}.   * If A_{i,j}=-1, the square does not contain a digit.  Find the minimum total amount of MP required to turn every digit on this wall into 1 in the end.
H, W = map(int, input().split())
c = [ list(map(int, input().split())) for _ in range(10)]

for k in range(10):
    for i in range(10):
        for j in range(10):
            c1 = c[i][j]
            c2 = c[i][k] + c[k][j]
            if c2 > c1:
                c[i][j] = c2
ans = 0
for _ in range(H):
    for a in map(int, input().split()):
        if a != -1:
            ans += c[a][1]

print(ans)
from collections import defaultdict
import heapq
INF = 1e9

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

n = 10
adj_matrix = []

for i in range(n):
    c = list(map(int, input().split()))
    adj_matrix.append(c)

Adj_list = defaultdict(set)

for i in range(n):
    for j in range(n):
        Adj_list[i].add((j, adj_matrix[i][j]))


def dijkstra(x, vertex):
    d = [INF] * x
    d[vertex] = 0
    hq = [(0, vertex)]
    
    while hq:
        u = heapq.heappop(hq)[1]
        for (v, c) in Adj_list[u]:
            if d[v] > d[u] + c:
                d[v] = d[u] + c
                heapq.heappush(hq, (d[u]+c, v))

    return d

ans = INF
t_dis = 0

table = {}
for i in range(n):
    table[i] = dijkstra(n, i)


t_dis = 0

for i in range(H):
    l = list(map(int, input().split()))
    for j in range(W):
        if l[j] != -1:
            t_dis += table[l[j]][1]
            
print(t_dis)