#C6504. Longest Increasing Path in a Matrix

    ID: 50272 Type: Default 1000ms 256MiB

Longest Increasing Path in a Matrix

Longest Increasing Path in a Matrix

Given an n x m grid of non-negative integers, find the length of the longest increasing path. From each cell, you can move to one of its 4 neighbors (up, down, left, right), but you can only move to a cell with a strictly greater value than the current one. Formally, if the grid is represented as \(grid_{ij}\), then you need to compute:

\[ \max_{0\leq i<n, 0\leq j<m} L(i,j) \]

where \(L(i, j)\) is the length of the longest increasing path starting at cell \((i, j)\). Your task is to output the length of the longest such path in the grid.

inputFormat

The first line contains two space-separated integers, n and m, representing the number of rows and columns of the grid, respectively.

This is followed by n lines, each containing m space-separated integers, representing the rows of the grid.

outputFormat

Output a single integer, which is the length of the longest increasing path found in the grid. The output should be printed to stdout.

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