```
class Solution:
# @param grid, a list of lists of integers
# @return an integer
def minPathSum(self, grid):
n = len(grid)
m = len(grid[0])
for i in range(1, n):
grid[i][0] = grid[i-1][0] + grid[i][0]
for i in range(1, m):
grid[0][i] = grid[0][i-1] + grid[0][i]
for i in range(1, m):
for j in range(1, n):
grid[j][i] = min(grid[j][i-1], grid[j-1][i])+grid[j][i]
return grid[n-1][m-1]
```