Selling Land

Time limit1sMemory limit128 MB

Summary
For every grid cell (as a rectangle's bottom-right corner), find the maximum perimeter of an all-grass rectangle ending there, then output counts grouped by perimeter.
Level

Hard8 of 10

Topics
Dynamic programming, Array, Matrix
Solved
No attempts yet

Problem

The country of Absurdistan is divided into unit squares, each of which is either grass or swamp. Land can only be bought as a rectangular block whose sides are aligned with the grid, because the local bureaucrats cannot handle any other shape. The price of a block equals its perimeter: the bureaucrats cannot multiply, so they charge for the perimeter rather than the area.

Per owns one rectangular parcel and wants to sell it off, possibly in many overlapping pieces. Whenever he sells a rectangular block, the bureaucrats record only the coordinates of its south-east (bottom-right) corner. They refuse a sale if a block with the same south-east corner has already been sold. Otherwise overlapping blocks are allowed, so Per may sell one block for every distinct south-east corner. No buyer will accept a block that contains any swamp square, so every block he sells must consist entirely of grass.

To make as much money as possible, for each possible south-east corner Per sells the all-grass rectangular block that has that corner and the largest possible perimeter. For example, a block that is 22 wide and 44 tall has perimeter 2×(2+4)=122\times(2+4)=12. Over all the blocks he sells, determine how many blocks he sells of each perimeter.

Input

The first line contains an integer TT (1≤T≤1001 \le T \le 100), the number of test cases. Each test case is given as follows:

  • One line with two integers nn and mm (1≤n,m≤10001 \le n, m \le 1000): the number of rows and columns of Per's parcel.
  • nn lines follow, each containing mm characters. Each character is either # (swamp) or . (grass). The character in row ii, column jj describes the square at position (i,j)(i, j); the north-west corner of the parcel is (1,1)(1, 1) and the south-east corner is (n,m)(n, m).

Output

For each test case, output zero or more lines describing how many blocks of each perimeter Per sells in the optimal plan. If he sells pip_i blocks of perimeter ii, print one line of the form count x perimeter (the count, a space, the letter x, a space, then the perimeter). Sort the lines by increasing perimeter ii, print no two lines with the same ii, and omit any perimeter for which pi=0p_i = 0.

Examples3

  1. Example 1

    Input
    1
    6 5
    ..#.#
    .#...
    #..##
    ...#.
    #....
    #..#.
    
    Expected output
    6 x 4
    5 x 6
    5 x 8
    3 x 10
    1 x 12
    
  2. Example 2

    Input
    1
    1 1
    .
    
    Expected output
    1 x 4
    
  3. Example 3

    Input
    1
    1 5
    .....
    
    Expected output
    1 x 4
    1 x 6
    1 x 8
    1 x 10
    1 x 12