This page is still under construction.

Parts of this page are still being built. What you see may change.

Hole in the Wall

Time limit3sMemory limit256 MB

Summary
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 N×NN \times N is built from N2/2N^2/2 bricks that fit tightly against each other, and NN is even. Every brick has size 2×12 \times 1 and the bricks are numbered from 1 to N2/2N^2/2. Some bricks lie horizontally, the rest lie vertically. The wall has no gaps, so every cell of the N×NN \times N 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.

  1. Its sides are parallel to the sides of the wall.
  2. The hole touches no side of the wall, so it lies strictly inside the wall.
  3. 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 NN, the length of a side of the wall. Each of the next NN lines contains NN 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 NN from top to bottom and columns 1 to NN from left to right, so the upper left cell of the wall is (1,1)(1, 1).

Several rectangles can reach the maximum area. Print the one whose quadruple (r1,c1,r2,c2)(r_1, c_1, r_2, c_2) is lexicographically smallest: take the smallest r1r_1, among those the smallest c1c_1, then the smallest r2r_2, then the smallest c2c_2.

If the wall admits no legal hole, print 0 0 0 0 0.

Constraints

  • 4≤N≤10004 \le N \le 1000, and NN is even.
  • Every number from 1 to N2/2N^2/2 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.

Examples4

  1. Example 1

    Input
    6
    1 1 4 4 13 14
    2 3 3 5 13 14
    2 6 7 5 12 12
    9 6 7 10 10 15
    9 8 8 11 11 15
    16 16 17 17 18 18
    
    Expected output
    8 2 2 5 3
    
  2. Example 2

    Input
    4
    1 1 2 2
    3 4 5 6
    3 4 5 6
    7 7 8 8
    
    Expected output
    4 2 2 3 3
    
  3. Example 3

    Input
    4
    1 3 4 6
    1 3 4 6
    2 5 7 8
    2 5 7 8
    
    Expected output
    0 0 0 0 0
    
  4. Example 4

    Input
    6
    5 5 8 2 4 4
    3 3 8 2 7 7
    15 6 10 10 14 14
    15 6 16 16 1 18
    9 9 17 17 1 18
    12 12 13 13 11 11
    
    Expected output
    6 3 2 4 4