This page is still under construction.

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

Mine the Gradient

Time limit10sMemory limit128 MB

Summary
Given a grayscale grid, find the largest square subgrid whose values follow a vertical, horizontal, or diagonal uniform gradient, and report its area.
Level

Hard9 of 10

Topics
Dynamic programming, Implementation, Array, Matrix
Solved
No attempts yet

Problem

To prepare for a possible alien invasion, we first have to find out where the aliens might come from. One way to tell whether a planet is inhabited by intelligent creatures is to study high-resolution pictures of the planet and look for the tell-tale effects of intelligent life. Because there are so many candidate planets, this search has to be done by a computer program.

One distinctive feature of a colonized planet is the presence of surface mines. A mine is a square structure whose depth decreases uniformly from one side of the square toward the opposite side. On a grayscale image it therefore shows up as a square whose shade is either constant or changes gradually and uniformly from one side to another.

Your task is to find the largest mine on a grayscale bitmap of a planet. The picture is a rectangular grid of integers between 00 and 6553565535, where Ai,jA_{i,j} is the shade of grey of the pixel in row ii and column jj. Only square mines in a few special orientations are considered.

An axis-parallel square of side LL is the block of pixels Ai,jA_{i,j} with r≤i≤r+L−1r \le i \le r+L-1 and c≤j≤c+L−1c \le j \le c+L-1.

Such a square is a mine if there exist integers SS and KK for which its shades follow one of the following four gradient patterns:

  • row gradient (vertical mine): Ai,j=S+iKA_{i,j} = S + iK — the shade depends only on the row;
  • column gradient (horizontal mine): Ai,j=S+jKA_{i,j} = S + jK — the shade depends only on the column;
  • diagonal gradient: Ai,j=S+(i+Qj)KA_{i,j} = S + (i + Qj)K for some Q∈{+1,−1}Q \in \{+1, -1\} — the shade stays constant along one of the two diagonal directions.

A single pixel is always a (degenerate) mine.

Input

The input contains several pictures. Each picture begins with a line containing two integers NN and MM (1≤N,M≤20001 \le N, M \le 2000), the height and the width of the picture. The next NN lines contain MM integers each: the jj-th integer on the ii-th of these lines is Ai,jA_{i,j} (0≤Ai,j≤655350 \le A_{i,j} \le 65535), the shade of the pixel in row ii and column jj. Integers on a line are separated by at least one space; there may be extra spaces between the numbers and also at the beginning or the end of a line.

The list of pictures ends with a line containing two zeros, which is not a picture and must not be processed.

Output

For each picture, output a single line containing one integer: the area (the number of pixels) of the largest row-gradient, column-gradient, or diagonal mine in that picture.

Examples4

  1. Example 1

    Input
    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
    
    Expected output
    4
    1
    9
    
  2. Example 2

    Input
    3 3
    7 7 7
    7 7 7
    7 7 7
    0 0
    
    Expected output
    9
    
  3. Example 3

    Input
    4 4
    2 2 2 2
    5 5 5 5
    8 8 8 8
    11 11 11 11
    0 0
    
    Expected output
    16
    
  4. Example 4

    Input
    3 3
    0 1 2
    1 2 3
    2 3 4
    0 0
    
    Expected output
    9