program ABC_079_D_Wall
   implicit none
   integer H, W, d(0:9, 0:9), c(0:9, 0:9), i, j, k, countN
   integer, allocatable :: A(:, :)
   read(*, *) H, W
   do i = 0, 9
      read(*, *) c(i, 0:9)
   end do
   allocate(A(1:H, 1:W))
   do i = 1, H
      read(*, *) A(i, 1:W)
   end do
   do k = 0, 9
       do j = 0, 9
	       do i = 0, 9
               c(i, j) = min(c(i, j), c(i, k) + c(k, j))
		   end do
       end do
   end do
   countN = 0
   do j = 1, W
       do i = 1, H
	       if (A(i, j) /= - 1) then
		       countN = countN + c(A(i, j), 1)
           end if
	   end do
   end do
   write(*, *) countN
end program ABC_079_D_Wall