"This town ain't big enough for the two of us."
Arthur Morgan has been challenged to a game of Russian Roulette. However, this isn't normal Russian Roulette — it is played on an $$$N \times M$$$ grid where each cell $$$(r, c)$$$ has an integer value $$$A_{r,c}$$$.
Before the game starts, Arthur chooses a positive integer $$$X$$$:
Connected safe cells (sharing an edge up, down, left, or right) form a safe zone. The size of a safe zone is the total number of cells in it.
Arthur starts at $$$(1, 1)$$$ and can move up, down, left, or right. To win the game, he must reach any cell in a safe zone of size at least $$$S$$$, while stepping on at most $$$K$$$ unsafe cells along his path (including $$$(1, 1)$$$ if it is unsafe).
Find the maximum integer $$$X$$$ Arthur can choose to win the game, or output 0 if it is impossible.
Rows are numbered from top to bottom and columns from left to right, so $$$(1, 1)$$$ is the top-left cell.
Constraints:
Print a single integer — the maximum valid $$$X$$$, or 0 if Arthur cannot win.
| Subtask | Points | $$$N$$$ | $$$M$$$ | $$$K$$$ | $$$S$$$ | $$$A_{r,c}$$$ | Additional Constraints |
| 0 | 0 | - | - | - | - | - | The examples |
| 1 | 20 | $$$\le 40$$$ | $$$\le 40$$$ | - | - | $$$\le 50$$$ | No additional constraints. |
| 2 | 20 | - | - | - | $$$= 1$$$ | $$$\in \{1, 2\}$$$ | An optimal path only moves Down and Right. |
| 3 | 25 | - | - | $$$= N \times M$$$ | - | - | No additional constraints. |
| 4 | 35 | - | - | - | - | - | No additional constraints. |
All other constraints are the same as in the Input section.
3 4 1 39 2 8 81 1 2 94 4 1 7
8
The grid is:
9 2 8 8
1 1 2 9
4 4 1 7
Choose $$$X = 8$$$. The safe cells are $$$(1,1)$$$, $$$(1,3)$$$, $$$(1,4)$$$ and $$$(2,4)$$$.
Choose $$$X = 9$$$. Only $$$(1,1)$$$ and $$$(2,4)$$$ are safe, and each forms a zone of size $$$1 \lt S$$$, so Arthur cannot win. Therefore the maximum valid $$$X$$$ is $$$8$$$.
| Name |
|---|


