Problem E
Mine the Gradient
If we need to be prepared for the possible danger of an alien invasion, the first thing to do is to find out where can they come from. One way to determine if a planet is inhabited by intelligent creatures is to study high resolution pictures of planets trying to find typical characteristics of effects of an intelligent life. There are so many habitable planets that a computer program must be written for this task. One distinctive feature of a colonized planet is the presence of surface mines. An alien mine is a square structure with a depth uniformly decreasing from one of the sides of the square towards the opposite.
Your task is to find the largest mine on a provided bitmap image of a planet. The picture is a rectangular grid of numbers between $0$ and $65\ 535$, representing the shades of grey. Mines will appear as squares of either the same shade or with shade levels gradually and uniformly changing from one side to another. See the above figure for an example. Your program will consider only square mines in some special orientations.
An axis-parallel square is the set of pixels $(i, j)$ such that $c_1 \leq i \leq c_2$ and $r_1 \leq j \leq r_2$ for some $c_1$, $c_2$, $r_1$, $r_2$, $c_2 - c_1 = r_2 - r_1$.
A vertically (horizontally) oriented mine is an axis-parallel square for which there exist integers $S$ and $K$ such that the shade of every pixel $(i, j)$ of the square is equal to $S + iK$ ($S + jK$ for horizontally oriented mines).
A diagonally oriented mine is an axis-parallel square for which there exist integers $Q \in \{ 1, -1\} $, $S$ and $K$ such that the shade of every pixel $(i, j)$ of the square is equal to $S + (i + Qj)K$.
Input
The input contains at most $5\ 000$ descriptions of pictures. The first line of each description contains two numbers $N$ and $M$ ($1 \leq N, M \leq 2\ 000$), the height and width of the picture. The following $N$ lines contain $M$ space-separated integers each. The $j$-th number in the $i$-th row $A_{i,j}$ ($0 \leq A_{i,j} \leq 65\ 535$) describes the shade of grey of the pixel in the $i$-th row and $j$-th column of the picture bitmap. The total number of pixels from all the pictures will be at most $4\ 000\ 000$.
The last description is followed by a line containing two zeros.
Output
For each picture, output the area (number of pixels inside) of the largest horizontal, vertical, or diagonal mine.
Sample Input 1 | Sample Output 1 |
---|---|
4 4 10 1 13 20 18 9 11 13 5 7 9 6 6 5 7 7 3 3 10 1 13 18 9 11 5 1000 9 4 4 10 12 15 20 5 9 13 10 5 9 13 6 5 9 13 7 0 0 |
4 1 9 |