Mining

Given a grid of mineral strengths with air only on the top, left, and right faces, find the smallest performance D so that at least K minerals can be removed in some order.

Hard8Binary searchBFSImplementationArrayNo attempts yetTime limit2sMemory limit256 MB

Problem

A mine of NN rows and MM columns rests on the ground. It is made of N×MN \times M minerals of size 1×11 \times 1, and the mineral in row ii, column jj has strength Si,jS_{i,j}.

The top face of the mine, its left face and its right face touch air. Its bottom face rests on the ground.

You want to mine these minerals with a mining machine. The mining machine can pick any mineral that has at least one face touching air and mine it. A mineral whose four faces all touch the ground or other minerals cannot be mined. Once a mineral is mined, air fills the square it occupied, so minerals further inside become exposed.

A machine of performance DD can mine only the minerals whose strength is at most DD. Find the smallest DD that lets the machine mine at least KK minerals.

Input

The first line contains NN, MM and KK. (1N,M10001 \le N, M \le 1000, 1KN×M1 \le K \le N \times M)

Each of the next NN lines gives the strengths of one row of minerals, from the top row down. Line ii contains MM integers Si,1,Si,2,,Si,MS_{i,1}, S_{i,2}, \dots, S_{i,M} separated by spaces. (1Si,j1061 \le S_{i,j} \le 10^6)

Output

Print the smallest performance DD that lets the machine mine at least KK minerals.