CodeEff/ECCO huggingface dataset:
# Original solution
from sys import stdin
import math
import bisect
from collections import defaultdict

def IL():return list(map(int, stdin.readline().split()))
def ItoSList(): return list(map(int,list(stdin.readline())))
def IL_reverse():return list(map(int, stdin.readline().split())).reverse()
def Int_all():return list(map(int, stdin))
def sumIL():return sum(list(map(int, stdin.readline().split())))
def SL():return list(map(int, stdin.readline().split()))
def Str_all():return list(map(int, stdin))
def tDL(p,q):return [[0 for i in range(p)] for j in range(q)]


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

N=10
res = float('inf')
dist = [[float("inf")] * N for i in range(N)]
def main():
	H,W = IL()
	for i in range(N):
		dist[i][i] = 0
	
	G = [[] for i in range(N)]
 
	for i in range(10):
	    l = IL()
	    for j in range(10):
	        dist[i][j] = l[j]
	floyd_warshall()

	ans = 0
	memo={}
	for i in range(H):
	    for i in IL():
	        if abs(i) == 1:
	          pass
	        elif i in memo:
	          ans += memo[i]
	        else:
	            t = dist[i][1] 
	            memo[i] = t
	            ans += t
	print(ans)

if __name__ == "__main__": main()


# Optimized solution
from heapq import heappush, heappop
def dijkstra(adjList, s):
    num = len(adjList)
    dist = [float("INF") for i in range(10)]
    queue = []
    heappush(queue,(0,s))
    dist[s] = 0
    
    while queue != []:
        v_cost, v = heappop(queue)
        if dist[v] < v_cost:
            continue
        for u_cost, u in adjList[v]:
            if u != v and u_cost + dist[v] < dist[u]:
                dist[u] = u_cost + dist[v]
                heappush(queue, (dist[u], u))
    return dist

 
 
from sys import stdin
def IL():return list(map(int, stdin.readline().split()))


h,w = IL()
G = [[] for i in range(10)]
def main(): 
    for i in range(10):
        l = IL()
        for j in range(10):
            G[i].append((l[j],j))
     
     
    ans = 0
    memo={}
    for i in range(h):
        for i in IL():
            if abs(i) == 1:
              pass
            elif i in memo:
              ans += memo[i]
            else:
                t = dijkstra(G, i)[1] 
                memo[i] = t
                ans += t
    print(ans)

if __name__ == "__main__":
    main()
# Original solution
import heapq
h, w = list(map(int, input().split()))
graph = [list(map(int, input().split())) for _ in range(10)]

trans_graph = list(zip(*graph))
next_nodes = []
for idx, d in enumerate(trans_graph[1]):
    next_nodes.append((d, idx))

heapq.heapify(next_nodes)

INF = 10**9
decided = [0]*10
decided[1] = 1
dist = [INF]*10
dist[1] = 0
while 0 in decided:
    distance, v = heapq.heappop(next_nodes)
    if decided[v]:
        continue
    decided[v] = 1
    dist[v] = distance
    for v2, x in enumerate(trans_graph[v]):
        if decided[v2]:
            continue
        heapq.heappush(next_nodes, (dist[v]+x, v2))

ans = 0
for _ in range(h):
    for x in map(int, input().split()):
        if x == -1:
            continue
        ans += dist[x]

print(ans)

# Optimized solution
import heapq
h, w = list(map(int, input().split()))
graph = [list(map(int, input().split())) for _ in range(10)]

trans_graph = list(zip(*graph))
next_nodes = []
for idx, d in enumerate(trans_graph[1]):
    next_nodes.append((d, idx))
heapq.heapify(next_nodes)

INF = 10**9
decided = [0]*10
decided[1] = 1
dist = [INF]*10
dist[1] = 0

while next_nodes:
    distance, v = heapq.heappop(next_nodes)
    if decided[v]:
        continue
    decided[v] = 1
    dist[v] = distance
    for v2, x in enumerate(trans_graph[v]):
        if decided[v2]:
            continue
        heapq.heappush(next_nodes, (dist[v]+x, v2))

ans = 0
for _ in range(h):
    for x in map(int, input().split()):
        if x == -1:
            continue
        ans += dist[x]

print(ans)
# Original solution
# coding: utf-8

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 = [float('inf')] * 10

