Flooding Fields

No attempts yetTime limit1sMemory limit512 MB

Problem

It is raining on Farmer John's farm and his pastures are in danger of flooding. Fortunately, he has installed a state-of-the-art drainage system that keeps the water level identical everywhere on the farm. He has been less fortunate with the terrain.

Farmer John's cows can only stand on dry land: if the sector a cow is standing on becomes wet, that cow drowns. Each hour a cow may either stay where it is or move to one of the four orthogonally adjacent sectors -- up, down, left, or right -- which gives it a chance to escape the water, whose level rises and falls from hour to hour.

The field is divided into grid sectors, each indexed by a row and a column. Every sector is small enough to hold at most one cow at any moment.

Each hour proceeds in two steps: first every cow moves (or stays), then that hour's flood level is applied and every cow standing on a flooded sector drowns. A sector is flooded when its height is less than or equal to the current flood level.

Assuming every cow is extremely intelligent and prescient, so the cows can always coordinate on the best possible moves, what is the maximum number of cows that can survive through the final hour?

Input

The input contains several test cases.

Each test case begins with a line of three integers $n$ ($1 \le n \le 100$), $k$ ($0 \le k \le 100$), and $h$ ($1 \le h \le 24$): $n$ is the side length of the field (an $n \times n$ grid of sectors), $k$ is the number of cows, and $h$ is the number of hours to track.

Each of the next $n$ lines contains $n$ integers giving the height of each sector ($0 \le \text{height} \le 100$). The first of these lines is row $0$ and the last is row $n-1$; within a line the first value is column $0$ and the last is column $n-1$.

The next $k$ lines each contain two integers $r$ and $c$ ($0 \le r, c < n$): the row and column of one cow at hour $0$. No two cows start in the same sector.

The next $h$ lines each contain one integer, the flood level for that hour ($0 \le \text{level} \le 100$), listed in order from hour $1$ to hour $h$. Time starts at hour $1$ while the cows are placed at hour $0$, so every cow may move once before the hour-$1$ flood.

The input ends with a line containing three zeros, which must not be processed.

Output

For each test case, print a single integer: the maximum number of cows that can survive. Print each answer on its own line, with no extra spaces and no blank lines between answers.