CodeEff/ECCO huggingface dataset:
# Original solution
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)


# Optimized solution
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()
# Original solution
H,W = list(map(int,input().split()))
C = []
for i in range(10):
  c = list(map(int,input().split()))
  C.append(c)

A = []
for i in range(H):
  a =  list(map(int,input().split()))
  A.append(a)

#最短距離 (V:頂点数,E:辺数)
#BFS : コスト1 O(E)
#Dijkstra : コスト任意 O(ElogE)
#Floyd Warshall O(V^3) →　今回 V=10 → OK

#「「「Floyd Warshall」」」
#→全点間の重み付きグラフ　経路最小化
#点j→点kに行くまでの最小コスト経路(j→k or j→i→k)
#===========================================================
#iを動かして
#「j→i」のコスト　と　「j→i→kのコスト」　の小さいほうを d[j][k]に採用
#ということを繰り返す
#→直感的には、これでは「2点間コスト」or「3点間コスト」の小さいほうを選んでいるだけっぽいで、
#4点間コスト,5点間コスト,...が最小の場合を考慮していないのでは...
#→実は大丈夫
#===========================================================
#・上の例で,右の経由コストがd[j][k]のコストに採用された場合は(i=i0として)
#「j→i0→k」のコスト と　「j→i0→i1→kのコスト」　の小さいほうを d[j][k]に採用
#これをiを動かして繰り返してコストの最小値を求めるのでn点間最小値も網羅している！！

dist = C[:]
for i in range(10):
  for j in range(10):
    for k in range(10):
      dist[j][k] = min(dist[j][k],dist[j][i]+dist[i][k])

ans = 0
for i in range(H):
  for j in range(W):
   if(A[i][j] >= 0): #0以上
     ans += dist[A[i][j]][1]
print(ans)


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

for i in range(10):
  for j in range(10):
    for k in range(10):
      C[j][k] = min(C[j][k],C[j][i]+C[i][k])

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)
# Original solution
def rec_cost(x,acc,plist,clist):
  if x == 1:
    return acc
  else:
    minimum = -1
    for next in plist:
      nextplist = plist.copy()
      nextplist.remove(next)
      candidate = rec_cost(next,acc+clist[x][next],nextplist,clist)
      if minimum == -1:
        minimum = candidate
      elif candidate < minimum:
        minimum = candidate
    return minimum

def cost(n,clist):
  plist = [0,1,2,3,4,5,6,7,8,9]
  plist.remove(n)
  return rec_cost(n,0,plist,clist)

def joisino(a,clist):
  note = [-1,0,-1,-1,-1,-1,-1,-1,-1,-1]
  answer = 0
  for raw in a:
    for number in raw:
      if number != -1:
        if note[number] == -1:
          note[number] = cost(number,clist)
        answer += note[number]
      
  return answer

def i_o():
  h, w = map(int,input().split(" "))
  i = 0 
  clist = []
  while i < 10:
    raw = list(map(int,input().split(" ")))
    clist.append(raw)
    i += 1
  a = []
  i = 1
  while i <= h:
    raw = list(map(int,input().split(" ")))
    a.append(raw)
    i += 1
  return h,w,clist,a

if __name__ == '__main__':
  h,w,clist,a = i_o()
  ans =joisino(a,clist)
  
  print(ans, flush=True)


# Optimized solution
def cost(n,clist):
  k = 0
  while k < 10:
    i = 0
    while i < 10:
      j = 0
      while j < 10:
        clist[i][j] = min(clist[i][j],clist[i][k]+clist[k][j])
        j += 1
      i += 1
    k += 1
  return clist[n][1]

def joisino(a,clist):
  note = [-1,0,-1,-1,-1,-1,-1,-1,-1,-1]
  answer = 0
  for raw in a:
    for number in raw:
      if number != -1:
        if note[number] == -1:
          note[number] = cost(number,clist)
        answer += note[number]
      
  return answer

def i_o():
  h, w = map(int,input().split(" "))
  i = 0 
  clist = []
  while i < 10:
    raw = list(map(int,input().split(" ")))
    clist.append(raw)
    i += 1
  a = []
  i = 1
  while i <= h:
    raw = list(map(int,input().split(" ")))
    a.append(raw)
    i += 1
  return h,w,clist,a

if __name__ == '__main__':
  h,w,clist,a = i_o()
  ans =joisino(a,clist)
  
  print(ans, flush=True)
# Original solution
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def abc079_d():
    inp = iter(map(int, read().split()))
    H, W = next(inp), next(inp)
    adj_mtx = [[next(inp) for _ in range(10)] for _ in range(10)]
    grid = [[next(inp) for _ in range(W)] for _ in range(H)]

    import heapq
    def dijkstra(start, W):
        ''' スタート地点から到達可能なすべての頂点への最短距離を返す
            Pythonで学ぶアルゴリズムとデータ構造 P.111 一部変更
        '''
        # 仮の最短距離をinfに設定
        inf = 10**9
        distance_list = [inf] * len(W)
        # スタートの頂点だけ距離を0にする
        distance_list[start] = 0
        # 最短距離が確定した点
        done_list = []
        # 次に処理する頂点を決めるためのヒープ
        wait_heap = []
        for i, d in enumerate(distance_list):
            # (スタートからの距離, 頂点) のタプル
            heapq.heappush(wait_heap, (d, i))
        # ヒープが空になるまで処理を続ける
        while wait_heap:
            p = heapq.heappop(wait_heap)
            i = p[1]
            if i in done_list:
                continue
            # この時点でスタートからiへの距離が確定する
            done_list.append(i)
            # iに隣接するすべての頂点に対する処理
            for j, x in enumerate(W[i]):
                if x > -1 and j not in done_list:
                    # 緩和
                    d = min(distance_list[j], distance_list[i] + x)
                    distance_list[j] = d
                    # jへの仮の最短距離をdとしてヒープに追加
                    heapq.heappush(wait_heap, (d, j))
        return distance_list

    # 各点から1への距離(コスト)
    dist_to_1 = [None] * 10
    for i in range(10):
        dist_to_1[i] = dijkstra(i, adj_mtx)[1]

    # グリッドの数字が-1と1以外はコスト発生するため足していく
    ans = 0
    for i in range(H):
        for j in range(W):
            if grid[i][j] == -1 or grid[i][j] == 1:
                continue
            ans += dist_to_1[grid[i][j]]
    print(ans)

abc079_d()

# Optimized solution
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def abc079_d():
    inp = iter(map(int, read().split()))
    H, W = next(inp), next(inp)
    adj_mtx = [[next(inp) for _ in range(10)] for _ in range(10)]
    grid = [[next(inp) for _ in range(W)] for _ in range(H)]

    def floyd_warshall(W):
        ''' ワーシャルフロイド法、全頂点間の最短距離 '''
        n = len(W)  # 頂点の数
        # DPで全頂点間の最短距離を求める
        for k in range(n):
            for u in range(n):
                for v in range(n):
                    W[u][v] = min(W[u][v], W[u][k] + W[k][v])
        return W

    min_dist = floyd_warshall(adj_mtx)

    # グリッドの数字が-1と1以外はコスト発生するため足していく
    ans = 0
    for i in range(H):
        for j in range(W):
            if grid[i][j] == -1 or grid[i][j] == 1:
                continue
            ans += min_dist[grid[i][j]][1]
    print(ans)

abc079_d()
# Original solution
def warshall_floyd(graph):
    num_v = len(graph)
    for k in range(num_v):
        for i in range(num_v):
            for j in range(num_v):
                graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j])
    return graph


h, w = list(map(int, input().split()))
route = []

for i in range(10):
    S = list(map(int, input().split()))
    route.append(S)
graph = warshall_floyd(route)
result = 0
for i in range(h):
    A = list(map(int, input().split()))
    for a in A:
        if a != -1:
            result += graph[a][1]
print(result)



# Optimized solution
def warshall_floyd(graph):
    num_v = len(graph)
    for k in range(num_v):
        for i in range(num_v):
            for j in range(num_v):
                graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j])
    return graph


H, W = list(map(int, input().split()))
items = []
for i in range(10):
    items.append(list(map(int, input().split())))

a_list = []
for i in range(H):
    a_list.append(tuple(map(int, input().split())))
graph = warshall_floyd(items)

ans = 0
for a in a_list:
    for item in a:
        if item == -1:
            continue
        ans += graph[item][1]
print(ans)
# Original solution
from collections import deque
h, w = [int(x) for x in input().split()]
c = [[int(x) for x in input().split()] for line in range(10)]
a = [[int(x) for x in input().split()] for line in range(h)]
for t in range(10):
    for i in range(10):
        for j in range(10):
            for k in range(10):
                c[i][j] = min(c[i][j], c[i][k] + c[k][j])
cost = 0
for line in range(h):
    for col in range(w):
        if a[line][col] in [-1, 1]:
            continue
        else:
            cost += c[a[line][col]][1]
print(cost)

# Optimized solution
h, w = [int(x) for x in input().split()]
c = [[int(x) for x in input().split()] for l in range(10)]
a = [[int(x) for x in input().split()] for l in range(h)]
for t in range(10):
    for i in range(10):
        for j in range(10):
            for k in range(10):
                c[i][j] = min(c[i][j], c[i][k] + c[k][j])
