You are a Software Engineer at a massive automated warehouse. The warehouse stores crates in a square grid (n x n).

The Technical Hurdle

You could pull all the crates out and sort them in a long line, but that takes too much memory (O(n^2)). The warehouse computer is old—it needs a solution with better memory efficiency.


What You Need to Do

Given an n x n matrix where:

Return the kᵗʰ smallest element in the matrix.


Function Requirement

Write a function that takes a 2D integer matrix matrix and an integer k, and returns the kᵗʰ smallest element.


Example Case