This page is still under construction.

Parts of this page are still being built. What you see may change.

Maximum Damage

Time limit1sMemory limit512 MB

Summary
On a grid with obstacles, orcs, and blank cells, pick at most T blank cells as stations so the total number of orcs within Manhattan distance R is maximized.
Level

Medium5 of 10

Topics
Prefix sum, Brute force, Matrix
Solved
No attempts yet

Problem

The orcs and the elves have been at war for a long time, and the fighting has reached a decisive moment. The elves have located every orc camp and marked it on a map of the battlefield. To simplify their planning, the elves lay a coordinate grid over the battlefield and treat each unit square as a single point with integer coordinates, so every orc camp is described by a pair of integers (its x- and y-coordinates).

The elves want to build a limited number of Elf stations to strike the camps. Each station deals 1 unit of damage to every orc camp within distance RR of it, so the total damage a single station inflicts equals the number of orc camps within distance RR.

Because the elves' cavalry rides along horizontal and vertical trails laid out in a grid, distance is measured with the Manhattan metric rather than straight-line distance. For two points P1P_1 and P2P_2,

D(P1,P2)=∣P1.x−P2.x∣+∣P1.y−P2.y∣.D(P_1, P_2) = |P_1.x - P_2.x| + |P_1.y - P_2.y|.

Some parts of the battlefield are too rugged to build on; the elves call such cells obstacles. The elves want to decide where to build a given number TT of Elf stations so as to inflict the maximum possible total damage on the orc camps.

The battlefield is a rectangular map in which every cell is one of three characters:

  • * — a blank cell, where an Elf station may be built;
  • O — an orc camp;
  • X — an obstacle, where no Elf station may be built.

An Elf station may be built only on a blank (*) cell, and each blank cell can hold at most one station. A station's damage is the number of orc camps whose Manhattan distance to it is at most RR. You place at most TT stations (fewer if there are fewer than TT blank cells) so as to maximize the sum of their damage. A legal battlefield map looks like this:

**O**
*OXO*
*****
**O**

Input

The first line contains the number of test cases (fewer than 15). The test cases follow one after another.

Each test case begins with a line of four integers NN, MM, TT, RR: NN and MM are the size of the battlefield along the y- and x-axes (the number of rows and columns), TT is the number of Elf stations you may place, and RR is each station's attack radius. The next NN lines each contain a string of MM characters describing one row of the map.

Output

For each test case, print a single line in the form

Maximum damage = X

where X is the maximum total damage that can be inflicted on the orc camps.

Constraints

  • 1≤N,M≤10001 \le N, M \le 1000
  • 1≤T≤1061 \le T \le 10^6
  • 1≤R≤1081 \le R \le 10^8

Examples1

  1. Example 1

    Input
    2
    3 3 2 1 
    *O*
    OXO
    ***
    4 5 3 2
    **O**
    *OXO*
    *****
    **O**
    
    Expected output
    Maximum damage = 4
    Maximum damage = 8