#include <iostream>
using namespace std;
long long pre[1010][1010] = {0,};
long long arr[1010][1010];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
long long N;
cin >> N;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
cin >> arr[i][j];
}
}
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
pre[i][j] = pre[i-1][j] + pre[i][j-1] - pre[i-1][j-1] + arr[i][j];
}
}
long long A;
cin >> A;
for (int i = 0; i < A; i++) {
long long x, y, x2, y2;
cin >> x >> y >> x2 >> y2;
cout << pre[x2][y2] - pre[x-1][y2] - pre[x2][y-1] + pre[x-1][y-1] << "\n";
}
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: