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

# Optimized solution
def main():
    H, W = list(map(int, input().split()))
    c = [list(map(int, input().split())) for _ in range(10)]
    a = [list(map(int, 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])

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

    print(ans)


if __name__ == '__main__':
    main()

# import sys
#
# sys.setrecursionlimit(10 ** 7)
#
# input = sys.stdin.readline
# rstrip()
# int(input())
# map(int, input().split())
# Original solution
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
mod = 1000000007

#A
def A():
    return

#B
def B():
    return

#C
def C():
    a,b,c,d = list(map(int, S()))
    s = ["-","+"]
    for i in range(2):
        for j in range(2):
            for k in range(2):
                if eval(str(a)+s[i]+str(b)+s[j]+str(c)+s[k]+str(d)) == 7:
                    print((str(a)+s[i]+str(b)+s[j]+str(c)+s[k]+str(d)+"=7"))
                    quit()
#D
def D():
    h,w = LI()
    d = [None for i in range(10)]
    for i in range(10):
        d[i] = LI()
    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):
        s = LI()
        for k in s:
            if k != -1:
                ans += d[k][1]
    print(ans)
#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#Solve
if __name__ == "__main__":
    D()


# Optimized solution
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from itertools import permutations
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    h,w = LI()
    d = LIR(10)
    for k in range(10):
        for i in range(10):
            for j in range(10):
                nd = d[i][k]+d[k][j]
                if nd < d[i][j]:
                    d[i][j] = nd
    a = LIR(h)
    ans = 0
    for i in a:
        for j in i:
            if j < 0:
                continue
            ans += d[j][1]
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()
# Original solution
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from itertools import permutations
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    h,w = LI()
    d = LIR(10)
    for k in range(10):
        for i in range(10):
            for j in range(10):
                nd = d[i][k]+d[k][j]
                if nd < d[i][j]:
                    d[i][j] = nd
    a = LIR(h)
    ans = 0
    for i in a:
        for j in i:
            if j < 0:
                continue
            ans += d[j][1]
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()


