#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param matrix int整型二维数组 the matrix
# @return int整型
#
class Solution:
    def minPathSum(self , matrix: List[List[int]]) -> int:
        # write code here
        n,m = len(matrix),len(matrix[0])
        dp = [[0 for j in range(m)] for i in range(n)]

        for i in range(n-1,-1,-1):
            for j in range(m-1,-1,-1):
                if i==n-1 and j==m-1:
                    dp[i][j] = matrix[i][j]
                    continue
                down= right = 9999999
                if i<n-1:
                    down = dp[i+1][j]
                if j<m-1:
                    right = dp[i][j+1]
                dp[i][j] = min(down,right) + matrix[i][j]
                #print(i,j,dp[i][j])
        
        return dp[0][0]