CodeEff/ECCO huggingface dataset:
# Original solution
import sys
input = sys.stdin.readline

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 row in A:
    for a in row:
        if a != -1:
            ans += c[a][1]
print(ans)


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

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 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)]
ans = 0

def bfs(x):
    if x[-1]==1:
        global s
        buf = 0
        for i in range(len(x)-1):
            buf += c[x[i]][x[i+1]]
        s = min(s,buf)
    else:
        for i in range(10):
            if not i in x:
                bfs(x+[i])

for i in range(h):
    for j in range(w):
        if not a[i][j] in [1,-1]:
            s = 10**9
            bfs([a[i][j]])
            ans += s
print(ans)

# Optimized solution
import itertools
import collections
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)]
a = list(itertools.chain.from_iterable(a))
b = collections.Counter(a)
ans = 0

def bfs(x):
    if x[-1]==1:
        global s
        buf = 0
        for i in range(len(x)-1):
            buf += c[x[i]][x[i+1]]
        s = min(s,buf)
    else:
        for i in range(10):
            if not i in x:
                bfs(x+[i])

for i in b:
    if not i in [1,-1]:
        s = 10**9
        bfs([i])
        ans += s*b[i]
print(ans)
# Original solution
import itertools
import collections
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)]
a = list(itertools.chain.from_iterable(a))
b = collections.Counter(a)
ans = 0

def bfs(x):
    if x[-1]==1:
        global s
        buf = 0
        for i in range(len(x)-1):
            buf += c[x[i]][x[i+1]]
        s = min(s,buf)
    else:
        for i in range(10):
            if not i in x:
                bfs(x+[i])

for i in b:
    if not i in [1,-1]:
        s = 10**9
        bfs([i])
        ans += s*b[i]
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)]
n = 10
for k in range(n):
    for i in range(n):
        for j in range(n):
            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: ans += c[j][1]
print(ans)
# Original solution
H,W = list(map(int,input().split()))
c = [list(map(int,input().split())) for i in range(10)]
import queue

def dist(i):
    res = c[i][1]
    que = queue.Queue()
    path = []
    for j in range(10):
        if j != i and j != 1:
            path.append(j)
    for j in range(10):
        if j != 1 and j != i and c[i][j] < res:
            jpath = path.copy()
            jpath.pop(jpath.index(j))
            que.put((j,c[i][j],jpath))
    while que.qsize():
        j,cost,path = que.get()
        if cost + c[j][1] < res:
            res = cost + c[j][1]
        for k in path:
            if k != 1 and cost+c[j][k] < res:
                kpath = path.copy()
                kpath.pop(kpath.index(k))
                que.put((k,cost+c[j][k],kpath))
    return res

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

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

# Optimized solution
H,W = list(map(int,input().split()))
c = [list(map(int,input().split())) for i in range(10)]
import queue



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

cost = []
for i in range(10):
    cost.append(d[i][1])

ans = 0
for i in range(H):
    A = list(map(int,input().split()))
    for j in range(W):
        if A[j] != -1:
            ans += cost[A[j]]
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)]

def dfs(nv,vl,cost):
    if nv==1:
        return cost
    vl_ = vl.copy()
    vl_.append(nv)
    ret = []
    for i in range(10):
        if not i in vl_:
            ret.append(dfs(i,vl_,cost+c[nv][i]))
    return min(ret)
cost = []
for i in range(10):
    cost.append(dfs(i,[],0))
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
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)]
memo = {}
def dfs(nv,vl,cost):
    if nv==1:
        return cost
    if nv in list(memo.keys()):
        return cost+memo[nv]
    vl_ = vl.copy()
    vl_.append(nv)
    ret = []
    for i in range(10):
        if not i in vl_:
            ret.append(dfs(i,vl_,cost+c[nv][i]))
    return min(ret)
cost = []
for i in range(10):
    memo[i] = dfs(i,[],0)
ans = 0
for i in range(h):
    for j in range(w):
        if a[i][j]!=-1:
            ans += memo[a[i][j]]
print(ans)
# Original solution
H,W=list(map(int,input().split()))
S=[list(map(int, input().split())) for i in range(10)]
l=[None]*10
for i in range(10):
    l[i]=S[i][1]
for i in range(8):
    for j in range(10):
        for k in range(10):
            if l[j]>l[k]+S[j][k]:
                l[j]=l[k]+S[j][k]
l.append(0)
a=0
for i in range(H):
    L=list(map(int,input().split()))
    for j in L:
        a+=l[j]
print(a)

