This page is still under construction.

Parts of this page are still being built. What you see may change.

Flooding Fields

Time limit1sMemory limit512 MB

Summary
Given an n by n grid, k cows, and h hourly flood levels, find the maximum number of cows that can survive by moving each hour before the water rises.
Level

Hard8 of 10

Topics
Dynamic programming, Graph, BFS, Simulation
Solved
No attempts yet

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 nn (1≤n≤1001 \le n \le 100), kk (0≤k≤1000 \le k \le 100), and hh (1≤h≤241 \le h \le 24): nn is the side length of the field (an n×nn \times n grid of sectors), kk is the number of cows, and hh is the number of hours to track.

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

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

The next hh lines each contain one integer, the flood level for that hour (0≤level≤1000 \le \text{level} \le 100), listed in order from hour 11 to hour hh. Time starts at hour 11 while the cows are placed at hour 00, so every cow may move once before the hour-11 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.

Examples2

  1. Example 1

    Input
    3 1 3
    2 1 2
    3 1 3
    2 4 2
    1 1
    0
    2 
    1
    0 0 0
    
    Expected output
    1
    
  2. Example 2

    Input
    2 1 1
    1 3
    3 3
    0 0
    1
    1 1 1
    0
    0 0
    0
    0 0 0
    
    Expected output
    1
    0