def bellman_ford(s):
    d[s] = 0
    for i in range(10):
        for j in range(10):
            for k in range(10):
                if d[j] != float('inf') and d[k] > d[j] + c[k][j]:
                    d[k] = d[j] + c[k][j]

bellman_ford(1)
ans = 0

for i in A:
    for j in i:
        if j != -1:
            ans += d[j]

print(ans)
# print sum(sum(d[a] if a != -1 else 0 for a in aa) for aa in A)


# Optimized solution
# coding: utf-8

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 = [float('inf')] * 10

d[1] = 0

for i in range(10):
    for j in range(10):
        for k in range(10):
            if d[j] != float('inf') and d[k] > d[j] + c[k][j]:
                d[k] = d[j] + c[k][j]

print(sum(sum(d[a] if a != -1 else 0 for a in aa) for aa in A))
# Original solution
import copy
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)]
# cost = [[-1]*10 for i in range(10)]
# for j in range(10):
#     cost[j] = [[c[i][j], i] for i in range(10)]
cost = [-1]*10
cost[1] = 0

dp = [[-1]*10 for i in range(10)]
for i in range(10):
    dp[i][i] = 0


def search_cost(i, j, reached):
    # if dp[i][j] != -1:
    #     return dp[i][j]
    tmp_list = copy.copy(reached)
    tmp_list.append(i)
    ret = c[i][j]

    for k in range(10):
        if k == i or k == 1 or k in reached:
            continue
        else:
            tmp = c[i][k] + search_cost(k, j, tmp_list)
            ret = min(tmp, ret)
            if ret <= 2:
                break
            #print("i:"+str(i) + "k:" + str(k) + "j:"+str(j) + "=" + str(tmp))

    # dp[i][j] = ret
    # for i in range(10):
    #     print(dp[i])
    return ret


for i in range(10):
    if i == 1:
        continue
    else:
        if c[i][1] <= 2:
            dp[i][1] = c[i][1]
            cost[i] = c[i][1]
        else:
            cost[i] = search_cost(i, 1, [])
ans = 0
for i in range(H):
    for j in range(W):
        if A[i][j] != -1:
            ans += cost[A[i][j]]
print(ans)


# Optimized solution
import copy
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)]
# cost = [[-1]*10 for i in range(10)]
# for j in range(10):
#     cost[j] = [[c[i][j], i] for i in range(10)]
cost = [-1]*10
cost[1] = 0

dp = [[-1]*10 for i in range(10)]
for i in range(10):
    dp[i][i] = 0


def search_cost(i, j, reached):
    # if dp[i][j] != -1:
    #     return dp[i][j]
    if cost[i] != -1:
        return cost[i]
    tmp_list = copy.copy(reached)
    tmp_list.append(i)
    ret = c[i][j]

    for k in range(10):
        if k == i or k == 1 or k in reached:
            continue
        else:
            tmp = c[i][k] + search_cost(k, j, tmp_list)
            ret = min(tmp, ret)
            if ret <= 2:
                break
            #print("i:"+str(i) + "k:" + str(k) + "j:"+str(j) + "=" + str(tmp))

    # dp[i][j] = ret
    # for i in range(10):
    #     print(dp[i])
    return ret


for i in range(10):
    if i == 1:
        continue
    else:
        if c[i][1] <= 2:
            dp[i][1] = c[i][1]
            cost[i] = c[i][1]
        else:
            cost[i] = search_cost(i, 1, [])
ans = 0
for i in range(H):
    for j in range(W):
        if A[i][j] != -1:
            ans += cost[A[i][j]]
print(ans)
# Original solution
h,w = list(map(int,input().split()))
d = [list(map(int,input().split())) for i in range(10)]
for t in range(10):
	for i in range(10):
		for j in range(10):
			for k in range(10):
				d[i][j] = min(d[i][j], d[i][k] + d[k][j])
arr = [list(map(int,input().split())) for i in range(h)]

print(sum(sum(d[a][1] if a>=0 else 0 for a in aa) for aa in arr))

# Optimized solution
h,w = list(map(int,input().split()))
d = [list(map(int,input().split())) for i in range(10)]

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

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

print(sum(sum(d[a][1] if a>=0 else 0 for a in aa) for aa in arr))
# Original solution
def calc_mp(C, candidates, n, ans):
    for i in range(len(candidates)):
        candidates2 = [c for c in candidates]
        c = candidates[i]
        del candidates2[i]
        a = C[n][c]
        if c != 1:
            a += calc_mp(C, candidates2, c, ans)
        ans = min(a, ans)
    return ans

