#K95852. Minimum Path Sum in a Matrix

    ID: 38955 Type: Default 1000ms 256MiB

Minimum Path Sum in a Matrix

Minimum Path Sum in a Matrix

You are given an n x m matrix filled with non-negative integers. Your task is to find a path from the top-left corner to the bottom-right corner which minimizes the sum of all numbers along its path.

You can only move either down or right at any point in time.

The recurrence relation for the dynamic programming solution is given by:

\( dp[i][j] = \min(dp[i-1][j],\, dp[i][j-1]) + matrix[i][j] \)

Note: The path always starts at the top-left cell and ends at the bottom-right cell.

inputFormat

The first line contains two integers n and m --- the number of rows and columns of the matrix.

The following n lines each contain m integers separated by spaces, representing the matrix.

outputFormat

Output a single integer --- the minimum path sum from the top-left corner to the bottom-right corner.

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

</p>