# Optimized solution
H,W=list(map(int,input().split()))
S=[list(map(int, input().split())) for i in range(10)]
l=[None]*10
for i in range(10):
    l[i]=S[i][1]
for i in range(7):
    for j in range(10):
        for k in range(10):
            if l[j]>l[k]+S[j][k]:
                l[j]=l[k]+S[j][k]
l.append(0)
a=0
for i in range(H):
    L=list(map(int,input().split()))
    for j in L:
        a+=l[j]
print(a)
# 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 i in range(H)]

INF=10**10
tmp=[[INF]*10 for i in range(10)]
for k in range(10):
    for i in range(10):
        for j in range(10):
            tmp[i][j]=min(c[i][k]+c[k][j] for k in range(10))
            c[i][j]=min(tmp[i][j],c[i][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)]

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

count=[0]*10
for i in range(H):
    l=list(map(int,input().split()))
    for j in range(10):
        count[j]+=l.count(j)
ans=0
for i in range(10):
    ans+=count[i]*c[i][1]

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

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

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):
    a=list(map(int,input().strip().split(' ')))
    for j in a:
        if j!=-1:
            ans+=cost[j][1]
print(ans)




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

def w_f():
    cost=[]
    for i in range(10):
        cost.append(list(map(int,input().strip().split(' '))))

    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
    
d=w_f()
ans=0
for i in range(H):
    a=list(map(int,input().strip().split(' ')))
    for j in a:
        if j!=-1:
            ans+=d[j][1]
print(ans)
# Original solution
H, W = list(map(int, input().split()))
c = [[0]*10 for i in range(10)]
for i in range(10):
    a = list(map(int, input().split()))
    for j in range(10):
        c[i][j] = a[j]

A = [[0]*W for i in range(H)]

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

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

# Optimized solution
H, W = list(map(int, input().split()))
c = [[0]*10 for i in range(10)]

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

A = [[0]*W for i in range(H)]

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

for k in range(10):
    for i in range(10):
        for j in range(10):
                if i != j:
                    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 and A[i][j] != 1:
            ans += c[A[i][j]][1]
            
print(ans)
# Original solution
def dijkstra(graph,src):
    if graph is None:
        return None
    nodes = [i for i in range(len(graph))]
    visited=[]
    if src in nodes:
        visited.append(src)
        nodes.remove(src)
    else:
        return None
    distance={src:0}
    for i in nodes:
        distance[i]=int(graph[src][i])
    path={src:{src:[]}}
    k=pre=src
    while nodes:
        mid_distance=float('inf')
        for v in visited:
            for d in nodes:
                new_distance = int(graph[src][v])+int(graph[v][d])
                if new_distance < mid_distance:
                    mid_distance=new_distance
                    graph[src][d]=str(new_distance)
                    k=d
                    pre=v
        distance[k]=mid_distance
        path[src][k]=[i for i in path[src][pre]]
        path[src][k].append(k)
        visited.append(k)
        nodes.remove(k)
    return distance

if __name__=="__main__":
    sum = 0
    s = input().split(" ")
    x, y = s[0], s[1]
    c = [n for n in range(0,10)]
    d = []
    matrix =[i for i in range(0,int(x))]*int(y)
    c[0] = list(input().split(" "))
    c[1] = list(input().split(" "))
    c[2] = list(input().split(" "))
    c[3] = list(input().split(" "))
    c[4] = list(input().split(" "))
    c[5] = list(input().split(" "))
    c[6] = list(input().split(" "))
    c[7] = list(input().split(" "))
    c[8] = list(input().split(" "))
    c[9] = list(input().split(" "))
    for m in range(0,10):
        d.append(dijkstra(c,m))
    for i in range(0,int(x)):
        matrix[i] = input().split(" ")
        for j in range(0,int(y)):
            n = int(matrix[i][j])
            if n==-1:
                continue
            else:
                sum = sum + int(d[n][1])
    print(sum)





# Optimized solution
if __name__=="__main__":
    sum = 0
    x,y = list(map(int,input().split()))
    c = [list(map(int,input().split())) for n in range(10)]
    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]

    m = [list(map(int,input().split())) for n in range(int(x))]
    for i in range(x):
        for j in range(y):
            if m[i][j]!=-1:
                sum+=int(c[m[i][j]][1])

    print(sum)
# Original solution
h,w=list(map(int,input().split()))
b=[list(map(int,input().split()))for _ in range(10)]
for i in range(10):
  for j in range(10):
    for k in range(10):b[j][k]=min(b[j][k],b[j][i]+b[i][k])