# Optimized solution
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.buffer.readline().split()]
def I(): return int(sys.stdin.buffer.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    h,w = LI()
    c = LIR(10)
    for k in range(10):
        for i in range(10):
            for j in range(10):
                nc = c[i][k]+c[k][j]
                if nc < c[i][j]:
                    c[i][j] = nc
    a = LIR(h)
    ans = 0
    for i in a:
        for j in i:
            if j < 0:
                continue
            ans += c[j][1]
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()
# Original solution
import sys

def main():
    input = sys.stdin.readline
    H, W = list(map(int, input().split()))
    C = [[int(c) for c in input().split()] for _ in range(10)]
    A = [list(map(int, input().split())) for _ in range(H)]

    dp = [[10**4] * 10 for _ in range(10)]
    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 a in A:
        for i in a:
            if i == -1:
                continue
            ans += C[i][1]

    print(ans)


if __name__ == '__main__':
    main()


# Optimized solution
import sys

def warshall_floyd(d):
    n = len(d)
    for i in range(n):
        for j in range(n):
            for k in range(n):
                d[j][k] = min(d[j][k], d[j][i] + d[i][k])
    return d


def main():
    input = sys.stdin.readline
    H, W = list(map(int, input().split()))
    C = [[int(c) for c in input().split()] for _ in range(10)]
    A = [list(map(int, input().split())) for _ in range(H)]

    d = warshall_floyd(C)

    ans = 0
    for a in A:
        for i in a:
            if i == -1:
                continue
            ans += d[i][1]

    print(ans)


if __name__ == '__main__':
    main()
# Original solution
H,W=list(map(int,input().split()))

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

for a in range(10):
    for b in range(10):
        for c in range(10):
            A[b][c]=min(A[b][c],A[b][a]+A[a][c])

ans=0
D=[]
for j in range(H):
    C=list(map(int,input().split()))
    D.append(C)
        
for k in range(H):
    for l in range(W):
        if D[k][l]!=(-1) and D[k][l]!=(1):
            ans+=A[D[k][l]][1]
print(ans)

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

for c in range(10):
    for a in range(10):
        for b in range(10):
            if list1[a][b]>list1[a][c]+list1[c][b]:
                list1[a][b]=list1[a][c]+list1[c][b]
                
list2=[0,0,0,0,0,0,0,0,0,0]       
list3=[]
for d in range(H):
    w=list(map(int,input().split()))
    list3.append(w)
    
for e in range(H):
    for f in range(W):
        if abs(list3[e][f])==1:
            pass
        else:
            list2[list3[e][f]]+=1
ans=0
for g in range(10):
    ans+=list2[g]*list1[g][1]
print(ans)
# Original solution
# -*- coding: utf-8 -*-
import sys
import heapq

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

#隣接リスト
c=[[] for i in range(10)]
for i in range(10):
	a=list(map(int,input().split()))
	for j in range(10):
		c[i].append(a[j])

#print c

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


def djk(n):
	#開始位置
	Start=n

	#初期化
	d=[-1 for i in range(10)]
	d[Start]=0


	#優先度付きキューに[距離,頂点の番号]を格納する
	#スタート地点の頂点1には距離0を入れる
	q=[]
	heapq.heappush(q, [0,Start])

	while len(q)!=0:
		#距離が最小の頂点を取り出し
		l,u=heapq.heappop(q)
		for i,val in enumerate(c[u]):
			v=i
			l_u_v=val
			#print v, l_u_v
			if (d[v]>d[u]+l_u_v) or d[v]==-1:
				d[v]=d[u]+l_u_v
				heapq.heappush(q, [d[v],v])
	return d[1]


sum=0
for i in range(H):
	for j in range(W):
		if A[i][j]==-1 or A[i][j]==1:
			continue
		else:
			sum+=djk(A[i][j])

print(sum)


# Optimized solution
# -*- coding: utf-8 -*-
import sys
import heapq

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

#隣接リスト
c=[[] for i in range(10)]
for i in range(10):
	a=list(map(int,input().split()))
	for j in range(10):
		c[i].append(a[j])

#print c

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


def djk(n):
	#開始位置
	Start=n

	#初期化
	d=[-1 for i in range(10)]
	d[Start]=0


	#優先度付きキューに[距離,頂点の番号]を格納する
	#スタート地点の頂点1には距離0を入れる
	q=[]
	heapq.heappush(q, [0,Start])

	while len(q)!=0:
		#距離が最小の頂点を取り出し
		l,u=heapq.heappop(q)
		for i,val in enumerate(c[u]):
			v=i
			l_u_v=val
			#print v, l_u_v
			if (d[v]>d[u]+l_u_v) or d[v]==-1:
				d[v]=d[u]+l_u_v
				heapq.heappush(q, [d[v],v])
	return d[1]


sum=0
temp=[0 for i in range(10)]
for i in range(H):
	for j in range(W):
		if A[i][j]==-1 or A[i][j]==1:
			continue
		else:
			if temp[A[i][j]]==0:
				temp[A[i][j]]=djk(A[i][j])
			sum+=temp[A[i][j]]

print(sum)
# Original solution
import heapq

def dijkstra(s):
    hq = [(0, s)]
    heapq.heapify(hq) # リストを優先度付きキューに変換
    cost = [float('inf')] * 10 # 行ったことのないところはinf
    cost[s] = 0 # 開始地点は0
    while hq:
        c, v = heapq.heappop(hq)
        if c > cost[v]: # コストが現在のコストよりも高ければスルー
            continue
        for d, u in e[v]:
            tmp = d + cost[v]
            if tmp < cost[u]:
                cost[u] = tmp
                heapq.heappush(hq, (tmp, u))
    return cost

h,w = list(map(int,input().split()))
e = [[] for _ in range(10)]
for i in range(10):
    alist = list(map(int,input().split()))
    for j in range(10):
        if i != j:
            e[i].append((alist[j], j))


l = []
for i in range(h):
    for a in list(map(int,input().split())):
        if a != -1:
            l.append(a)

ans = 0
for i in l:
    ans += dijkstra(i)[1]
print(ans)

# Optimized solution
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)
# Original solution
import copy

