Wet Tiles

No attempts yetTime limit15sMemory limit256 MB

Problem

Alice runs a construction company in Norainia, a town known for its unusually dry weather. It rains only a few days a year there, so many residents put off roof repairs until a leak appears and ruins the floor. Norainia floor tiles look beautiful and hold up badly against water. Once a tile gets wet it is ruined and has to be replaced. Every rainy day brings Alice a flood of calls, and for each house she needs to know how many replacement tiles the crew should bring.

The floor of a house is a grid of square tiles, XX tiles wide and YY tiles tall. Water drips from one or more known leak positions above particular tiles. After the first minute, the tile directly under each leak is wet. After every following minute, every dry tile that shares an edge with an already wet tile becomes wet.

Walls stop the water. The house always has four outer walls around the whole grid, so water never leaves it. A house may also have inner walls. Each inner wall covers a connected straight run of tiles and is either axis aligned or set at 45 degrees to both axes. A diagonal wall is a run of tiles that touch only at their corners. Water never enters a tile that holds a wall, and it moves only between tiles that share an edge. Walls may cross each other, and no leak sits over a wall.

Figure 1 shows in gray the damage from three leaks, each marked with a white letter L, over the first five minutes. A tile labeled 2 becomes wet during the second minute, a tile labeled 3 during the third minute, and so on. Black tiles are inner walls that restrict the flow of water. After 5 minutes, 75 tiles are wet.

Figure 1

Figure 1. 75 wet tiles

Given the grid dimensions, the leaks, the inner walls, and the number of minutes TT that pass before the crew stops the leaks, count the wet tiles.

Figures 2 through 4 show the second, third, and fourth houses of the first example.

Figure 2

Figure 2. 17 wet tiles

Figure 3

Figure 3. 4 wet tiles

Figure 4

Figure 4. 94 wet tiles

Input

The input describes one or more houses.

Each house begins with a line holding five integers XX, YY, TT, LL, WW. XX and YY are the grid dimensions, with 1X10001 \le X \le 1000 and 1Y10001 \le Y \le 1000. Coordinates are one indexed, so a tile is written as a pair (x,y)(x, y) with 1xX1 \le x \le X and 1yY1 \le y \le Y. TT is the number of minutes that pass before the crew arrives and stops the leaks, with 1T2000001 \le T \le 200000. LL is the number of leaks, with 1L1001 \le L \le 100. WW is the number of inner walls, with 0W1000 \le W \le 100.

The next 2L2L integers, spread over one or more lines, are the LL distinct leak positions (x,y)(x, y).

If W>0W > 0, the next 4W4W integers, spread over one or more lines, describe the inner walls. Each wall is given by four integers x1x_1, y1y_1, x2x_2, y2y_2, the coordinates of its two ends. The wall covers every tile on the straight run from (x1,y1)(x_1, y_1) to (x2,y2)(x_2, y_2). That run is either axis aligned or set at 45 degrees. If the two ends are the same, the wall covers that single tile.

A line holding the single integer -1 ends the input.

Output

For each house, print the number of tiles that are wet after TT minutes, one per line, in the order the houses appear in the input.