Forest Fires
Global warming increases the risk of forest fires. To ensure a quick response in the event of a forest fire, Mykola Petrovych wants to simulate how a potential fire might spread.
The forest is shaped like a rectangle with dimensions N x M, divided into unit squares. Each square has a specified number of minutes it takes to burn completely. Once a square is fully burned, the fire immediately spreads to adjacent squares (horizontally, vertically, and diagonally) that have not yet burned or are not currently burning.
The starting points of the fire are known. Your task is to help Mykola Petrovych determine how many minutes it will take for the fire to reach a specific square.
Input
The first line contains 5 integers: N, M, K, X, Y. Here, N and M represent the dimensions of the forest, K is the number of ignition points, and X, Y are the coordinates of the square for which you need to calculate the time it takes for the fire to reach.
The following N lines each contain M numbers, representing the time it takes for each corresponding square to burn completely.
The next K lines provide the coordinates of the ignition points.
All numbers are natural numbers and do not exceed 150.
Output
Output a single integer: the time in minutes it will take for the fire to reach the square at coordinates X, Y.