def main():
    H, W = list(map(int, input().split()))
    C = [0]*10
    for i in range(10):
        C[i] = list(map(int, input().split()))
    A = [0]*H
    for i in range(H):
        A[i] = list(map(int, input().split()))
    # print(H, W, C, A)

    MPs = [0]*10
    for i in range(10):
        if i != 1:
            candidates = [j for j in range(10)]
            del(candidates[i])
            MPs[i] = calc_mp(C, candidates, i, 1000)
    # print(MPs)
    ans = 0
    for h in range(H):
        for w in range(W):
            a = A[h][w]
            if a != -1:
                ans += MPs[a]
    print(ans)

if __name__ == '__main__':
    main()


# Optimized solution
def calc_mp(C, d):
    for k in range(10):
        for i in range(10):
            for j in range(10):
                if k == 0:
                    d[i][j][k] = min(C[i][j], C[i][k] + C[k][j])
                else:
                    d[i][j][k] = min(d[i][j][k-1], d[i][k][k-1] + d[k][j][k-1])

def main():
    H, W = list(map(int, input().split()))
    C = [0]*10
    for i in range(10):
        C[i] = list(map(int, input().split()))
    A = [0]*H
    for i in range(H):
        A[i] = list(map(int, input().split()))
    # print(H, W, C, A)

    d = [[[0 for i in range(10)] for i in range(10)] for i in range(10)]
    calc_mp(C, d)

    # print(d)
    ans = 0
    for h in range(H):
        for w in range(W):
            a = A[h][w]
            if a != -1 and a != 1:
                ans += d[a][1][9]
    print(ans)

if __name__ == '__main__':
    main()
# Original solution
h, w = list(map(int, input().split()))
d = [list(map(int, input().split())) for _ in range(10)]
A = [list(map(int, input().split())) for _ in range(h)]

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

d = warshall_floyd(d) 
ans = 0 
for i in range(h):
    for j in range(w):
        if A[i][j] != 1 and A[i][j] != -1:
            ans += d[A[i][j]][1]
print(ans)

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

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

d = warshall_floyd(c)
ans = 0
for i in range(H):
    for j in range(W):
        if A[i][j] != -1 and A[i][j] != 1:
            ans += d[A[i][j]][1]
print(ans)
# Original solution
#!/usr/bin/env python3

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 i in range(H):
    for j in range(W):
        if A[i][j] != -1:
            ans += c[A[i][j]][1]

print(ans)


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

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 = sum([c[a][1] for a in itertools.chain.from_iterable(A) if a >= 0])
print(ans)
# Original 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)]

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 A[i][j] != -1:
            ans += C[A[i][j]][1]

print(ans)

# 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):
            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 A[i][j] != -1:
            ans += C[A[i][j]][1]

print(ans)
# Original solution
# coding: utf-8
# Your code here!
H,W = [int(i) for i in 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)]


dist = c
n=10
#n: 頂点数
for k in range(n):
    for i in range(n):
        for j in range(n):
            dist[i][j] = min(dist[i][j], dist[i][k]+ dist[k][j])


ans = 0
for i in range(H):
    for j in range(W):
        if A[i][j] != -1:
            ans += dist[A[i][j]][1]
print(ans)     



# Optimized solution
# coding: utf-8
# Your code here!
H,W = [int(i) for i in 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)]


dist = c
n=10
#n: 頂点数
for k,dk in enumerate(dist):
    for di in dist:
        for j in range(n):
            if di[j] > di[k]+ dk[j]:
                di[j] = di[k]+ dk[j]


ans = 0
for i in range(H):
    for j in range(W):
        if A[i][j] != -1:
            ans += dist[A[i][j]][1]
print(ans)
# Original solution
import collections
import heapq

h, w = list(map(int, input().split()))
cs = [list(map(int, input().split())) for _ in range(10)]
counter = collections.Counter()
for _ in range(h):
    for x in map(int, input().split()):
        if x != -1:
            counter[x] += 1

dists = [float('inf') for i in range(10)]
dists[1] = 0
q = []
for i in range(10):
    heapq.heappush(q, (dists[i], i))

while q:
    d0, i = heapq.heappop(q)
    for j in range(10):
        if j == i:
            continue
        d = d0 + cs[j][i]
        if dists[j] > d:
            dists[j] = d
            heapq.heappush(q, (dists[j], j))