c=[b[i][1]for i in range(10)]+[0]
a=0
for i in range(h):
  for j in input().split():a+=c[int(j)]
print(a)

# Optimized solution
h,w=list(map(int,input().split()))
c=[list(map(int,input().split()))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])
co=[c[i][1] for i in range(10)]
ans=0
for _ in range(h):
	for i in list(map(int,input().split())):
		if i!=-1:ans+=co[i]
print(ans)
# Original solution
from itertools import permutations
h,w=list(map(int,input().split()))
C=[list(map(int,input().split())) for i in range(10)]

cost=[C[i][1] for i in range(10)]
for i in range(10):
    if i!=1:
        keiyu=[x for x in range(10) if x!=1 and x!=i]
        for j in range(9):
            perm=permutations(keiyu,r=j)
            for k in perm:
                k=[i]+list(k)+[1]
                tmp=0
                for l in range(len(k)-1):
                    tmp+=C[k[l]][k[l+1]]
                cost[i]=min(tmp, cost[i])

from collections import defaultdict
count=defaultdict(int)
for i in range(h):
    A=list(map(int,input().split()))
    for a in A:
        count[a]+=1

ans=0
t=[x for x in range(10) if x!=1]
for i in t:
    ans+=cost[i]*count[i]
print(ans)

# Optimized solution
from itertools import permutations
h,w=list(map(int,input().split()))
es=[[] for i in range(10)]

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

n=10
import heapq

def dijkstra_heap(s):
    d = [float("inf")] * n
    used = [True] * n
    d[s] = 0
    used[s] = False
    edgelist = []
    for e in es[s]:
        heapq.heappush(edgelist,e)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        if not used[minedge[1]]:
            continue
        v = minedge[1]
        d[v] = minedge[0]
        used[v] = False
        for e in es[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,[e[0]+d[v],e[1]])
    return d

cost=[dijkstra_heap(i)[1] for i in range(10)]

from collections import defaultdict
count=defaultdict(int)
for i in range(h):
    A=list(map(int,input().split()))
    for a in A:
        count[a]+=1

ans=0
t=[x for x in range(10) if x!=1]
for i in t:
    ans+=cost[i]*count[i]
print(ans)
# Original solution
import collections
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)]

result = [0]*10
    
    
def way(n,e,i,p): #現在の数字、現在の必要魔力、始点、暫定最低魔力
    for j in range(0,10):
        if (n == j):
            continue
        if (n == 1):
            if (e < result[i]):
                result[i] = e
            break
        if (e +  c[n][j] < p):
            q = e + c[n][j]
            way(j,q,i,p)
        
for i in range(0,10):
    result[i] = c[i][1]
    way(i,0,i,result[i])
    

r = []
for w in range(0,10):
    t = 0
    for k in range(0,h):
        l = collections.Counter(a[k])
        t += l[w]
    r.append(t)

ans = 0
for z in range(0,10):
    ans += result[z] * r[z]
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)]

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
def minindex(arrived,costs):
    mindex = "end"
    curmin = 100000000
    for i in range(10):
        if arrived[i]:continue
        if curmin>costs[i]:
            mindex = i
            curmin = costs[i]
    return mindex

H,W = list(map(int,input().split()))
C = [[int(j) for j in input().split()] for i in range(10)]
#C[i][j]はiをjにするコスト
#のつもりなんだけど、ダイクストラ法の逆打ち計算では逆をやります

arrived = [False] * 10
arrived[1] = True
costs = [1000000] * 10
costs[1] = 0
curpos = 1
for i in range(10):#残り9頂点
    for j,done in enumerate(arrived):
        costs[j] = min(costs[j],costs[curpos] + C[j][curpos])
    if i==9:break
    curpos = minindex(arrived,costs)
    arrived[curpos] = True
A = [[int(i) for i in input().split()] for j in range(H)]

def maryoku(x):
    if x == -1:return(0)
    else:return(costs[x])

print((sum([sum([maryoku(aa) for aa in a]) for a in A])))


# Optimized solution
inf = 1000000000
H,W = list(map(int,input().split()))
C = [[int(j) for j in input().split()] for i in range(10)]
#C[i][j]はiをjにするコスト
#各ノードから１への最短距離
for m in range(10):
    for s in range(10):
        for g in range(10):
                C[s][g] = min(C[s][g],C[s][m] + C[m][g])

def maryoku(x):
    if x == -1:return(0)
    else:return(C[x][1])

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

print((sum([sum([maryoku(aa) for aa in a]) for a in A])))
# 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 i in range(h)]
inf = float('inf')
cost = 0
D = [[inf]*10 for i in range(10)]
for k in range(10):
    for i in range(10):
        for j in range(10):
            D[i][j] = min(C[i][j],C[i][k]+C[k][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])

