

Ninja lives in a beautiful city known as Byteland. A grand festive event is going to be organised in the city. To make place for the event, King has ordered Ninja to clear the nearby forest. The forest can be represented in the form of ‘N’*‘M’ grid ‘FOREST’, where each cell of ‘FOREST’ can have one of the possible values:
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.
Ninja is present at the top left corner of ‘FOREST’ i.e. at cell(0,0) and he has to cut down all the trees in ‘FOREST’. In one step ninja can move to any one of the four directions: East, West, North, South.
There is a rule that Ninja must cut off the trees in order from shortest to tallest and after cutting a tree, the value of the cell will become 0.
While standing on a cell with a tree, Ninja always has a choice to cut the tree or pass through the cell. It is guaranteed that no two cells will have the same height and there will always be at least one tree that need to be cut off.
Your task is to help Ninja to cut down all trees in ‘FOREST’ with minimum steps and return the number of steps.
If there is at least one tree that can not be cut off then return -1.
For example :
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.
Input Format:
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’.
Output format:
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.
Note :
You don’t have to print anything, it has already been taken care of. Just implement the given function.
Constraints
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
1
3 3
5 0 7
3 2 -1
6 -1 1
-1
The forest can be represented as:
.png)
There is no way such that we can cut off all the trees in ‘FOREST’, so we will return -1.
2
3 3
0 5 6
2 3 7
-1 -1 8
1 1
8
6
0
For test case 1:
We can see from the below image that we need 6 steps to cut off all trees.
.png)
For test case 2:
The forest has only 1 tree at (0,0) and Ninja is also at (0,0), so he can cut off the tree without using any moves.
Can you think of an algorithm that helps us find the shortest path between two points?
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:
O((N * M) ^ 2), where ‘N’ is the number of rows and ‘M’ is the number of columns in ‘FOREST’.
In the worst-case scenario for all N * M trees, we iterate N * M times to find the distance.
O(N * M), where ‘N’ is the number of rows and ‘M’ is the number of columns in ‘FOREST’.
This is the space used to store coordinates of all N * M trees.