print(sum([c[a[x][y]][1] for x in range(h) for y in range(w) if a[x][y] != -1]))
# Original solution
from collections import defaultdict
from itertools import product

H, W = list(map(int, input().split()))
c = [[int(x) for x in input().split()] for _ in range(10)]
A = defaultdict(int)
for _ in range(H):
    for x in input().split():
        x = int(x)
        if x != -1 and x != 1:
            A[x] += 1

for _, i, j, k in product(list(range(10)), list(range(10)), list(range(10)), list(range(10))):
    if i == 1 or k == 1:
        continue
    c[i][j] = min(c[i][j], c[i][k] + c[k][j])

ans = 0
for k, v in list(A.items()):
    ans += c[k][1] * v
print(ans)

# Optimized solution
from collections import defaultdict
from itertools import product

H, W = list(map(int, input().split()))
c = [[int(x) for x in input().split()] for _ in range(10)]
A = defaultdict(int)
for _ in range(H):
    for x in input().split():
        x = int(x)
        if x != -1 and x != 1:
            A[x] += 1

for k, i, j in product(list(range(10)), list(range(10)), list(range(10))):
    c[i][j] = min(c[i][j], c[i][k] + c[k][j])

ans = 0
for k, v in list(A.items()):
    ans += c[k][1] * v
print(ans)
# Original solution
# h*wのマスにかかれているそれぞれの数字の位置は関係ないしどうでもいい
# それぞれの数がどれだけあるかに興味がある
# iから1に至る最小値を全探索 間に合うのか？ まあいけるやろ
h, w = list(map(int, input().split()))
table = []
for _ in range(10):
    table.append(list(map(int, input().split())))

wall = {}
for _ in range(h):
    x = list(map(int, input().split()))
    for v in x:
        if v not in wall:
            wall[v] = 1
        else:
            wall[v] += 1


def rec(idx, cost, used):
    if idx == 1:
        return cost
    ret = 10 ** 10
    for i in range(10):
        if i in used:
            continue
        next_used = used[:]
        next_used.append(i)
        ret = min(ret, rec(i, cost + table[idx][i], next_used))
    return ret


arr = [0 for _ in range(10)]
for i in range(10):
    if i == 1:
        continue
    arr[i] = rec(i, 0, [i])

ans = 0
for k, v in list(wall.items()):
    if k != -1 and k != 1:
        ans += arr[k] * v
print(ans)


# Optimized solution
# ワーシャルフロイドで解くバージョン
h, w = list(map(int, input().split()))
INF = 10 ** 10
table = [[INF] * 10 for _ in range(10)]

for i in range(10):
    column = list(map(int, input().split()))
    for j in range(10):
        if i == j:
            continue
        table[i][j] = column[j]

wall = {}
for _ in range(h):
    for a in list(map(int, input().split())):
        if a not in wall:
            wall[a] = 1
        else:
            wall[a] += 1

for k in range(10):
    for i in range(10):
        for j in range(10):
            table[i][j] = min(table[i][j], table[i][k] + table[k][j])

ans = 0
for k, v in list(wall.items()):
    if k != -1 and k != 1:
        ans += table[k][1] * v
print(ans)
# Original solution
#!/usr/bin/env python3
import sys


def solve(H: int, W: int, c: "List[List[int]]", A: "List[List[int]]"):
    #print(*c, sep='\n')
    for _ in range(10):
        for i in range(10):
            for j in range(10):
                for k in range(10):
                    c[i][j] = min(c[i][j], c[i][k]+c[k][j])
    #print(*c, sep='\n')
    cost = 0
    for a in A:
        for aa in a:
            if aa == -1:
                continue
            cost += c[aa][1]
    return cost


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    c = [[int(next(tokens)) for _ in range(9 - 0 + 1)] for _ in range(9 - 0 + 1)]  # type: "List[List[int]]"
    A = [[int(next(tokens)) for _ in range(W)] for _ in range(H)]  # type: "List[List[int]]"
    print((solve(H, W, c, A)))

def test():
    import doctest
    doctest.testmod()

if __name__ == '__main__':
    #test()
    main()


# Optimized solution
#!/usr/bin/env python3
import sys


