Alice in the Hills
Practice
3.1 (9 votes)
Dynamic programming
Algorithms
2d dynamic programming
Data structures
Problem
37% Success 640 Attempts 30 Points 5s Time Limit 256MB Memory 1024 KB Max Code

Alice visited a hill with various mountains. The hill is present in the form of a 2-D square matrix \(A\) where each element denotes the height of the mountain. Alice can start from any mountain and jump to any mountain which lies on the same line as the current mountain, either vertically or horizontally.

A jump can be made only if the destination has a height smaller or equal to the current mountain he is standing upon. Alice wonders what is the maximum number of mountains he can visit jumping in the hills if he is allowed to start from any mountain.

Input

The first line of each test case contains two space-separated integers \(M\) and \(N\) — the number of rows and columns in the grid respectively.

The next \(M\) lines contain \(N\) space-separated integers, which are the entries in each row.

Output

Return a single integer denoting the maximum number of mountains he can visit in the hills in a new line.

Problem Constraints

\(1 \leq N,M \leq 10^3\)

\(1 \leq A[i][j] \leq 10^9\)

\(N = M\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
116 votes
Tags:
ApprovedGeometryMediumOpenTwo dimensional
Points:30
187 votes
Tags:
Ad-HocAlgorithmsApprovedDynamic ProgrammingMediumOpen
Points:30
25 votes
Tags:
AlgorithmsApprovedDynamic ProgrammingMediumOpenTwo dimensional