import sys

from heapq import heappush, heappop

stdin = sys.stdin



 

sys.setrecursionlimit(10**5) 

 

def li(): return list(map(int, stdin.readline().split()))

def li_(): return [int(x)-1 for x in stdin.readline().split()]

def lf(): return list(map(float, stdin.readline().split()))

def ls(): return stdin.readline().split()

def ns(): return stdin.readline().rstrip()

def lc(): return list(ns())

def ni(): return int(stdin.readline())

def nf(): return float(stdin.readline())





def dijkstra(graph:list, node:int, start:int) -> list:

    # 未探索のノードは距離INF

    INF = float("inf")

    dist = [INF]*node

    

    # 始点ノードの距離を0とし、dfsのためのpriority queを作成

    dist[start] = 0

    heap = [(0,start)]

    

    # 未探索のノードをpriority queueに入れる

    while heap:

        cost, cur_node = heappop(heap)

        

        for nex_cost, nex_node in graph[cur_node]:

            dist_cand = dist[cur_node] + nex_cost

            if dist_cand < dist[nex_node]:

                dist[nex_node] = dist_cand

                heappush(heap, (dist[nex_node], nex_node))

    

    return dist



h,w = li()

graph = [[] for _ in range(10)]



for i in range(10):

    d = list(li())

    for j,dj in enumerate(d):

        graph[i].append((dj,j))

        

a = [list(li()) for _ in range(h)]



dist = [0]*10

for i in range(10):

    dist[i] = dijkstra(graph,10,i)[1]



ans = 0

for row in range(h):

    for col in range(w):

        if a[row][col] != -1:

            ans += dist[a[row][col]]

            

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

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

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



nums = 10



for k in range(nums):

  for i in range(nums):

    for j in range(nums):

      cost[i][j] = min(cost[i][j],

                       cost[i][k] + cost[k][j])



ans = 0

for wi in wall:

  for wij in wi:

    ans += 0 if wij == -1 else cost[wij][1]

    

print(ans)
import sys
from heapq import heappush, heappop

stdin = sys.stdin
sys.setrecursionlimit(10**5)


def li():
    return list(map(int, stdin.readline().split()))


def li_():
    return [int(x) - 1 for x in stdin.readline().split()]


def lf():
    return list(map(float, stdin.readline().split()))


def ls():
    return stdin.readline().split()


def ns():
    return stdin.readline().rstrip()


def lc():
    return list(ns())


def ni():
    return int(stdin.readline())


def nf():
    return float(stdin.readline())


def dijkstra(graph: list, node: int, start: int) -> list:
    # 未探索のノードは距離INF
    INF = float("inf")
    dist = [INF] * node
    # 始点ノードの距離を0とし、dfsのためのpriority queを作成
    dist[start] = 0
    heap = [(0, start)]
    # 未探索のノードをpriority queueに入れる
    while heap:
        cost, cur_node = heappop(heap)
        for nex_cost, nex_node in graph[cur_node]:
            dist_cand = dist[cur_node] + nex_cost
            if dist_cand < dist[nex_node]:
                dist[nex_node] = dist_cand
                heappush(heap, (dist[nex_node], nex_node))
    return dist


h, w = li()
graph = [[] for _ in range(10)]
for i in range(10):
    d = list(li())
    for j, dj in enumerate(d):
        graph[i].append((dj, j))
a = [list(li()) for _ in range(h)]
dist = [0] * 10
for i in range(10):
    dist[i] = dijkstra(graph, 10, i)[1]
ans = 0
for row in range(h):
    for col in range(w):
        if a[row][col] != -1:
            ans += dist[a[row][col]]
print(ans)

h, w = list(map(int, input().split()))
cost = [list(map(int, input().split())) for _ in range(10)]
wall = [list(map(int, input().split())) for _ in range(h)]
nums = 10
for k in range(nums):
    for i in range(nums):
        for j in range(nums):
            cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j])
ans = 0
for wi in wall:
    for wij in wi:
        ans += 0 if wij == -1 else cost[wij][1]
print(ans)

-import sys -from heapq import heappush, heappop - -stdin = sys.stdin -sys.setrecursionlimit(10**5) - - -def li(): -    return list(map(int, stdin.readline().split())) - - -def li_(): -    return [int(x) - 1 for x in stdin.readline().split()] - - -def lf(): -    return list(map(float, stdin.readline().split())) - - -def ls(): -    return stdin.readline().split() - - -def ns(): -    return stdin.readline().rstrip() - - -def lc(): -    return list(ns()) - - -def ni(): -    return int(stdin.readline()) - - -def nf(): -    return float(stdin.readline()) - - -def dijkstra(graph: list, node: int, start: int) -> list: -    # 未探索のノードは距離INF -    INF = float("inf") -    dist = [INF] * node -    # 始点ノードの距離を0とし、dfsのためのpriority queを作成 -    dist[start] = 0 -    heap = [(0, start)] -    # 未探索のノードをpriority queueに入れる -    while heap: -        cost, cur_node = heappop(heap) -        for nex_cost, nex_node in graph[cur_node]: -            dist_cand = dist[cur_node] + nex_cost -            if dist_cand < dist[nex_node]: -                dist[nex_node] = dist_cand -                heappush(heap, (dist[nex_node], nex_node)) -    return dist - - -h, w = li() -graph = [[] for _ in range(10)] -for i in range(10): -    d = list(li()) -    for j, dj in enumerate(d): -        graph[i].append((dj, j)) -a = [list(li()) for _ in range(h)] -dist = [0] * 10 -for i in range(10): -    dist[i] = dijkstra(graph, 10, i)[1] +h, w = list(map(int, input().split())) +cost = [list(map(int, input().split())) for _ in range(10)] +wall = [list(map(int, input().split())) for _ in range(h)] +nums = 10 +for k in range(nums): +    for i in range(nums): +        for j in range(nums): +            cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]) -for row in range(h): -    for col in range(w): -        if a[row][col] != -1: -            ans += dist[a[row][col]] +for wi in wall: +    for wij in wi: +        ans += 0 if wij == -1 else cost[wij][1]