```
def minPathSum(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
m, n = len(grid), len(grid[0])
for j in range(1, n):
grid[0][j] += grid[0][j-1]
for i in range(1, m):
grid[0][0] += grid[i][0]
for j in range(1, n):
grid[0][j] = min(grid[0][j-1],grid[0][j]) + grid[i][j]
return grid[0][-1]
```