program ABC079D
    implicit none
    integer(8)::H,W
    integer(8)::cost(10,10)
    integer(8),allocatable,dimension(:,:)::A
    integer(8)::i,j
    integer(8)::ans
    read*,H,W
    do i=1,10
        read*,cost(i,:)
    end do
    call Warshall_Floyd(10_8,cost)

    allocate(A(H,W))
    do i=1,H
        read*,A(i,:)
    end do
    
    ans=0
    do i=1,H
        do j=1,W
            if(A(i,j)==-1)cycle
            ans=ans+Cost(A(i,j)+1_8,2_8)
        end do
    end do
    print"(i0)",ans
    contains
subroutine Warshall_Floyd(n,Route)
  !nは頂点の数、Routeは全ての経路の長さを記録したもの
  !以下の型宣言は場合に応じて変えること
  implicit none
  integer(8), intent(in) :: n
  integer(8), intent(inout) :: Route(n, n)
  integer(8) :: i, j, k

    do k = 1, n
      do i = 1, n
        do j = 1, n
          Route(j, i) = min( Route(j, i), Route(j, k) + Route(k, i))
        end do
      end do
    end do
end subroutine Warshall_Floyd
end program 