#K6301. Longest Monotonically Increasing Sequence in a Grid

    ID: 31658 Type: Default 1000ms 256MiB

Longest Monotonically Increasing Sequence in a Grid

Longest Monotonically Increasing Sequence in a Grid

You are given a square grid of integers. Your task is to determine the length of the longest contiguous subsequence where each element is strictly greater than its previous element. The subsequence can come from any row, any column, or any diagonal (both main and anti-diagonals) of the grid.

More formally, given a grid \(G\) of size \(n \times n\), find the maximum \(L\) such that there exists a contiguous sequence \(a_1, a_2, \dots, a_L\) from one of the rows, columns, or diagonals satisfying:

[ a_{i+1} > a_i \quad \text{for all } 1 \le i < L ]

If no increasing sequence exists, the answer is 1 (since any single number is trivially an increasing sequence).

inputFormat

The input is read from stdin and consists of:

  • The first line contains an integer \(n\) denoting the size of the grid.
  • The next \(n\) lines each contain \(n\) space-separated integers representing the grid.

outputFormat

Print to stdout a single integer: the length of the longest strictly increasing contiguous subsequence found in any row, column, or diagonal of the grid.

## sample
3
1 2 3
4 5 6
7 8 9
3