x가 행 y가 열로 된 것 주의!
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st;
st=new StringTokenizer(br.readLine());
int n=Integer.parseInt(st.nextToken());
int m=Integer.parseInt(st.nextToken());
int[][] nums=new int[n+1][n+1];
for(int i=1;i<n+1;i++){
st=new StringTokenizer(br.readLine());
for(int j=1;j<n+1;j++){
nums[i][j]=Integer.parseInt(st.nextToken());
}
}
int[][] sums=new int[n+1][n+1];
sums[1][1]=nums[1][1];
for(int i=1;i<n+1;i++){
for(int j=1;j<n+1;j++){
if(i==1&&j==1)continue;
sums[i][j]=sums[i-1][j]+sums[i][j-1]-sums[i-1][j-1]+nums[i][j];
}
}
while(m-->0){
st=new StringTokenizer(br.readLine());
int x1=Integer.parseInt(st.nextToken());
int y1=Integer.parseInt(st.nextToken());
int x2=Integer.parseInt(st.nextToken());
int y2=Integer.parseInt(st.nextToken());
bw.write(sums[x2][y2]-sums[x2][y1-1]-sums[x1-1][y2]+sums[x1-1][y1-1]+"\n");
}
bw.flush();
}
}
#누적합