program Wall
    implicit none
    integer(4):: h,w,c(0:9,0:9), changer(0:9)
    integer(4):: i,j,ans
    integer(4),allocatable:: a(:,:)

    read*, h,w

    do i=0,9
        read*, c(:,i)
    end do

    allocate(a(w,h))

    do i=1,h
        read*, a(:,i)
    end do

    call warf(c)

    changer(0:9) = c(1,0:9)

    ans=0
    do i=1,h
        do j=1,w
            if (a(j,i) == -1) cycle
            ans = ans + changer(a(j,i))
        end do
    end do
    print*, ans
contains
    subroutine warf(det)
        integer(4):: det(:,:)
        integer(4):: i,j,k,l
        
        l = size(det,dim=1)
        do k=1,l
            do i=1,l
                do j=1,l
                    det(i,j) = min(det(i,j), det(i,k)+det(k,j))
                end do
            end do
        end do
    end subroutine
end program Wall