def solve(H: int, W: int, c: "List[List[int]]", A: "List[List[int]]"):
    #print(*c, sep='\n')
    for k in range(10):
        for i in range(10):
            for j in range(10):
                c[i][j] = min(c[i][j], c[i][k]+c[k][j])
    #print(*c, sep='\n')
    cost = 0
    for a in A:
        for aa in a:
            if aa == -1:
                continue
            cost += c[aa][1]
    return cost


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    c = [[int(next(tokens)) for _ in range(9 - 0 + 1)] for _ in range(9 - 0 + 1)]  # type: "List[List[int]]"
    A = [[int(next(tokens)) for _ in range(W)] for _ in range(H)]  # type: "List[List[int]]"
    print((solve(H, W, c, A)))

def test():
    import doctest
    doctest.testmod()

if __name__ == '__main__':
    #test()
    main()
# Original solution
import heapq
H, W = list(map(int, input().split()))
edges = [[] for _ in range(10)]
for i in range(10):
    e_list = list(map(int, input().split()))
    for j in range(10):
        if j == i:
            continue
        edges[i].append([j, e_list[j]])
A = []
for i in range(H):
    A.append(list(map(int, input().split())))

def dijkstra(edges, num_v, start):
    dist = [float('inf')] * num_v
    dist[start] = 0
    q = []
    heapq.heappush(q, [0, start])

    while len(q) > 0:
        _, u = heapq.heappop(q)
        for edge in edges[u]:
            if dist[edge[0]] > dist[u] + edge[1]:
                dist[edge[0]] = dist[u] + edge[1]
                heapq.heappush(q, [dist[u] + edge[1], edge[0]])
    return dist
ans = 0
for a_line in A:
    for a in a_line:
        if a == -1 or a == 1:
            continue
        ans += dijkstra(edges, 10, a)[1]

print(ans)

# Optimized solution
import heapq
H, W = list(map(int, input().split()))
edges = [[] for _ in range(10)]
for i in range(10):
    e_list = list(map(int, input().split()))
    for j in range(10):
        if j == i:
            continue
        edges[i].append([j, e_list[j]])
A = []
for i in range(H):
    A.append(list(map(int, input().split())))

def dijkstra(edges, num_v, start):
    dist = [float('inf')] * num_v
    dist[start] = 0
    q = []
    heapq.heappush(q, [0, start])

    while len(q) > 0:
        _, u = heapq.heappop(q)
        for edge in edges[u]:
            if dist[edge[0]] > dist[u] + edge[1]:
                dist[edge[0]] = dist[u] + edge[1]
                heapq.heappush(q, [dist[u] + edge[1], edge[0]])
    return dist
ans = 0
root_list = [0] * 10
for i in range(10):
    if i == 1:
        continue
    root_list[i] = dijkstra(edges, 10, i)[1]
for a_line in A:
    for a in a_line:
        if a == -1 or a == 1:
            continue
        ans += root_list[a]

print(ans)
# Original solution
H,W=list(map(int,input().split()))
C=[]
for i in range(10):
    c=list(map(int,input().split()))
    C.append(c)
decide=set()
INF=10**9
cost=[INF]*10
def check(cost):
    for i in cost:
        if i==INF:
            return True
    return False
while(check(cost)):
    l=-1
    m=INF
    for j in range(10):
        for i in range(10):
            if i in decide:
                continue
            c=min(C[i][1],C[i][j]+cost[j])
            if c<m:
                m=c
                l=i
    cost[l]=m
    decide.add(l)
ans=0
for i in range(H):
    A=list(map(int,input().split()))
    for j in A:
        if j==-1:
            continue
        ans+=cost[j]
print(ans)


# Optimized solution
H,W=list(map(int,input().split()))
d=[]
for i in range(10):
  l=list(map(int,input().split()))
  d.append(l)
for k in range(10):
  for i in range(10):
    for j in range(10):
      d[i][j]=min(d[i][j],d[i][k]+d[k][j])
ans=0
for i in range(H):
  l=list(map(int,input().split()))
  for j in l:
    if j==-1:
      continue
    ans+=d[j][1]
print(ans)
# Original solution
# coding: utf-8
import queue
INF = 10000000

q = queue.PriorityQueue()

#初期化
G = []
kabe = []
H, W = list(map(int, input().split()))

#入力
for i in range(10):
    G.append(list(map(int, input().split())))

for i in range(H):
    kabe.append(list(map(int, input().split())))

lenList = [INF for i in range(10)]

def bfs():
    global G
    global q
    global lenList
    
    x = q.get()
    #行ったことあるものは捨てる。
    while lenList[x[1]] != INF:
        return
    
    #間違いなく最短なので更新
    lenList[x[1]] = x[0]

    #行ったことないところをプッシュ
    for i in range(10):
        if lenList[i] == INF:
            q.put([x[0] + G[i][x[1]], i])

#初期値を入力して発火
q.put([0, 1])
while not(q.empty()):
    bfs()

