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

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

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

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

-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 - - +inf = 1000000000 -# のつもりなんだけど、ダイクストラ法の逆打ち計算では逆をやります -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)] +# 各ノードから１への最短距離 +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]) -        return costs[x] +        return C[x][1] +A = [list(map(int, input().split())) for i in range(H)]