NTHU is a kingdom, housing students and various creatures, those creatures like Procat (破貓), Crazytingyaobeast (瘋狂挺腰獸), Koprofessor (摳腳獸), EmperorPenguin07 (皇帝企鵝7號), FatFatCat (胖胖貓), Thankshanger (謝衣架), and more. We refer to these creatures as “Pokemon” because they are cute and unique in their own way.
One day, a Pokemon snake, though it does not consider itself a Pokemon ^^, has escaped!
Recently, several computer science students have found the snake near the 新齋.
Our snake-catching team leader (捕蛇大隊長), Thanksone, volunteered to capture the snake. He prepared to go with a wooden stick and a blue laundry basket.
[Thanksone is ready to catch the snake]
However, Koying thought this was too dangerous and decided to lend the road roller to Thanksone, allowing him to use it to crush the snake.
Once Thanksone got hold of the road roller, he became very excited and embarked on numerous modifications to the machine. These modifications were aimed at making it easier for him to capture the snake.
[Simulation environment]: In order to test whether the modified road roller could successfully crush the snake, Thanksone designed a simulation environment.
[Snake's movement]: Since the snake doesn't consider itself a Pokemon, it will start from the grid with the lowest Pokemon Value.
The snake keeps moving until it is crushed by the road roller or the road roller runs out of fuel.
[Road roller's movement]: On the other hand, since Thanksone thinks that the snake is also a Pokemon, he will start the road roller from the grid with the highest Pokemon Value.
The road roller keeps moving following the policy in Step 2 until it runs out of fuel or the snake is successfully crushed.
We define the road roller crushing the snake as the snake and the road roller being on the same grid at the same time.
When the road roller comes to a stop, please output how much fuel is remaining.
The first line consists of three positive integers, N, M, and S, indicating that the map size is N x M, and the initial fuel level of the road roller is S.
Following that, there are N lines, each containing M positive integers, denoted as Pi, j, representing the Pokemon Value for the grid cell in row i and column j.
In the first line, please output an integer, representing the remaining fuel of the road roller.
In the second line:
Remember to print a ‘\n’ at the end of the output.
Sample I/O 1:
1. The road roller emits searchlights (探照燈) in all eight directions.
2. A searchlight illuminates the snake and the snake is at (3, 3), so the road roller decided to go to (3, 3).
Since the snake can't go right, it switches to facing the left.
3. The road roller goes 1 step on the right-down direction and takes 1 unit of fuel, with 6 units of fuel left.
The snake goes 1 step on the left direction.
4. The road roller goes 1 step on the right-down direction and takes 1 unit of fuel, with 5 units of fuel left.
The snake goes 1 step on the left direction.
5. Since the road roller reaches (3, 3), it emits searchlights in all eight directions again.
6. A searchlight illuminates the snake and the snake is at (3, 1), so the road roller decided to go to (3, 1).
Since the snake can't go left, it switches to facing the right.
7. The road roller goes 1 step on the left direction and takes 1 unit of fuel, with 4 units of fuel left.
The snake goes 1 step on the right direction.
The snake met the road roller at (3, 2) and the road roller has 4 units of fuel left, so Thanksone stops road rolling.
Sample I/O 2:
1. The road roller emits searchlights (探照燈) in all eight directions.
2. No searchlights illuminate the snake, so the road roller will teleport to the grid with the closest Pokemon Value to 95 and has not been visited. (If there are multiple such grids, move to the one with the lowest Pokemon Value.)
So the road roller teleports to 91, which costs 1 unit of fuel, with 3 units of fuel left.
The snake goes 1 step on the right direction.
3. The road roller emits searchlights (探照燈) in all eight directions.
4. A searchlight illuminates the snake and the snake is at (3, 3), so the road roller decided to go to (3, 3).
Since the snake can't go right, it switches to facing the left.
5. The road roller goes 1 step on the right direction and takes 1 unit of fuel, with 2 units of fuel left.
The snake goes 1 step on the left direction.
The snake met the road roller at (3, 2) and the road roller has 2 units of fuel left, so Thanksone stops road rolling.