#K93277. Minimum Path Cost in a Grid

    ID: 38384 Type: Default 1000ms 256MiB

Minimum Path Cost in a Grid

Minimum Path Cost in a Grid

You are given a grid of positive integers with m rows and n columns. Your task is to determine the minimum path cost to travel from the top-left cell to the bottom-right cell. You can only move either to the right or down at any step.

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

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

with the starting condition:

$$dp[0][0] = grid[0][0]$$

Calculate and output the value of dp[m-1][n-1].

inputFormat

The first line of input contains two space-separated integers m and n, representing the number of rows and columns respectively. This is followed by m lines, each containing n space-separated integers representing the grid.

outputFormat

Output a single integer which is the minimum path cost from the top-left to the bottom-right of the grid.

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