分享下我的思路
public void first() {
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
int M = scanner.nextInt();
int[][] nums = new int[N + 2][M + 2];
int result = 0;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++) {
nums[i][j] = scanner.nextInt();
}
}
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++) {
if (nums[i][j] < 1) {
continue;
}
int ***Num = nums[i][j];
result += 2;
int up = ***Num - nums[i - 1][j];
int down = ***Num - nums[i + 1][j];
int left = ***Num - nums[i][j - 1];
int right = ***Num - nums[i][j + 1];
if (up > 0) {
result += up;
}
if (down > 0) {
result += down;
}
if (left > 0) {
result += left;
}
if (right > 0) {
result += right;
}
}
}
System.out.println(result);
}