Precompute a 2D prefix sum matrix. Each cell prefix[i][j] stores the sum of all elements in the rectangle from (0, 0) to (i, j).
To answer a query for rectangle (r1, c1) to (r2, c2), use inclusion-exclusion:
sum = prefix[r2][c2] - prefix[r1-1][c2] - prefix[r2][c1-1] + prefix[r1-1][c1-1]
You subtract the regions above and to the left, but that double-subtracts the top-left corner, so you add it back.
Build the prefix matrix by scanning once: each cell equals the original value plus the cell above, plus the cell to the left, minus the diagonal (to avoid double-counting).