#!/usr/bin/env python3

__author__ = 'Wei Mu'

class Solution:
    def minPathSum(self, grid: 'List[List[int]]') -> 'int':
        matrix = []
        
        matrix = [[None] * len(grid[0]) for i in range(len(grid))]
        
        matrix[0][0] = grid[0][0]
        
        for i in range(1, len(grid)):
            matrix[i][0] = matrix[i-1][0] + grid[i][0]
        for i in range(1, len(grid[0])):
            matrix[0][i] = matrix[0][i-1] + grid[0][i]
        
        for i in range(1, len(grid[0])):
            for j in range(1, len(grid)):
                matrix[j][i] = min(matrix[j-1][i], matrix[j][i-1]) + grid[j][i]
        
        return(matrix[-1][-1])
