CSES - Shared codeLink to this code:
https://cses.fi/paste/2bfff4ba202881fd7bc2f2/
#include <bits/stdc++.h>
#define range(it, a, b) for (ll it = a; it < b; it++)
#define all(x) begin(x), end(x)
#define ll long long
#define ull unsigned long long
#define INF64 ((ll) 1 << 60)
#define INF32 (1 << 30)
#define uset unordered_set
#define umap unordered_map
#define pqueue priority_queue
using namespace std;
void setio (string name) {
ios_base::sync_with_stdio(0);
cin.tie(0);
if (name.size()) {
freopen((name + ".in").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
}
ll n, q;
vector<vector<ll>> pref;
void solve() {
cin >> n >> q;
pref.resize(n + 1, vector<ll> (n + 1));
char c;
range(i, 1, n + 1){
range(j, 1, n + 1) {
cin >> c;
pref[i][j] = (c == '*');
pref[i][j] += pref[i - 1][j] + pref[i][j - 1] - pref[i - 1][j - 1];
}
}
ll x, y, X, Y;
while (q--) {
cin >> x >> y >> X >> Y;
cout << pref[X][Y] - pref[x - 1][Y] - pref[X][y - 1] + pref[x - 1][y - 1] << '\n';
}
}
int main () {
setio("");
ll t = 1;
// cin >> t;
while (t--) solve();
}