Under construction

Still being built. Things may change

Hate Stagnant Water

Interview

Time limit1sMemory limit1024 MB

Summary
Count the h by w rectangles on an N by M grid that contain no standing water, where drained cells spread to neighbors at equal or higher height.
Level

Medium4 of 10

Topics
BFS, Prefix sum, Matrix
Solved
No attempts yet

Problem

On a rainy day, Sejong went out to buy water for his parents. While waiting at a crosswalk, he saw water pooling on it because the crosswalk was not flat.

The crosswalk is an N×MN\times M rectangular grid. The cell at the front row on Sejong's side, far left, is row 1, column 1. The cell far right in that row is row 1, column MM.

Drains are installed in KK cells of the crosswalk. A cell drains water if one of its vertically or horizontally adjacent cells has a drain or is already drained, and that adjacent cell's height is lower than or equal to its own. A cell without a drain that does not drain is called a standing water cell.

Sejong solved A+B and got new shoes. He does not want to dirty them, so he tries to cross without stepping on any standing water cell. His new shoes are a rectangle that covers hh consecutive rows and ww consecutive columns. When Sejong steps, the shoe always fits exactly onto the grid cells, and he never turns his foot.

Given the height of each cell, write a program that counts the number of ways Sejong can place one foot on the crosswalk without dirtying his shoes.

Input

The first line contains two integers NN and MM, separated by a space. (1≤N,M≤10001 \le N, M \le 1000)

The second line contains two integers hh and ww, the length and width of Sejong's shoes, separated by a space. (1≤h≤N1 \le h \le N, 1≤w≤M1 \le w \le M)

The next NN lines contain MM integers each. On line i+2i+2, the integers Hi,1,Hi,2,⋯ ,Hi,MH_{i,1}, H_{i,2}, \cdots, H_{i,M} are separated by spaces. Hi,jH_{i,j} is the height of the cell at row ii, column jj. (1≤Hi,j≤1001 \le H_{i,j} \le 100)

Line N+3N+3 contains an integer KK, the number of drains. (1≤K≤N×M1 \le K \le N \times M)

The next KK lines each contain two integers rr and cc, separated by a space, meaning there is a drain at row rr, column cc. No position is repeated. (1≤r≤N1 \le r \le N, 1≤c≤M1 \le c \le M)

Output

Print the number of ways Sejong can place one foot on the crosswalk.

Examples1

  1. Example 1

    Input
    5 5
    2 1
    2 6 7 8 8
    2 3 3 7 9
    4 5 2 6 5
    3 2 1 3 4
    5 7 9 8 1
    2
    2 2
    5 5
    
    Expected output
    9