Making Chess Boards (Small)

Time limit5sMemory limit512 MB

Summary
You repeatedly cut the largest alternating-color square from a hex-encoded grid and report the count for each board size.
Level

Medium5 of 10

Topics
Simulation, Brute force
Solved
No attempts yet

Problem

Chess boards are made from the bark of a very rare tree. The bark is stripped off and unrolled into one large rectangular grid of black and white cells.

Your task is to cut square chess boards out of that bark, as large as possible. A chess board is a square piece of the bark whose sides are parallel to the sides of the bark rectangle and whose cells alternate in color, so two cells that share an edge always have different colors.

Every cut takes the largest chess board that can still be formed from the bark that is left. Cells that have already been cut out are gone from the bark, so a later chess board cannot use them. If several chess boards of that largest size are available, take the topmost one, and if several of those are still tied, take the leftmost one. Repeat until no bark is left. You may need to go as far as cutting out 1×11 \times 1 chess boards.

The picture below shows one sheet of bark and the first few chess boards cut out of it.

Input

The first line contains the number of test cases TT. The first line of each test case contains the dimensions of the bark grid, MM and NN. The next MM lines each contain a hexadecimal integer of N/4N/4 digits that represents one row of the grid. In binary that integer gives NN bits, one bit per cell. A 0 is a black cell and a 1 is a white cell. Rows are given from top to bottom, and inside a row the most significant bit of the hexadecimal integer is the leftmost cell.

Limits

  • 1≤T≤1001 \le T \le 100
  • 1≤M≤321 \le M \le 32
  • 1≤N≤321 \le N \le 32, and NN is divisible by 4.
  • Each hexadecimal integer has exactly N/4N/4 digits and uses only the digits 0 to 9 and the uppercase letters A to F.

Output

For each test case, print one line in the form "Case #x: KK", where x is the case number starting from 1 and KK is the number of different chess board sizes produced by the procedure above. Then print KK lines, each with two integers: a chess board size and how many chess boards of that size were cut out. Print the sizes from the largest to the smallest.

Hint

The first test case of example 1 is the bark shown in the picture above.

Examples2

  1. Example 1

    Input
    4
    15 20
    55555
    FFAAA
    2AAD5
    D552A
    2AAD5
    D542A
    4AD4D
    B52B2
    52AAD
    AD552
    AA52D
    AAAAA
    5AA55
    A55AA
    5AA55
    4 4
    0
    0
    0
    0
    4 4
    3
    3
    C
    C
    4 4
    6
    9
    9
    6
    
    Expected output
    Case #1: 5
    6 2
    4 3
    3 7
    2 15
    1 57
    Case #2: 1
    1 16
    Case #3: 2
    2 1
    1 12
    Case #4: 1
    2 4
    
  2. Example 2

    Input
    3
    1 4
    F
    4 4
    5
    A
    5
    A
    2 4
    5
    5
    
    Expected output
    Case #1: 1
    1 4
    Case #2: 1
    4 1
    Case #3: 1
    1 8