Given a grid with each cell consisting of positive, negative or no points i.e, zero points. We can move across a cell only if we have positive points ( > 0 ). Whenever we pass through a cell, points in that cell are added to our overall points. We need to find minimum initial points to reach cell (m-1, n-1) from (0, 0) by following these certain set of rules :

1.From a cell (i, j) we can move to (i+1, j) or (i, j+1).

2.We cannot move from (i, j) if your overall points at (i, j) is <= 0.

3.We have to reach at (n-1, m-1) with minimum positive points i.e., > 0.

Example:

Input: points[m][n] = { {-2, -3, 3},

{-5, -10, 1},

{10, 30, -5}

};

Output: 7

Explanation:

7 is the minimum value to reach destination with

positive throughout the path. Below is the path.

(0,0) -> (0,1) -> (0,2) -> (1, 2) -> (2, 2)

We start from (0, 0) with 7, we reach(0, 1)

with 5, (0, 2) with 2, (1, 2) with 5, (2, 2)

with and finally we have 1 point (we needed

greater than 0 points at the end).

**Input:**

The first line contains an integer 'T' denoting the total number of test cases.

In each test cases, the first line contains two integer 'R' and 'C' denoting the number of rows and column of array.

The second line contains the value of the array i.e the grid, in a single line separated by spaces in row major order.

**Output:**

Print the minimum initial points to reach the bottom right most cell in a separate line.

**Constraints:**

1 ≤ T ≤ 30

1 ≤ R,C ≤ 10

-30 ≤ A[R][C] ≤ 30

**Example:**

Input:

1

3 3

-2 -3 3 -5 -10 1 10 30 -5

Output:

7

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

murli01 | 343 |

decoder_101 | 216 |

Sulagna | 189 |

kya_bolti_public | 172 |

madhursengar24 | 172 |

PiyushPandey4 | 717 |

john_wick | 681 |

ASWATHAMA | 565 |

akhyasharma01 | 547 |

jagrit_ | 507 |

blackshadows | 5362 |

Ibrahim Nash | 5242 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4598 |

Login to report an issue on this page.