A recursive solution is a natural fit for this problem but the following solution times out. Could somebody improve it.

```
class Solution {
private int mps(int[][]grid, int r, int c) {
int top = r==0 ? Integer.MAX_VALUE : mps(grid, r-1, c);
int left = c==0 ? Integer.MAX_VALUE : mps(grid, r, c-1);
return grid[r][c] + (r > 0 || c > 0 ? Math.min(top, left) : 0);
}
public int minPathSum(int[][] grid) {
return mps(grid, grid.length-1, grid[0].length-1);
}
}
```