program wall
  implicit none
  integer :: h, w, c(0:9, 0:9) = 0, a(200) = -1, i, j, k, x = 0
  read(*,*) h, w
  do i = 0, 9
    read(*,*) c(i, 0:9)
  end do
  do k = 0, 9
    do i = 0, 9
      do j = 0, 9
        c(i, j) = min(c(i, j), c(i, k) + c(k, j))
      end do
    end do
  end do
  do i = 1, h
    read(*,*) a(1:w)
    do j = 1, w
      if (a(j) < 0) cycle
      x = x + c(a(j), 1)
    end do
  end do
  write(*,'(i0)') x
end program wall