是不是因为没有循环输入。
public class Main1 {
public int getSum(int[][] cube, int M, int N){
int sum = 0;
for(int temi = 1; temi <= M; temi++){
int temsum = 0;
for(int temj = 1; temj <= N; temj++){
int val = cube[temi][temj];
temsum += (val > cube[temi-1][temj])?val-cube[temi-1][temj]:0;
temsum += (val > cube[temi+1][temj])?val-cube[temi+1][temj]:0;
temsum += (val > cube[temi][temj-1])?val-cube[temi][temj-1]:0;
temsum += (val > cube[temi][temj+1])?val-cube[temi][temj+1]:0;
}
sum += temsum;
}
return sum;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
Main1 obj = new Main1();
while(sc.hasNext()){
int M, N;
M = sc.nextInt();
N = sc.nextInt();
int[][] cube = new int[M+2][N+2];
for(int temi = 1; temi <= M; temi++){
for(int temj = 1; temj <= N; temj++){
cube[temi][temj] = sc.nextInt();
}
}
int sum = obj.getSum(cube, M, N);
System.out.println(sum+2*M*N);
}
}
}