#include using namespace std; #define rep(i,a,b) for(int i=a;i> H >> W; rep(y, 1, H + 1) cin >> _S[y], _S[y] = "#" + _S[y]; cin >> Q; rep(q, 0, Q) scanf("%d%d%d%d", &A[q], &B[q], &C[q], &D[q]); rep(y, 1, max(H, W) + 1) rep(x, 0, max(H, W) + 1) S[y] += "#"; // → rep(y, 1, H + 1) rep(x, 1, W + 1) S[y][x] = _S[y][x]; pre(H, W); rep(q, 0, Q) rep(y, A[q], C[q] + 1) ans[q] += get(y, B[q], D[q]); // ← rep(y, 1, H + 1) rep(x, 1, W + 1) S[y][W - x + 1] = _S[y][x]; pre(H, W); rep(q, 0, Q) rep(y, A[q], C[q] + 1) ans[q] += get(y, W - D[q] + 1, W - B[q] + 1); // ↓ rep(y, 1, H + 1) rep(x, 1, W + 1) S[x][y] = _S[y][x]; pre(W, H); rep(q, 0, Q) rep(y, B[q], D[q] + 1) ans[q] += get(y, A[q], C[q]); // ↑ rep(y, 1, H + 1) rep(x, 1, W + 1) S[x][H - y + 1] = _S[y][x]; pre(W, H); rep(q, 0, Q) rep(y, B[q], D[q] + 1) ans[q] += get(y, H - C[q] + 1, H - A[q] + 1); //rep(y, 1, H + 1) cout << S[y] << endl; rep(q, 0, Q) printf("%lld\n", ans[q]); }