res = 0
for k, v in list(counter.items()):
    res += dists[k] * v
print(res)

# Optimized solution
import collections

h, w = list(map(int, input().split()))
cs = [list(map(int, input().split())) for _ in range(10)]
counter = collections.Counter()
for _ in range(h):
    for x in map(int, input().split()):
        if x != -1:
            counter[x] += 1

for k in range(10):
    for i in range(10):
        for j in range(10):
            d = cs[i][k] + cs[k][j]
            if cs[i][j] > d:
                cs[i][j] = d

res = 0
for k, v in list(counter.items()):
    res += cs[k][1] * v
print(res)
# Original solution
h,w=list(map(int,input().split()))
tra=[list(map(int,input().split())) for i in range(10)]
ll=[[tra[i][j] for j in range(10)] for i in range(10)]
for i in range(10**3-1):
    for i in range(10):
        for j in range(10):
            if i!=j:
                ll[i][j]=min(ll[i][j],min(ll[i][o]+tra[o][j] for o in range(10) if o!=i and o!=j))
ans=0
for i in range(h):
    for j in map(int,input().split()):
        if j==-1 or j==1:continue
        ans+=ll[j][1]
print(ans)

# Optimized solution
class WarshallFloyd:
    #O(V^3)で任意２頂点の最短距離
    def __init__(self,n,_first_index=0):
        self.v = n
        self._first_idx=_first_index
        self.d = [[float("INF")]*(n) for _ in range(n)]
        for i in range(n):
            self.d[i][i] = 0

    def path(self,x,y,c):
        if x == y:
            return False
        f=self._first_idx
        self.d[x-f][y-f] = c
        return True

    def build(self):
        for k in range(self.v):
            for i in range(self.v):
                for j in range(self.v):
                    self.d[i][j] = min(self.d[i][j], self.d[i][k] + self.d[k][j])
        return self.d
h,w=list(map(int,input().split()))
war=WarshallFloyd(10)
for i in range(10):
    s=list(map(int,input().split()))

    for j in range(10):
        war.path(i,j,s[j])
dis=war.build()
from collections import Counter as co
l=[list(map(int,input().split())) for i in range(h)]
l=sum(l,[])
var=list(co(l).items())
print((sum([0]+[v*dis[k][1] for k,v in var if k!=-1])))
# Original 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 j 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])
cost=[C[i][1] for i in range(10)]
cnt=0
for i in range(h):
    for j in range(w):
        add=A[i][j]
        if add!=-1:
            cnt +=cost[add]
print(cnt)

# 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 j in range(h)]
#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])

cost=[C[i][1] for i in range(10)]+[0]
cnt=0
for i in range(h):
    for j in range(w):
        cnt +=cost[A[i][j]]
print(cnt)
# Original solution
import sys
input = sys.stdin.readline

INF = float("inf")


def f(c, pos, unused, sum_c):
    if pos == 1:
        return sum_c
    ret = INF
    l = unused[:]
    l.remove(pos)
    for next_n in l:
        res = f(c, next_n, l, sum_c + c[pos][next_n])
        if res < ret:
            ret = res
    return ret


def main():
    H, W = list(map(int, input().split()))
    c = [None] * 10
    for i in range(10):
        c[i] = list(map(int, input().split()))
    A = [None] * H
    for i in range(H):
        A[i] = list(map(int, input().split()))

    c_min = [0] * 10
    for i in range(10):
        c_min[i] = f(c, i, list(range(10)), 0)

    ans = 0
    for h in range(H):
        for w in range(W):
            if A[h][w] != -1:
                ans += c_min[A[h][w]]
    print(ans)


if __name__ == "__main__":
    main()


# Optimized solution
import sys
input = sys.stdin.readline

INF = float("inf")


def warshall_floyd(n, d):
    for k in range(n):
        for i in range(n):
            d_ik = d[i][k]
            if d_ik == INF:
                continue
            for j in range(n):
                if d[i][j] > d_ik + d[k][j]:
                    d[i][j] = d_ik + d[k][j]


def main():
    H, W = list(map(int, input().split()))
    c = [None] * 10
    for i in range(10):
        c[i] = list(map(int, input().split()))
    A = [None] * H
    for i in range(H):
        A[i] = list(map(int, input().split()))

    warshall_floyd(10, c)

    ans = 0
    for h in range(H):
        for w in range(W):
            if A[h][w] != -1:
                ans += c[A[h][w]][1]
    print(ans)


if __name__ == "__main__":
    main()
# Original solution
import sys
input = sys.stdin.readline

INF = float("inf")


def warshall_floyd(n, d):
    for k in range(n):
        for i in range(n):
            d_ik = d[i][k]
            if d_ik == INF:
                continue
            for j in range(n):
                if d[i][j] > d_ik + d[k][j]:
                    d[i][j] = d_ik + d[k][j]


def main():
    H, W = list(map(int, input().split()))
    c = [None] * 10
    for i in range(10):
        c[i] = list(map(int, input().split()))
    A = [None] * H
    for i in range(H):
        A[i] = list(map(int, input().split()))

    warshall_floyd(10, c)

    ans = 0
    for h in range(H):
        for w in range(W):
            if A[h][w] != -1:
                ans += c[A[h][w]][1]
    print(ans)


if __name__ == "__main__":
    main()


# Optimized solution
import sys
input = sys.stdin.readline

INF = float("inf")


def warshall_floyd(n, d):
    for k in range(n):
        for i in range(n):
            d_ik = d[i][k]
            # if d_ik == INF:
            #     continue
            for j in range(n):
                if d[i][j] > d_ik + d[k][j]:
                    d[i][j] = d_ik + d[k][j]


def main():
    H, W = list(map(int, input().split()))
    c = [None] * 10
    for i in range(10):
        c[i] = list(map(int, input().split()))
    A = [None] * H
    for i in range(H):
        A[i] = list(map(int, input().split()))

    warshall_floyd(10, c)

    ans = 0
    for h in range(H):
        for w in range(W):
            if A[h][w] != -1:
                ans += c[A[h][w]][1]
    print(ans)


if __name__ == "__main__":
    main()
# Original solution
H,W=list(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):
			c[i][j] = min(c[i][j], c[i][k] + c[k][j])

ans = 0
for _ in range(H):
	for x in input().split():
		x = int(x)
		if x != -1:
			ans += c[x][1]
print(ans)


# Optimized solution
H,W=list(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):
			c[i][j] = min(c[i][j], c[i][k] + c[k][j])

ans = 0
for _ in range(H):
	ans += sum([c[x][1] for x in map(int,input().split()) if x != -1])

print(ans)
# Original solution
H,W=list(map(int,input().split()))

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

f=True

while f:
    f=False
    for i in range(10):
        for j in range(10):
            temp=[]
            for k in range(10):
                if co[i][k]<co[i][j] and co[k][j]<co[i][j]:
                    temp.append(co[i][k]+co[k][j])
            if len(temp) >= 1:
                if co[i][j] > min(temp):
                    co[i][j] = min(temp)
                    f = True




for i in range(10):
    c[i]=co[i][1]


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

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

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


for k in range(10):
    for i in range(10):
        for j in range(10):
            temp=[]
            if co[i][j] > co[i][k] + co[k][j]:
                co[i][j] = co[i][k] + co[k][j]

for i in range(10):
    c[i]=co[i][1]


ans=0
for i in range(H):
    A=list(map(int,input().split()))
    for j in range(W):
        if A[j]!=-1 and A[j]!=1:
            ans+=c[A[j]]
print(ans)
# Original solution
def d_wall(H, W, C, A):
    cost=C
    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])
    ans = 0
    for i in range(H):
        for j in range(W):
            if A[i][j] != -1:
                ans += cost[A[i][j]][1]
    return ans

H,W = [int(i) for i in input().split()]
C = [[int(i) for i in input().split()] for j in range(10)]
A = [[int(i) for i in input().split()] for j in range(H)]
print((d_wall(H, W, C, A)))

# Optimized solution
def d_wall(H, W, C, A):
    # cost[i][j]には最終的に数字iからjに移行するための最小コストが格納される
    cost = [x[:] for x in C]

    # ワーシャルフロイド法で最小コストを計算
    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])
    ans = 0
    # 壁の各数字ごとにコストの値を参照、和を取る
    for i in range(H):
        for j in range(W):
            if A[i][j] != -1:
                ans += cost[A[i][j]][1]  # 壁の数字から1へ移行するための最小コスト
    return ans

H,W = [int(i) for i in input().split()]
C = [[int(i) for i in input().split()] for j in range(10)]
A = [[int(i) for i in input().split()] for j in range(H)]
print((d_wall(H, W, C, A)))