Window
InterviewTime limit8sMemory limit256 MB
Find the largest interior grid-aligned square whose border runs only along domino edges and report its size and top-left corner.
- Level
Medium5 of 10
- Topics
- Prefix sum, Brute force
- Solved
- No attempts yet
Problem
A square wall of size is filled with bricks of size , packed with no gaps. is even. The bricks are numbered from 1 to . Some of them lie horizontally and the rest lie vertically. The wall has no empty cell. Two cells carrying the same number form one brick.

You cut a square hole in the wall to fit a window. The hole must satisfy all of the following.
- The sides of the hole are parallel to the sides of the wall.
- The hole does not touch any border of the wall, so the whole hole lies inside the wall.
- Cutting the hole must not break a brick, so the border of the hole runs only along brick borders.
Write a program that finds a square hole with the longest side among those that satisfy the conditions. At least one such hole is guaranteed to exist.
Input
The first line contains the side length of the wall. is even and .
Each of the next lines contains integers describing the brick layout. Each integer is between 1 and , every value appears exactly twice, and the two cells with the same value are adjacent.
Output
On the first line print the side length of the square hole, then the row number and the column number of its upper left cell, separated by spaces. The upper left cell of the wall is .
If several holes share the longest side, print the one with the smallest row number. If several of those remain, print the one with the smallest column number among them.
Hint
In the example the longest square hole has side 2. Removing bricks 6 and 7 works, and removing bricks 10 and 11 works as well, but the answer is because its row number is smaller.