

0 -> The cell is empty and Ninja can pass through it.
‘X’ -> The cell contains a tree with a height equal to ‘X’ where ‘X’ > 0 and Ninja can pass through it.
-1 -> The cell can not be visited by Ninja.
If ‘FOREST’ is :
[1,2,0]
[-1,5,-1]
[0,6,7]
We can see we need 4 steps to cut down all trees as shown below:
.png)
So, the output will be 4.
The first line of input contains an integer ‘T’ denoting the number of test cases. Then each test case follows.
The first line of each test case contains two single space-separated integers ‘N’ and ‘M’ representing the number of rows and columns in ‘FOREST’.
The next ‘N’ lines of each test case contain ‘M’ single space-separated integers denoting the values of ‘FOREST’.
For each test case, print a single line containing a singe integer representing the minimum number of steps to cut down all the trees.
The output of every test case will be printed in a separate line.
You don’t have to print anything, it has already been taken care of. Just implement the given function.
1 <= T <=10
1 <= N, M <= 50
-1 <= FOREST[i] <= 10 ^ 5
Where ‘T’ denotes the number of test cases, ‘N’ denotes the number of rows and ‘M’ denotes the number of columns of ‘FOREST’.
Time limit: 1 second
The idea is to first find all the trees and their coordinates and then sort the trees according to their height. Then we just need to find the steps needed between two consecutive trees. We will use Breadth-First-Search algorithm to find the shortest path between two trees.
Complete Algorithm: