


Consider 0 based indexing.
The first line of input contains an integer ‘T’ denoting the number of test cases. The description of ‘T’ test cases are as follows -:
The first line contains two space-separated positive integers ‘N’, ‘M’ representing the number of rows and columns respectively.
Each of the next ‘N’ lines contains ‘M’ space-separated integers that give the description of the matrix ‘mat’.
For each test case, print the length of the longest path in the matrix.
Print the output of each test case in a separate line.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 50
1 <= N <= 100
1 <= M <= 100
-10^9 <= mat[i][j] <= 10^9
Time Limit: 1 sec
Algorithm
In the recursive approach, we can observe that we are doing a lot of repeated work here. To avoid doing repeated work we can memoize the already computed result in a table.
Algorithm
The idea is to use dynamic programming. Maintain the 2D matrix ‘dp’ of size n*m, where dp[i][j] stores the length of the longest path ending at the ith row and jth column i.e at mat[i][j].
Algorithm