Housing Complexes

Time limit1sMemory limit128 MB

Summary
Each plot can be cleared only for one owner, and each owner can be cleared on one plot; maximize the number of h by w complexes built.
Level

Medium7 of 10

Topics
Binary search, Greedy, Brute force, Implementation
Solved
No attempts yet

Problem

The Ministry of Housing is planning a huge construction project consisting of several housing complexes. Each complex contains several apartments to be sold to government employees at reasonable prices. The ministry has secured several large plots of land for the project and wants to build exactly one complex on each plot. Every plot is a rectangle divided into m×nm \times n square blocks of size 1×11 \times 1. Every housing complex is an h×wh \times w rectangle that covers exactly h×wh \times w blocks of the plot it is built on.

The difficulty is that each plot originally contains some old buildings — each building occupies exactly one block — so there may not be enough free space to place a complex. The ministry must therefore buy some of these buildings and demolish them to free the required space. The old buildings belong to a number of different owners.

In response to protests, the ministry announced the following "fair" policy: when it buys buildings on a plot, it will choose only buildings that belong to a single owner and buy all of them at a reasonable price; and it promises never to buy buildings belonging to that same owner on any other plot. Because of this constraint, there may be plots on which building a complex is impossible.

In other words, on each plot you may demolish the buildings of at most one owner, and across the whole project the buildings of any single owner may be bought on at most one plot. Determine the maximum number of housing complexes that can be built under these conditions.

Input

The first line contains a single integer tt (1≤t≤101 \le t \le 10), the number of test cases. The data for each test case follows.

The first line of each test case contains five integers kk, mm, nn, hh, and ww: kk (1≤k≤301 \le k \le 30) is the number of plots, mm and nn (1≤m,n≤501 \le m, n \le 50) are the number of rows and columns of each plot, and hh and ww (1≤h,w≤501 \le h, w \le 50) are the number of rows and columns a complex occupies.

The next k×mk \times m lines describe the kk plots, each as an m×nm \times n matrix. Each line is a string of length nn with no leading or trailing spaces. Each character is a block of the plot: an uppercase letter from A to Z denotes the owner of that block, and the character 0 (the digit zero) denotes a free block. The same letter denotes the same owner on every plot.

Output

For each test case, print a single line containing the maximum number of housing complexes that can be built for that test case.

Examples3

  1. Example 1

    Input
    2
    3 4 3 3 2
    A0B
    000
    0A0
    00B
    AA0
    00B
    0B0
    000
    A0A
    000
    B00
    B00
    3 4 3 3 2
    A0B
    000
    0A0
    00B
    AA0
    00B
    0B0
    000
    A0A
    000
    0B0
    B00
    
    Expected output
    3
    2
    
  2. Example 2

    Input
    1
    2 1 2 1 2
    A0
    0B
    
    Expected output
    2
    
  3. Example 3

    Input
    1
    4 1 2 1 2
    A0
    A0
    00
    0B
    
    Expected output
    3