import java.util.*;
public class Solution {
public int minPathSum(int[][] matrix) {
int row = matrix.length;
int cow = matrix[0].length;
int[][] dp = new int[row + 1][cow + 1];
for (int i = 1; i <= row ; i++) {
for (int j = 1; j <= cow; j++) {
if (i == 1) {
dp[i][j] = dp[i][j - 1] + matrix[i - 1][j - 1];
} else if (j == 1) {
dp[i][j] = dp[i - 1][j] + matrix[i - 1][j - 1];
} else {
dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j]) + matrix[i - 1][j - 1];
}
}
}
return dp[row][cow];
}
}