#K48237. Minimum Path Sum in Grid

    ID: 28376 Type: Default 1000ms 256MiB

Minimum Path Sum in Grid

Minimum Path Sum in Grid

You are given an n x m grid where each cell contains a non-negative integer representing the cost to step on that cell. Your task is to find the minimum cost path from the top-left corner to the bottom-right corner of the grid. You can only move either right or down at any point in time. Formally, if the grid is denoted by \( grid[i][j] \), you need to compute the minimum cost \( C \) satisfying:

\[ C = grid[0][0] + \min_{path}(\sum_{(i,j) \in path} grid[i][j]) \]

where the path starts at \( (0,0) \) and ends at \( (n-1, m-1) \). For example, given the grid:

1 3 1
1 5 1
4 2 1

the minimum cost path has a cost of 7.

inputFormat

The first line of input contains an integer t, the number of test cases. For each test case, the first line contains two integers n and m, representing the number of rows and columns of the grid. This is followed by n lines, each containing m space-separated integers that represent the grid's cells.

outputFormat

For each test case, output a single line containing one integer: the minimum cost required to reach the bottom-right corner from the top-left corner.

## sample
1
3 3
1 3 1
1 5 1
4 2 1
7

</p>