program prob5
    implicit none
    integer, allocatable::a(:,:)
    integer::c(10,10), n(10), t1(10), dp(10)
    integer::h, w, i, j, k, ans
    read(*,*) h, w
    allocate(a(h,w))
    do i = 1, 10
        read(*,*) c(i,:)
    end do

    n = 0
    do i = 1, h
        read(*,*) a(i,:)
        do j = 1, w
            if(a(i,j) > -1) then 
                n(a(i,j)+1) = n(a(i,j)+1) + 1
            end if
        end do
    end do
    dp(:) = c(:,2)
    do k = 1, 10
        do i = 1, 10
            do j = 1, 10
                dp(i) = min(dp(i),dp(j)+c(i,j))
            end do
        end do
    end do
    ans = 0
    do i = 1, 10
        ans = ans + n(i)*dp(i)
    end do
    write(*,*) ans
    stop
end program