Under construction

Still being built. Things may change

Longest Increasing Subsequence on a Grid Path

Time limit1sMemory limit1024 MB

Summary
Find the longest increasing subsequence possible among all shortest top-left to bottom-right paths in an N by N grid of integers.
Level

Medium7 of 10

Topics
Dynamic programming, Matrix
Solved
No attempts yet

Problem

Jeonghwi wants to move from the top-left cell to the bottom-right cell of an N×NN\times N array of integers along a shortest path. He can only move between cells that share an edge.

While moving, Jeonghwi collects the integers he passes, in order, and gives them to Seonghyeon as a gift. Seonghyeon likes sequences with a long increasing subsequence, so Jeonghwi wants to build a sequence whose longest increasing subsequence is as long as possible.

Jeonghwi has no time to find the path because he is writing a problem. Given the N×NN\times N array, find the maximum possible length of the longest increasing subsequence among all sequences he can build.

Input

The first line contains NN (1≤N≤1001 \leq N \leq 100).

Each of the next NN lines contains NN integers separated by spaces. Each integer is between 11 and 10 00010\,000, inclusive.

Output

Print the maximum possible length of the longest increasing subsequence.

Hints

  • Python users are recommended to submit with PyPy.
  • A subsequence is a sequence obtained by picking one or more elements from the given sequence and listing them in their original order.
  • An increasing subsequence is a sequence in which every element except the first is greater than the element right before it. In other words, for a subsequence AA of length NN, if Ai−1<AiA_{i-1} < A_i holds for every 2≤i≤N2 \leq i \leq N, then AA is an increasing subsequence.
  • The longest increasing subsequence is the increasing subsequence of a given sequence that has the greatest length.

Examples2

  1. Example 1

    Input
    3
    1 2 3
    3 2 1
    3 4 5
    
    Expected output
    4
    
  2. Example 2

    Input
    3
    1 1 1
    1 1 1
    1 1 1
    
    Expected output
    1