#C2703. Maximum Crops Harvested

    ID: 46049 Type: Default 1000ms 256MiB

Maximum Crops Harvested

Maximum Crops Harvested

You are given a grid of non-negative integers representing the amount of crops in each cell. Starting at the top-left corner (0,0), you need to move to the bottom-right corner of the grid collecting crops on the way. You may only move right or down. The goal is to determine the maximum total amount of crops that can be harvested by the time you reach the bottom-right corner.

The problem can be solved via dynamic programming. Let \(dp[i][j]\) denote the maximum crop amount that can be collected when reaching cell \((i, j)\). The recurrence relation is given by:

\( dp[i][j] = \max(dp[i-1][j],\ dp[i][j-1]) + grid[i][j] \)

with the starting condition \(dp[0][0] = grid[0][0]\). You are required to process multiple test cases.

inputFormat

The first line contains an integer \(T\) representing the number of test cases.

For each test case, the first line contains two integers \(n\) and \(m\) which denote the number of rows and columns of the grid. This is followed by \(n\) lines, each containing \(m\) integers representing the cells of the grid.

outputFormat

For each test case, output a single line containing the maximum total crops harvested from the top-left to the bottom-right corner.

## sample
3
2 3
1 2 3
0 1 4
3 3
1 2 3
4 5 6
7 8 9
1 1
5
10

29 5

</p>