#!usr/bim/python

#! -*- coding: utf-8 -*-



import numpy as np



hw = input()

hw = hw.split()

h,w = list(map(int,hw))



c = np.zeros([10,10],int)

for i in range(10):

    line = input()

    line = line.split()

    c[i] = list(map(int,line))



#a = np.zeros([h,w],dtype=int)

#for i in range(h):

#    line = raw_input()

#    line = line.split()

#    a[i] = map(int,line)



suji_num = np.zeros([10],int)

for i in range(h):

    line = input()

    line = line.split()

    line = list(map(int,line))

    for j in range(10):

        num = line.count(j)

        suji_num[j] += num



maryoku = np.zeros([10],int)



for i in range(10):

    maryoku[i] = c[i,1]



i = 1

while i < max(maryoku):

    #print maryoku

    for j in range(10):

        if maryoku[j] == i:

            for k in range(10):

                keiyum = c[k,j]+i

                if keiyum < maryoku[k]:

                    maryoku[k] = keiyum

    i += 1



ans = 0

for i in range(10):

    ans += suji_num[i] * maryoku[i]



print(ans)

#!usr/bim/python

#! -*- coding: utf-8 -*-



import numpy as np



hw = input()

hw = hw.split()

h,w = list(map(int,hw))



c = np.zeros([10,10],int)

for i in range(10):

    line = input()

    line = line.split()

    c[i] = list(map(int,line))



#a = np.zeros([h,w],dtype=int)

#for i in range(h):

#    line = raw_input()

#    line = line.split()

#    a[i] = map(int,line)



suji_num = np.zeros([10],int)

for i in range(h):

    line = input()

    line = line.split()

    line = list(map(int,line))

    for j in range(10):

        num = line.count(j)

        suji_num[j] += num



maryoku = np.zeros([10],int)



for i in range(10):

    maryoku[i] = c[i,1]



i = 1

while i < max(maryoku)-1:

    #print maryoku

    for j in range(10):

        if maryoku[j] == i:

            for k in range(10):

                keiyum = c[k,j]+i

                if keiyum < maryoku[k]:

                    maryoku[k] = keiyum

    i += 1



ans = 0

for i in range(10):

    ans += suji_num[i] * maryoku[i]



print(ans)

#!usr/bim/python
#! -*- coding: utf-8 -*-
import numpy as np

hw = input()
hw = hw.split()
h, w = list(map(int, hw))
c = np.zeros([10, 10], int)
for i in range(10):
    line = input()
    line = line.split()
    c[i] = list(map(int, line))
# a = np.zeros([h,w],dtype=int)
# for i in range(h):
#    line = raw_input()
#    line = line.split()
#    a[i] = map(int,line)
suji_num = np.zeros([10], int)
for i in range(h):
    line = input()
    line = line.split()
    line = list(map(int, line))
    for j in range(10):
        num = line.count(j)
        suji_num[j] += num
maryoku = np.zeros([10], int)
for i in range(10):
    maryoku[i] = c[i, 1]
i = 1
while i < max(maryoku):
    # print maryoku
    for j in range(10):
        if maryoku[j] == i:
            for k in range(10):
                keiyum = c[k, j] + i
                if keiyum < maryoku[k]:
                    maryoku[k] = keiyum
    i += 1
ans = 0
for i in range(10):
    ans += suji_num[i] * maryoku[i]
print(ans)

#!usr/bim/python
#! -*- coding: utf-8 -*-
import numpy as np

hw = input()
hw = hw.split()
h, w = list(map(int, hw))
c = np.zeros([10, 10], int)
for i in range(10):
    line = input()
    line = line.split()
    c[i] = list(map(int, line))
# a = np.zeros([h,w],dtype=int)
# for i in range(h):
#    line = raw_input()
#    line = line.split()
#    a[i] = map(int,line)
suji_num = np.zeros([10], int)
for i in range(h):
    line = input()
    line = line.split()
    line = list(map(int, line))
    for j in range(10):
        num = line.count(j)
        suji_num[j] += num
maryoku = np.zeros([10], int)
for i in range(10):
    maryoku[i] = c[i, 1]
i = 1
while i < max(maryoku) - 1:
    # print maryoku
    for j in range(10):
        if maryoku[j] == i:
            for k in range(10):
                keiyum = c[k, j] + i
                if keiyum < maryoku[k]:
                    maryoku[k] = keiyum
    i += 1
ans = 0
for i in range(10):
    ans += suji_num[i] * maryoku[i]
print(ans)

-while i < max(maryoku): +while i < max(maryoku) - 1: