Gold Leaf
Time limit5sMemory limit256 MB
Locate the single horizontal, vertical, or 45-degree fold that explains the observed gold and bare paper pixels.
- Level
Medium6 of 10
- Topics
- Brute force, Simulation, Matrix
- Solved
- No attempts yet
Problem
Gold leaf is a very thin layer of gold with a paper backing. If the paper gets folded and then unfolded, the gold sticks to itself more readily than it sticks to the paper, so patches of gold and patches of bare paper appear. The gold always ends up on one side of the fold, never on both.
In the picture below, the paper was folded along the dashed line.

You are given a crude digital image of a sheet of gold leaf. A pixel whose area is mostly gold is written as #, and a pixel whose area is mostly bare paper is written as .. Find where the sheet was folded.
The sheet was folded exactly once, along a horizontal line, a vertical line, or a 45 degree diagonal line. A horizontal or vertical fold always runs between two rows or between two columns. A diagonal fold runs through a diagonal line of cells, and every cell on the fold is #. Of two cells that meet each other when the paper is folded, exactly one is #, and a cell that meets no other cell keeps its gold and is #.
Input
The first line contains the number of test cases ().
Each test case starts with a line containing two integers and (), where is the number of rows and is the number of columns of the photograph. Each of the next lines contains exactly characters, each of which is # or .. At least one . appears, and a fold that explains the image always exists.
Output
For each test case, print four integers on one line, giving the two places where the fold hits the edges of the paper. Here is a row and is a column, the top left character is , and the bottom right character is .
For a horizontal or diagonal fold, print the left coordinates before the right ones. For a vertical fold, print the top coordinates before the bottom ones. For a horizontal fold, use the coordinates above the fold. For a vertical fold, use the coordinates to the left of the fold. For a diagonal fold, use the coordinates of the cells that the fold runs through. If more than one fold is possible, choose the one with the smallest first coordinate, then the smallest second coordinate, then the third, then the fourth.