def main():
    H,W = list(map(int, input().split()))
    c = [[int(i) for i in input().split()] for i in range(10)]
    a = [[int(i) for i in input().split()] for i in range(H)]
    #d = copy.deepcopy(c)

    # ワーシャルフロイド法
    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])
    
    ans = 0
    for i in a:
        for j in i:
            if j > 1 or j == 0:
                ans += c[j][1]
    
    print(ans)


if __name__ == "__main__":
    main()


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

def main():
    H,W = list(map(int, readline().split()))
    C = [[int(i) for i in readline().split()] for _ in range(10)]
    A = [[int(i) for i in readline().split()] for _ in range(H)]

    # 頂点kを経由する場合としない場合を比較
    for k in range(10):
        for i in range(10):
            for j in range(10):
                C[i][j] = min(C[i][k] + C[k][j], C[i][j])
    
    ans = 0
    for a in A:
        for i in a:
            if i != -1 and i != 1:
                ans += C[i][1]

    print(ans)


if __name__ == "__main__":
    main()
# Original solution
INFTY = 10**4
H,W = list(map(int,input().split()))
C = [list(map(int,input().split())) for _ in range(10)]
A = [list(map(int,input().split())) for _ in range(H)]

d = [[INFTY for _ in range(10)] for _ in range(10)]
for i in range(10):
    for j in range(10):
        d[i][j] = C[i][j]
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])

cnt = 0
for i in range(H):
    for j in range(W):
        if A[i][j]>-1:
            cnt += d[A[i][j]][1]
print(cnt)

# Optimized solution
INFTY = 10**5
H,W = list(map(int,input().split()))
C = [list(map(int,input().split())) for _ in range(10)]
A = [list(map(int,input().split())) for _ in range(H)]
dist = [[INFTY for _ in range(10)] for _ in range(10)]
for i in range(10):
    dist[i][i] = 0
for i in range(10):
    for j in range(10):
        dist[i][j] = C[i][j]
for k in range(10):
    for i in range(10):
        for j in range(10):
            dist[i][j] = min(dist[i][j],dist[i][k]+dist[k][j])
cnt = 0
for i in range(H):
    for j in range(W):
        a = A[i][j]
        if a!=-1:
            cnt += dist[a][1]
print(cnt)
# Original solution
INFTY = 10**4+10
H,W = list(map(int,input().split()))
C = [list(map(int,input().split())) for _ in range(10)]
cost = [[INFTY for _ in range(10)] for _ in range(10)]
for i in range(10):
    for j in range(10):
        cost[i][j] = C[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])
A = [list(map(int,input().split())) for _ in range(H)]
tot = 0
for i in range(H):
    for j in range(W):
        if A[i][j]==1 or A[i][j]==-1:continue
        tot += cost[A[i][j]][1]
print(tot)

# Optimized solution
H,W = list(map(int,input().split()))
C = [list(map(int,input().split())) for _ in range(10)]
A = [list(map(int,input().split())) for _ in range(H)]
dist = [[0 for _ in range(10)] for _ in range(10)]
for i in range(10):
    for j in range(10):
        dist[i][j] = C[i][j]
for k in range(10):
    for i in range(10):
        for j in range(10):
            dist[i][j] = min(dist[i][j],dist[i][k]+dist[k][j])
cnt = 0
for i in range(H):
    for j in range(W):
        if A[i][j]>=0:
            cnt += dist[A[i][j]][1]
print(cnt)
# Original solution
from heapq import heappush, heappop


