program divisible_substring
  implicit none
  integer :: n, p, i, j, k = 0, m, t
  character(200000) :: s
  integer(8) :: dp(0:10000,0:1) = 0, ans = 0
  read(*,*) n, p
  read(*,*) s
  do i = 1, n
    k = 1-k
    dp(:,k) = 0
    m = mod(ichar(s(i:i))-48,p)
    dp(m,k) = 1
    do j = 0, p-1
      t = mod(j*10+m,p)
      dp(t,k) = dp(t,k)+dp(j,1-k)
    end do
    ans = ans+dp(0,k)
  end do
  write(*,'(i0)') ans
end program divisible_substring