import math

st=input().split()

H=int(st[0]);W=int(st[1])

c=[[0 for i in range(10)] for i in range(10)]

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

for i in range(10):

    st=input().split()

    for j in range(10):

        c[i][j]=int(st[j])

for i in range(H):

    st=input().split()

    for j in range(W):

        A[i][j]=int(st[j])

def Dijkstra(s):

    d=[10000]*10

    d[s]=0

    Q=set(range(10))

    V=set(range(10))

    while(len(Q)>0):

        u=list(Q)[0]

        for i in Q:

            if d[i]<d[u]:

                u=i

        Q-={u}

        for v in V-{u}:

            if (d[v]>d[u]+c[u][v]):

                d[v]=d[u]+c[u][v]

    return d[1]

result=0

DD=[0]*10

for i in range(10):

    DD[i]=Dijkstra(i)

for i in range(H):

    for j in range(W):

        if A[i][j]!=-1:

            result+=DD[A[i][j]]

print(result)

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

      if c[i][j]>c[i][k]+c[k][j]:

        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)

import math

st = input().split()
H = int(st[0])
W = int(st[1])
c = [[0 for i in range(10)] for i in range(10)]
A = [[0 for i in range(W)] for i in range(H)]
for i in range(10):
    st = input().split()
    for j in range(10):
        c[i][j] = int(st[j])
for i in range(H):
    st = input().split()
    for j in range(W):
        A[i][j] = int(st[j])


def Dijkstra(s):
    d = [10000] * 10
    d[s] = 0
    Q = set(range(10))
    V = set(range(10))
    while len(Q) > 0:
        u = list(Q)[0]
        for i in Q:
            if d[i] < d[u]:
                u = i
        Q -= {u}
        for v in V - {u}:
            if d[v] > d[u] + c[u][v]:
                d[v] = d[u] + c[u][v]
    return d[1]


result = 0
DD = [0] * 10
for i in range(10):
    DD[i] = Dijkstra(i)
for i in range(H):
    for j in range(W):
        if A[i][j] != -1:
            result += DD[A[i][j]]
print(result)

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):
            if c[i][j] > c[i][k] + c[k][j]:
                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)

-import math - -st = input().split() -H = int(st[0]) -W = int(st[1]) -c = [[0 for i in range(10)] for i in range(10)] -A = [[0 for i in range(W)] for i in range(H)] -for i in range(10): -    st = input().split() -    for j in range(10): -        c[i][j] = int(st[j]) -for i in range(H): -    st = input().split() -    for j in range(W): -        A[i][j] = int(st[j]) - - -def Dijkstra(s): -    d = [10000] * 10 -    d[s] = 0 -    Q = set(range(10)) -    V = set(range(10)) -    while len(Q) > 0: -        u = list(Q)[0] -        for i in Q: -            if d[i] < d[u]: -                u = i -        Q -= {u} -        for v in V - {u}: -            if d[v] > d[u] + c[u][v]: -                d[v] = d[u] + c[u][v] -    return d[1] - - -result = 0 -DD = [0] * 10 -for i in range(10): -    DD[i] = Dijkstra(i) +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): +            if c[i][j] > c[i][k] + c[k][j]: +                c[i][j] = c[i][k] + c[k][j] +ans = 0 -            result += DD[A[i][j]] -print(result) +            ans += c[A[i][j]][1] +print(ans)