Hole in the Wall
Time limit3sMemory limit256 MB
Find the largest strictly interior rectangle whose border runs only along domino edges, breaking area ties by lexicographic coordinates.
- Level
Medium6 of 10
- Topics
- Matrix, Brute force
- Solved
- No attempts yet
Problem
A square wall of size is built from bricks that fit tightly against each other, and is even. Every brick has size and the bricks are numbered from 1 to . Some bricks lie horizontally, the rest lie vertically. The wall has no gaps, so every cell of the square is covered by exactly one brick. In the picture below, two cells that carry the same number are the two halves of one brick.

A rectangular hole has to be cut into the wall to fit a window. The hole must meet three requirements.
- Its sides are parallel to the sides of the wall.
- The hole touches no side of the wall, so it lies strictly inside the wall.
- No brick is cut, so the border of the hole runs only along borders of bricks.
Find a hole of maximum area.
Input
The first line contains the integer , the length of a side of the wall. Each of the next lines contains integers, the brick numbers of the cells of that row from left to right. Two cells carry the same number exactly when they belong to the same brick.
Output
Print five integers separated by single spaces: the area of a largest legal hole, the row and the column of its upper left cell, and the row and the column of its lower right cell. Rows are numbered 1 to from top to bottom and columns 1 to from left to right, so the upper left cell of the wall is .
Several rectangles can reach the maximum area. Print the one whose quadruple is lexicographically smallest: take the smallest , among those the smallest , then the smallest , then the smallest .
If the wall admits no legal hole, print 0 0 0 0 0.
Constraints
- , and is even.
- Every number from 1 to appears on exactly two cells, and those two cells share a side.
Note
In the first example the largest hole has area 8. It is cut by removing the bricks numbered 3, 6, 7 and 8.