UFO

Time limit2sMemory limit256 MB

Summary
Simulate laser shots that each destroy up to R blocks at height h along one row or column, then find the P by P square holding the most surviving blocks.
Level

Medium6 of 10

Topics
Segment tree, Simulation, Prefix sum
Solved
No attempts yet

Problem

An alien spaceship has been forced down in a desert and has to be destroyed. The ship is built from unit cube blocks, and its bottom layer is an N×MN \times M rectangle. Cell (i,j)(i, j) of that rectangle carries a stack of blocks, and the layers of a stack are numbered 11, 22 and so on from the ground up. Rows are numbered 11 to NN from the north side to the south side, and columns are numbered 11 to MM from the west side to the east side. The figure shows the top view of a ship with N=4N = 4 and M=8M = 8.

The blocks are made of a metal that only a laser can cut, so laser guns were placed on the four sides of the ship. A ray flies perpendicular to the side it was fired from, parallel to the ground, along one fixed layer.

A shot is given by a side, an index, and a height hh. A ray fired from the west runs along the given row from column 11 toward column MM, and a ray fired from the east runs along the same row from column MM toward column 11. A ray fired from the north runs along the given column from row 11 toward row NN, and a ray fired from the south runs along the same column from row NN toward row 11.

The ray passes over the cells of that line one at a time. If a cell holds at least hh blocks, the ray destroys the block on layer hh, every block above it drops one layer, and the height of that cell falls by 11. At most one block is destroyed in a cell, and the ray moves straight on to the next one. A cell holding fewer than hh blocks lets the ray through unchanged. The ray stops once it has destroyed RR blocks, and it also stops if it leaves the ship before that.

After the KK shots an airstrike is called on a square area of size P×PP \times P. The area with the largest number of surviving blocks is chosen, and the strike destroys every block inside it. Compute how many blocks the airstrike destroys.

Input

The first line contains five integers NN, MM, RR, KK, PP (1≤N×M≤1061 \le N \times M \le 10^6, 1≤R≤101 \le R \le 10, 1≤K≤3×1051 \le K \le 3 \times 10^5, 1≤P≤min⁡(N,M,10)1 \le P \le \min(N, M, 10)).

Each of the next NN lines contains MM integers. The jj-th number on the ii-th line is the number of blocks stacked on cell (i,j)(i, j), between 11 and 10610^6.

Each of the next KK lines describes one shot with a single character and two integers, separated by spaces. The character is W for west, E for east, S for south, or N for north. For W and E the first integer is a row number between 11 and NN; for N and S it is a column number between 11 and MM. The second integer is the height of the shot, between 11 and 10610^6.

Output

Print one integer, the largest number of blocks left inside a P×PP \times P area after all KK shots.

Hint

The figure shows the ship of the first example after every shot of that example. The shaded square is the 2×22 \times 2 area holding the most blocks.

Examples7

  1. Example 1

    Input
    4 8 2 6 2
    1 1 1 1 1 1 1 1
    1 2 3 1 1 1 3 1
    1 2 1 1 3 1 1 1
    1 1 1 1 1 1 1 2
    N 2 2
    W 2 2
    W 2 3
    E 2 1
    S 4 1
    S 7 1
    
    Expected output
    6
    
  2. Example 2

    Input
    1 1 1 1 1
    5
    W 1 1
    
    Expected output
    4
    
  3. Example 3

    Input
    3 3 3 4 2
    2 3 1
    1 4 2
    3 1 1
    W 1 5
    E 2 9
    N 3 4
    S 1 100
    
    Expected output
    10
    
  4. Example 4

    Input
    3 4 10 4 2
    1 1 1 1
    1 1 1 1
    1 1 1 1
    W 1 1
    N 1 1
    E 3 1
    S 4 1
    
    Expected output
    2
    
  5. Example 5

    Input
    5 5 2 10 3
    1 1 1 1 1
    1 9 5 9 1
    1 5 1 5 1
    1 9 5 9 1
    1 1 1 1 1
    W 2 5
    E 2 5
    N 2 5
    S 2 5
    W 4 9
    E 4 9
    N 4 9
    S 4 9
    W 3 1
    E 3 1
    
    Expected output
    46
    
  6. Example 6

    Input
    6 7 4 20 1
    8 9 8 8 9 10 4
    3 9 8 11 10 3 2
    8 5 3 2 9 12 11
    1 10 7 8 11 12 10
    11 3 10 1 9 2 1
    1 4 4 10 1 8 6
    N 5 4
    E 6 5
    N 1 11
    W 4 11
    S 4 9
    W 6 5
    S 7 4
    S 1 2
    W 4 2
    S 4 2
    W 6 1
    E 2 1
    N 4 12
    N 4 2
    E 6 5
    S 1 5
    S 1 7
    W 2 4
    W 1 1
    N 7 8
    
    Expected output
    12
    
  7. Example 7

    Input
    4 9 3 15 4
    4 3 6 5 6 3 2 4 1
    3 4 3 6 4 6 5 2 5
    1 6 5 2 4 3 2 3 2
    1 5 2 1 5 6 3 6 4
    W 1 1
    E 1 4
    N 2 6
    N 3 5
    S 9 2
    W 2 5
    W 4 5
    N 8 4
    N 9 1
    W 2 7
    S 6 3
    N 5 1
    S 4 3
    S 6 5
    E 1 4
    
    Expected output
    50