import sys

from scipy.sparse.csgraph import floyd_warshall as wf

input = sys.stdin.readline



def main():

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

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

  a = [list(map(int, input().split())) for i in range(H)]

  

  link = wf(link)

  

  ans = 0

  for i in range(H):

    for j in range(W):

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

        ans += link[a[i][j]][1]

  print((int(ans)))



if __name__ == "__main__":

  main()
from scipy.sparse.csgraph import floyd_warshall as fw



def main():

    h, w = list(map(int, input().split()))

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



    a = [list(map(int, input().split())) for i in range(h)]



    link = fw(link)



    ans = 0

    for i in range(h):

        for j in range(w):

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

                ans += link[a[i][j]][1]

    print((int(ans)))



if __name__ == "__main__":

    main()
import sys
from scipy.sparse.csgraph import floyd_warshall as wf

input = sys.stdin.readline


def main():
    H, W = list(map(int, input().split()))
    link = [list(map(int, input().split())) for i in range(10)]
    a = [list(map(int, input().split())) for i in range(H)]
    link = wf(link)
    ans = 0
    for i in range(H):
        for j in range(W):
            if a[i][j] != -1:
                ans += link[a[i][j]][1]
    print((int(ans)))


if __name__ == "__main__":
    main()

from scipy.sparse.csgraph import floyd_warshall as fw


def main():
    h, w = list(map(int, input().split()))
    link = [list(map(int, input().split())) for i in range(10)]
    a = [list(map(int, input().split())) for i in range(h)]
    link = fw(link)
    ans = 0
    for i in range(h):
        for j in range(w):
            if a[i][j] != -1:
                ans += link[a[i][j]][1]
    print((int(ans)))


if __name__ == "__main__":
    main()

-import sys -from scipy.sparse.csgraph import floyd_warshall as wf - -input = sys.stdin.readline +from scipy.sparse.csgraph import floyd_warshall as fw -    H, W = list(map(int, input().split())) +    h, w = list(map(int, input().split())) -    a = [list(map(int, input().split())) for i in range(H)] -    link = wf(link) +    a = [list(map(int, input().split())) for i in range(h)] +    link = fw(link) -    for i in range(H): -        for j in range(W): +    for i in range(h): +        for j in range(w):