#!/usr/bin/env python3





def main():

    H, W = list(map(int, input().split()))

    INF = 10**12

    c = [None for _ in range(10)]

    for i in range(10):

        c[i] = list(map(int, input().split()))



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

        A = list(map(int, input().split()))

        for j in range(W):

            if A[j] != -1:

                ans += c[A[j]][1]

    print(ans)



if __name__ == "__main__":

    main()

from heapq import heappop, heappush



H, W = list(map(int, input().split()))

c =  [list(map(int, input().split())) for _ in range(10)]



# Dijkstra法で1からの距離を求める

INF = 10**12

q = [(0,1)] # 始点のコストとindex

d = [INF] * 10

d[1] = 0



while len(q) > 0:

    cost, u = heappop(q)

    #print(u,c[u])

    for v in range(10):

        if v == u: continue

        #print(i,v)

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

        #print(v,tmp)

        if tmp < d[v]:

            d[v] = tmp

            heappush(q, (d[v], v))

    #print(d)



A =  [0]*10

for i in range(H):

    tmp = list(map(int, input().split()))

    for j in range(W):

        if tmp[j] >= 0:

            A[tmp[j]] += 1



ans = 0

for i in range(10):

    ans += A[i]*d[i]

print(ans)
#!/usr/bin/env python3
def main():
    H, W = list(map(int, input().split()))
    INF = 10**12
    c = [None for _ in range(10)]
    for i in range(10):
        c[i] = list(map(int, input().split()))
    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):
        A = list(map(int, input().split()))
        for j in range(W):
            if A[j] != -1:
                ans += c[A[j]][1]
    print(ans)


if __name__ == "__main__":
    main()

from heapq import heappop, heappush

H, W = list(map(int, input().split()))
c = [list(map(int, input().split())) for _ in range(10)]
# Dijkstra法で1からの距離を求める
INF = 10**12
q = [(0, 1)]  # 始点のコストとindex
d = [INF] * 10
d[1] = 0
while len(q) > 0:
    cost, u = heappop(q)
    # print(u,c[u])
    for v in range(10):
        if v == u:
            continue
        # print(i,v)
        tmp = d[u] + c[v][u]
        # print(v,tmp)
        if tmp < d[v]:
            d[v] = tmp
            heappush(q, (d[v], v))
    # print(d)
A = [0] * 10
for i in range(H):
    tmp = list(map(int, input().split()))
    for j in range(W):
        if tmp[j] >= 0:
            A[tmp[j]] += 1
ans = 0
for i in range(10):
    ans += A[i] * d[i]
print(ans)

-#!/usr/bin/env python3 -def main(): -    H, W = list(map(int, input().split())) -    INF = 10**12 -    c = [None for _ in range(10)] -    for i in range(10): -        c[i] = list(map(int, input().split())) -    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): -        A = list(map(int, input().split())) -        for j in range(W): -            if A[j] != -1: -                ans += c[A[j]][1] -    print(ans) +from heapq import heappop, heappush - -if __name__ == "__main__": -    main() +H, W = list(map(int, input().split())) +c = [list(map(int, input().split())) for _ in range(10)] +# Dijkstra法で1からの距離を求める +INF = 10**12 +q = [(0, 1)]  # 始点のコストとindex +d = [INF] * 10 +d[1] = 0 +while len(q) > 0: +    cost, u = heappop(q) +    # print(u,c[u]) +    for v in range(10): +        if v == u: +            continue +        # print(i,v) +        tmp = d[u] + c[v][u] +        # print(v,tmp) +        if tmp < d[v]: +            d[v] = tmp +            heappush(q, (d[v], v)) +    # print(d) +A = [0] * 10 +for i in range(H): +    tmp = list(map(int, input().split())) +    for j in range(W): +        if tmp[j] >= 0: +            A[tmp[j]] += 1 +ans = 0 +for i in range(10): +    ans += A[i] * d[i] +print(ans)