Under construction

Still being built. Things may change

Ominobox

Time limit10sMemory limit512 MB

Summary
For every fixed N-omino placement in a small grid, the drop score is H minus the maximum stack height covered; sum the best placement score over all N-ominoes.
Level

Hard8 of 10

Topics
Brute force, Implementation, Simulation, Recursion
Solved
No attempts yet

Problem

An evil wizard planted a bomb in the middle of the city and set a timer on it. Next to the bomb he left a keypad and a puzzle. Entering the answer to the puzzle stops the bomb. The puzzle is called Ominobox.

An Ominobox consists of a box holding some unit cubes and a set of pieces that contains every possible N-omino exactly once. You drop the pieces into the box one at a time to score points, and the answer to the Ominobox is the largest total score you can get.

An N-omino is a set of NN unit squares joined along their edges. A 1-omino is a single unit square, and an N-omino is an (N-1)-omino with one more unit square attached along an edge. Two pieces are the same piece when one is a translation of the other, and two pieces that match only after a rotation or a reflection count as different pieces. The figure below shows all six 3-ominoes and some of the nineteen 4-ominoes.

The floor of the box is an R×CR \times C grid and the four walls have height HH. Cell (i,j)(i, j) of the grid holds a stack of HijH_{ij} unit cubes, and the stacked cubes cannot be moved.

You align a piece with the grid, place it above the box and let it fall. Rotating or reflecting a piece is not allowed, and the whole piece must stay inside the floor grid. The piece falls until it touches a stack of cubes or the floor. So if tt is the largest HijH_{ij} among the cells the piece covers, the bottom face of the piece rests at height tt, and the score for that drop is H−tH - t, the distance from the bottom face of the piece to the top rim of the box. After writing the score down you take the piece back out, so the cubes inside the box stay where they are the whole time. A piece that has no place inside the floor grid scores 0.

Compute the answer to the Ominobox, the total score when every piece is dropped in its best place.

Input

The first line contains the number of challenges TT (1≤T≤2001 \le T \le 200).

Then TT challenges follow. The first line of each challenge contains four integers RR, CC, HH and NN (1≤R,C,H≤301 \le R, C, H \le 30, 1≤N≤101 \le N \le 10). The floor of the box is an R×CR \times C grid, the walls have height HH, and the pieces are the N-ominoes. Each of the next RR lines contains CC integers. The jj-th integer on the ii-th line is HijH_{ij}, the number of cubes on cell (i,j)(i, j) (0≤Hij≤H0 \le H_{ij} \le H).

Output

For each challenge, print the answer to the Ominobox on its own line.

Examples1

  1. Example 1

    Input
    4
    2 2 3 1
    1 2
    0 3
    2 2 3 2
    1 2
    0 3
    2 2 3 3
    1 2
    0 3
    2 3 5 4
    1 2 5
    0 3 4
    
    Expected output
    3
    3
    1
    5