```
class Solution(object):
def kthSmallest(self, matrix, k):
return list(heapq.merge(*matrix)][k-1]
```

Ok, kidding aside. This is `O(klog(n))`

.

```
class Solution(object):
def kthSmallest(self, matrix, k):
heap, res = [(row[0], r, 0)
for r, row in enumerate(matrix)
if row], 0
heapq.heapify(heap)
for k in range(1, k + 1):
res, row, col = heapq.heappop(heap)
if col + 1 < len(matrix[row]):
heapq.heappush(heap, (matrix[row][col + 1], row, col + 1))
return res
```

This is `O(klog(k))`

```
class Solution(object):
def kthSmallest(self, matrix, k):
heap, res, n = [(matrix[0][0], 0, 0)], 0, len(matrix)
for k in range(1, k + 1):
res, row, col = heapq.heappop(heap)
if not row and col < n - 1:
heapq.heappush(heap, (matrix[row][col + 1], row, col + 1))
if row < n - 1:
heapq.heappush(heap, (matrix[row + 1][col], row + 1, col))
return res
```