for y in range(h):
    for x in range(w):
        if A[y][x] == -1 or A[y][x] == 1:
            continue
        else:
            cost += D[A[y][x]][1]

print(cost)

# 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)]
INF = float('inf')
cost = 0
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 y in range(h):
    for x in range(w):
        if A[y][x] != -1:
            cost += C[A[y][x]][1]
print(cost)
# Original solution
inf = float("inf")

H, W = list(map(int, input().split()))
C = []
for _ in range(10):
    c = [int(s) for s in input().split()]
    C.append(c)
A = []
for _ in range(H):
    a = [int(s) for s in input().split()]
    for ai in a:
        if ai != -1:
            A.append(ai)
for _ in range(9):
    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])
ans = sum([C[ai][1] for ai in A])
print(ans)


# Optimized solution
H, W = list(map(int, input().split()))
C = []
for _ in range(10):
    c = [int(s) for s in input().split()]
    C.append(c)
A = []
for _ in range(H):
    a = [int(s) for s in input().split()]
    for ai in a:
        if ai != -1:
            A.append(ai)
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[ai][1] for ai in A])
print(ans)
# Original solution
from itertools import permutations
h, w = list(map(int, input().split()))
c = []
for _ in range(10):
    c.append(list(map(int, input().split())))
a = [0]*10
for _ in range(h):
    for i in map(int, input().split()):
        if i<0:
            continue
        a[i] += 1
ans = 0    
for i in range(10):
    if i==1 or a[i]==0:
        continue
    mi = c[i][1]
    li = [k for k in range(10) if k!=1 and k!=i]
    for j in range(1, 9):
        for p in permutations(li, j):
            pre = i
            tmp = 0
            for now in p:
                tmp += c[pre][now]
                pre = now
            tmp += c[pre][1]
            if tmp < mi:
                mi = tmp
    ans += mi*a[i]
print(ans)

# Optimized solution
from itertools import permutations
h, w = list(map(int, input().split()))
c = []
for _ in range(10):
    c.append(list(map(int, input().split())))
a = [0]*10
for _ in range(h):
    for i in map(int, input().split()):
        if i<0:
            continue
        a[i] += 1
INF = 10**10
q = list(range(10))
dist = [INF]*10
dist[1] = 0
while q:
    mi = INF
    for i in q:
        if dist[i]<mi:
            mi = dist[i]
            mi_ind = i
    node = mi_ind
    q.remove(mi_ind)
    for i in range(10):
        if dist[node]+c[i][node] < dist[i]:
            dist[i] = dist[node]+c[i][node]

ans = 0
for i in range(10):
    ans += dist[i]*a[i]
print(ans)
# 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 i in range(H)]

flag = True
while flag:
    flag = False
    for i in range(10):
        for j in range(10):
            for k in range(10):
                if c[i][k] + c[k][j] < c[i][j]:
                    c[i][j] = c[i][k] + c[k][j]
                    flag = True

result = 0
countNumber = []
for i in range(10):
    if i != 1:
        a = sum([A[h].count(i) for h in range(H) if i in A[h]])
        countNumber.append(a)
        if countNumber[-1] != 0:
            result += c[i][1] * countNumber[-1]
print(result)


# Optimized solution
def main():
    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)]

    flag = True
    while flag:
        flag = False
        for i in range(10):
            for j in range(10):
                for k in range(10):
                    if c[i][k] + c[k][j] < c[i][j]:
                        c[i][j] = c[i][k] + c[k][j]
                        flag = True

    result = 0
    countNumber = []
    for i in range(10):
        if i != 1:
            a = sum([A[h].count(i) for h in range(H) if i in A[h]])
            countNumber.append(a)
            if countNumber[-1] != 0:
                result += c[i][1] * countNumber[-1]
    print(result)

if __name__ == '__main__':
    main()
# Original solution
import sys

H, W = list(map(int, input().split()))
C = [list(map(int, sys.stdin.readline().rsplit())) for _ in range(10)]
A = [list(map(int, sys.stdin.readline().rsplit())) for _ 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]

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)


# Optimized solution
import sys

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()))
inf = float("inf")
C = [list(map(int, sys.stdin.readline().rsplit())) for _ in range(10)]
A = [list(map(int, sys.stdin.readline().rsplit())) for _ in range(H)]
warshall_floyd()
res = 0
for row in A:
    for col in row:
        if col != -1:
            res += C[col][1]

print(res)