#B3818. Escape Routes of the Pink Rabbits

    ID: 11475 Type: Default 1000ms 256MiB

Escape Routes of the Pink Rabbits

Escape Routes of the Pink Rabbits

In this problem, the computer screen is divided into rr rows and cc columns forming a grid. The cell in the ii-th row and jj-th column is represented by (i,j)(i,j).

Suddenly, F's computer is infected with the "Full Screen Pink Rabbit" virus, which causes NN male pink rabbits and MM female pink rabbits to appear on the screen.

A pink rabbit located at (i,j)(i,j) is said to have an escape channel in one of the four cardinal directions (up, down, left, or right) if it can move continuously in that direction and exit the grid without passing through any cell containing a pink rabbit of the opposite gender (i.e. if the rabbit is male, cells with a female pink rabbit block the path, and vice versa). Rabbits of the same gender do not block the path.

A pink rabbit is considered a "qualified pink rabbit" if it has at least three escape channels. Your task is to determine the number of qualified pink rabbits on the screen given their positions.

inputFormat

The input consists of multiple lines:

The first line contains four integers rr, cc, NN, MM, where rr and cc (1r,c10001\le r,c\le 1000) represent the number of rows and columns of the grid, and NN and MM denote the number of male and female pink rabbits respectively.

Each of the next NN lines contains two integers ii and jj (1ir1\le i\le r, 1jc1\le j\le c), representing the position of a male pink rabbit.

Each of the following MM lines contains two integers ii and jj, representing the position of a female pink rabbit.

You can assume that no cell will contain more than one rabbit.

outputFormat

Output a single integer representing the total number of qualified pink rabbits.

sample

5 5 1 1
3 3
2 2
2