sum = 0

for i in range(H):
    for j in range(W):
        if kabe[i][j] != -1:
            sum += lenList[kabe[i][j]]

print(sum)

# Optimized solution
# coding: utf-8
import queue
INF = 10000000

#初期化
G = []
kabe = []
H, W = list(map(int, input().split()))

#入力
for i in range(10):
    G.append(list(map(int, input().split())))

for i in range(H):
    kabe.append(list(map(int, input().split())))

lenList = [INF for i in range(10)]

q = queue.PriorityQueue()

def dijkstra():
    global G
    global q
    global lenList
    
    x = q.get()
    #行ったことあるものは捨てる。
    if lenList[x[1]] != INF:
        return
    
    #間違いなく最短なので更新
    lenList[x[1]] = x[0]

    #行ったことないところをプッシュ
    for i in range(10):
        if lenList[i] == INF:
            q.put([x[0] + G[i][x[1]], i])

#初期値を入力して発火
q.put([0, 1])# [距離,現在地]
while not(q.empty()):
    dijkstra()

sum = 0

for i in range(H):
    for j in range(W):
        if kabe[i][j] != -1:
            sum += lenList[kabe[i][j]]

print(sum)
# Original solution
import sys
input = sys.stdin.readline
from collections import Counter
from itertools import chain

def readlines(n):
    for _ in range(n):
        yield list(map(int, input().split()))

def main():
    h, w = list(map(int, input().split()))

    C = [[int(c) for c in input().split()] for _ in range(10)]

    for k in range(10):
        for i in range(10):
            for j in range(10):
                C[i][j] = min(C[i][j], C[i][k] + C[k][j])

    counter = Counter(chain(*readlines(h)))

    for i in range(10):
        yield C[i][1] * counter[i]


print((sum(main())))


main()

# Optimized solution
import sys
input = sys.stdin.readline
def readlines(n):
    for _ in range(n):
        yield list(map(int, input().split()))

def main():
    h, w = list(map(int, input().split()))

    C = list(readlines(10))

    for k in range(10):
        for i in range(10):
            for j in range(10):
                C[i][j] = min(C[i][j], C[i][k] + C[k][j])

    for line in readlines(h):
        yield sum(C[c][1] for c in line if c != -1) 


print((sum(main())))


main()
# Original solution
import math
st=input().split()
H=int(st[0]);W=int(st[1])
c=[[0 for i in range(10)] for i in range(10)]
A=[[0 for i in range(W)]for i in range(H)]
for i in range(10):
    st=input().split()
    for j in range(10):
        c[i][j]=int(st[j])
for i in range(H):
    st=input().split()
    for j in range(W):
        A[i][j]=int(st[j])
def Dijkstra(s):
    d=[10000]*10
    d[s]=0
    Q=set(range(10))
    V=set(range(10))
    while(len(Q)>0):
        u=list(Q)[0]
        for i in Q:
            if d[i]<d[u]:
                u=i
        Q-={u}
        for v in V-{u}:
            if (d[v]>d[u]+c[u][v]):
                d[v]=d[u]+c[u][v]
    return d[1]
result=0
DD=[0]*10
for i in range(10):
    DD[i]=Dijkstra(i)
for i in range(H):
    for j in range(W):
        if A[i][j]!=-1:
            result+=DD[A[i][j]]
print(result)


# Optimized solution
H,W=list(map(int,input().split()))
c=[list(map(int,input().split())) for i in range(10)]
A=[list(map(int,input().split())) for i in range(H)]
for k in range(10):
  for i in range(10):
    for j in range(10):
      if c[i][j]>c[i][k]+c[k][j]:
        c[i][j]=c[i][k]+c[k][j]
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)
# Original solution
def warshall_floyd(cost):
    # cost[i][j] : iからjへの最短距離
    for k in range(10):
        for i in range(10):
            for j in range(10):
                cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j])
    return cost


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

cost = warshall_floyd(cost)

ans = 0
for r in range(h):
    for c in range(w):
        frm = a[r][c]
        if frm == -1:
            continue
        ans += cost[frm][1]
print(ans)


# Optimized solution
def main():
    h, w = list(map(int, input().split()))
    c = [[int(x) for x in input().split()] for _ in range(10)]
    a = [[int(x) for x in input().split()] for _ in range(h)]

    for k in range(10):
        for i in range(10):
            for j in range(10):
                c[i][j] = min(c[i][j], c[i][k] + c[k][j])

    ret = 0
    for row in a:
        for cell in row:
            if ~cell:
                ret += c[cell][1]

    print(ret)


if __name__ == '__main__':
    main()