def dijkstra(s):
    '''
    始点sから各頂点への最短距離を求める
    '''
    d = [float("inf")] * 10
    d[s] = 0
    used = [False] * 10
    used[s] = True
    edgelist = []

    for e in edge[s]:
        heappush(edgelist, e)

    while edgelist:
        cost, v = heappop(edgelist)
        if used[v]:
            continue
        d[v] = cost
        used[v] = True
        for e in edge[v]:
            if not used[e[1]]:
                heappush(edgelist, [e[0] + d[v], e[1]])

    return d


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

edge = [[] for _ in range(10)]
for i in range(10):
    for j in range(10):
        edge[i].append([c[i][j], j])
        edge[j].append([c[j][i], i]) 

dist = []
for i in range(10):
    dist.append(dijkstra(i))

res = 0
for i in range(h):
    for j in range(w):
        if a[i][j] >= 0:
            res += dist[a[i][j]][1]

print(res)

# Optimized solution
def warshall_floyd():
    '''
    すべての頂点間の最短距離を求める
    '''
    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])


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

warshall_floyd()

res = 0
for i in range(h):
    for j in range(w):
        if a[i][j] >= 0:
            res += c[a[i][j]][1]

print(res)
# Original solution
inf = 1000
h,w = list(map(int,input().split()))

dp = []
for i in range(10):
    c = list(map(int,input().split()))
    dp.append(c)

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

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

for i in range(h):
    for j in range(w):
        if A[i][j] not in [-1,1]:
            ans += dp[A[i][j]][1]

print(ans)

# Optimized solution
h,w = list(map(int,input().split()))

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

def warshall_floyd(d):
    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])
    return d

trans_map = warshall_floyd(C)

ans = 0
for i in range(h):
    line = list(map(int,input().split()))
    for j in range(w):
        num = line[j]
        if num == -1:
            continue
        ans += trans_map[num][1]

print(ans)
# Original solution
h,w = list(map(int,input().split()))
c = [[int(i) for i in input().split()] for _ in range(10)]
a = [[int(i) for i 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])

ans = 0
for i in range(h):
    for j in range(w):
        if abs(a[i][j]) != 1:
            ans += c[a[i][j]][1]

print(ans)

# Optimized solution
h,w = list(map(int,input().split()))
c = [[int(i) for i in input().split()] for _ in range(10)]
a = [[int(i) for i 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])

ans = 0
mp = [c[i][1] for i in range(10)]
mp.append(0)
for i in range(h):
    for j in range(w): ans += mp[a[i][j]]
print(ans)
# Original solution
import heapq
def dijkstra_heap(s,C):
  #始点sから各頂点への最短距離
  d = [float("inf")] * 10
  used = [False] * 10
  d[s] = 0
  used[s] = True
  edgelist = []
  #edgelist : (sからの暫定(未確定)最短距離,頂点)のリスト
  #edge[s] : sから出る枝の(重み,終点)の配列
  for i,w in enumerate(C[s]):
    heapq.heappush(edgelist,[w,i]) #sの隣の点は枝の重さがそのまま暫定最短距離となる
  while len(edgelist):
    #まだ使われてない頂点の中から最小の距離のものを探す→確定させる
    minedge = heapq.heappop(edgelist)
    #minedge : (sからの(確定)最短距離,頂点)
    v = minedge[1]
    if used[v]:
      continue
    d[v] = minedge[0]
    used[v] = True
    for i,w in enumerate(C[v]):
      if not used[i]:
        heapq.heappush(edgelist,[d[v]+w,i])
  return d

H, W = list(map(int, input().split()))
C = [[] for _ in range(10)]
for i in range(10):
  c = list(map(int, input().split()))
  for j in range(10):
    C[j].append(c[j])
d = dijkstra_heap(1,C)
#print(d)
ans = 0
for i in range(H):
  A = list(map(int, input().split()))
  for a in A:
    if a>=0:
      ans += d[a]
print(ans)

# Optimized solution
#Warshall Floyd
H, W = list(map(int, input().split()))

def solve():
    d = []
    for i in range(10): #ひとまず枝があるペアは枝の長さをセット
        d.append(list(map(int, input().split())))

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

    #三重ループ
    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):
        for j in range(W):
            if A[i][j]!=-1:
                ans += d[A[i][j]][1]
    return ans
