k-th smallest in a sorted matrix

matrix is a list of rows where every row and every column is sorted ascending. Write kth_smallest(matrix, k) that returns the k-th smallest value overall, counting repeats.

kth_smallest([[1, 5, 9], [10, 11, 13], [12, 13, 15]], 8)  ->  13
kth_smallest([[-5]], 1)                                   ->  -5

Two good routes: a heap merging the rows (k log n), or binary search on the value with a linear count of how many entries are at most the guess. Flattening and sorting also passes.

def kth_smallest(matrix, k):
    ...