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

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

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