Under construction

Still being built. Things may change

Largest Increasing Submatrix

Time limit2sMemory limit512 MB

Summary
Given a matrix, find the largest rectangular submatrix whose row-by-row linearization is strictly increasing.
Level

Hard8 of 10

Topics
Dynamic programming, Matrix, Prefix sum
Solved
No attempts yet

Problem

Finding the longest contiguous increasing subsequence of a list of numbers is a classic programming contest problem. You solve the same problem here, with one change: the numbers are given as a two-dimensional matrix, and the longest sequence is embedded in a submatrix of the original matrix.

Here is the precise definition. The linearization of a two-dimensional matrix is the sequence formed by joining its rows one after another, from the first row to the last. A submatrix is a rectangular region of a matrix whose sides are parallel to the sides of the matrix. The size of a submatrix is its number of elements. Write a program that, given a matrix of integers, finds the largest submatrix whose linearization is an increasing sequence.

The figure below shows some examples of maximum-size submatrices that contain increasing sequences. A matrix can contain more than one submatrix with a sequence of maximum length. An increasing sequence cannot contain repeated elements: 22, 31, 33 is increasing, but 22, 31, 31, 33 is not.

For the three matrices in the figure, the answers are 4, 3, and 9.

Input

The input contains several test cases. The first line of each test case contains two integers NN and MM, the dimensions of the matrix (1≤N,M≤6001 \le N, M \le 600). Each of the next NN lines contains MM integers separated by single spaces, the elements of the matrix. The element Xi,jX_{i,j} is the jj-th integer on the ii-th of these lines (−106≤Xi,j≤106-10^6 \le X_{i,j} \le 10^6).

The end of the input is marked by a line that contains only two zeros separated by a space.

Output

For each test case, print one line with the number of elements in the largest submatrix whose linearization is an increasing sequence.

Examples1

  1. Example 1

    Input
    3 3
    1 2 5
    4 6 7
    10 8 3
    3 4
    1 2 1 2
    9 6 7 3
    8 7 2 8
    4 2
    -23 -12
    0 2
    16 15
    57 33
    4 4
    2 2 2 2
    2 2 2 2
    2 2 2 2
    2 2 2 2
    0 0
    
    Expected output
    4
    3
    4
    1