print((solve()))
# Original solution
import heapq

H, W = list(map(int, input().split()))
graph = []
wall = []

for _ in range(10):
  graph.append(list(map(int, input().split())))

for _ in range(H):
  wall += list(map(int, input().split()))

def dijkstra(s):
  INF = 10**5
  hq = [(0, s)]
  heapq.heapify(hq)
  cost = [INF] * 10
  cost[s] = 0
  while hq:
    c, v = heapq.heappop(hq)
    if c > cost[v]:
      continue
    for u, d in enumerate(graph[v]):
      tmp = d + cost[v]
      if tmp < cost[u]:
        cost[u] = tmp
        heapq.heappush(hq, (tmp, u))
  return cost[1]

mincost = [0]*10
for i in range(10):
  mincost[i] = dijkstra(i)

ans = 0
for x in wall:
  if x == -1 or x == 1:
    continue
  else:
    ans += mincost[x]

print(ans)

# Optimized solution
H, W = list(map(int, input().split()))
C = []
wall = []

for _ in range(10):
  C.append(list(map(int, input().split())))

for _ in range(H):
  wall += list(map(int, input().split()))

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])

ans = 0
for x in wall:
  if x == -1:
    continue
  else:
    ans += C[x][1]

print(ans)
# Original solution
h,w = list(map(int,input().split()))
l = [list(map(int,input().split())) for i in range(10)]
a = [list(map(int,input().split())) for i in range(h)]
ans = 0
for rep in range(10):
    for i in range(10):
        for j in range(10):
            for k in range(10):
                if l[i][j] > l[i][k]+l[k][j]:
                    l[i][j] = l[i][k]+l[k][j]
num = [i[1] for i in l] + [0]
for i in a:
    for j in i:
        ans += num[j]
print(ans)

# Optimized solution
h,w = list(map(int,input().split()))
l = [list(map(int,input().split())) for i in range(10)]
a = [list(map(int,input().split())) for i in range(h)]
ans = 0
for k in range(10):
    for i in range(10):
        for j in range(10):
            if l[i][j] > l[i][k]+l[k][j]:
                l[i][j] = l[i][k]+l[k][j]
num = [i[1] for i in l] + [0]
for i in a:
    for j in i:
        ans += num[j]
print(ans)
# Original solution
magialist=[]
datalist=[]
h,w=list(map(int,input().split()))
for i in range(10):
    add=list(map(int,input().split()))
    magialist.append(add)

for j in range(h):
    add1=list(map(int,input().split()))
    datalist.extend(add1)
 
storelist=[[[[] for i in range(10)] for i in range(10)] for i in range(10)]
    #わーシャルフロイド法
for i in range(10):
    for j in range(10):
            a=min(magialist[i][j],magialist[i][0]+magialist[0][j])
            storelist[0][i][j]=a
k=1
while k<=9:
    for i in range(10):
        for j in range(10):
                a=min(storelist[k-1][i][j],storelist[k-1][i][k]+storelist[k-1][k][j])
                storelist[k][i][j]=a
    k+=1

uselist=[]
                                                                                        
for j in range(10):
    uselist.append(storelist[9][j][1])
ans=0
for nums in range(h*w):
        if datalist[nums]==-1:
            continue
        else:
            for j in range(10):
                if datalist[nums]==j:
                    ans+=uselist[j]
print(ans)

# Optimized solution
h,w=list(map(int,input().split()))
d=[list(map(int,input().split())) for _ in range(10)]
e=[list(map(int,input().split())) for _ in range(h)]
def warshall_floyd(d,n):
    #d[i][j]=i番目の頂点からj番目の頂点までの最短距離
    for k in range(n+1):
        for i in range(n+1):
            for j in range(n+1):
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return d
d=warshall_floyd(d,9)
ans=0
for i in range(h):
    for j in range(w):
        if e[i][j]>=0:
            ans+=d[e[i][j]][1]
print(ans)