The GoC Timber Mafia is notorious for its deforestation
activities in the forests near Siruseri. These activities have
increased multifold after the death of the bandit who used to
lord over these jungles.  Having lost the battle to prevent the
Mafia from illegally felling the teak trees in this forest, the
government of Siruseri came up with a novel idea.  Why not
legalise the deforestation activity and at least make some money
in the process? So the Government decided to lease out parts of
the forest to the Timber Mafia.
Most of the teak trees in the forests of Siruseri were planted
during the colonial times, after the native trees had been cut.
Like everything European, the forest is very regular and
orderly. It is rectangular in shape and the trees are arranged in
uniformly spaced rows and coloumns.
Since the trees differ in height and girth, the timber value
differs from tree to tree.  The forest department has collected
data on each tree and knows the volume of wood (in cubic feet)
available in each tree in the forest.
The forest department maintains this information in the form of
an $M \times N$ array of integers, where the
$(i, j)$th entry is the volume, in cubic feet, of the
$i^{th}$ tree on the $i^{th}$ row (or, equivalently, the
$i^{th}$ tree on the $i^{th}$ column).  We assume that
our rows are numbered top to bottom and the columns are numbered
from left to right.  For example, such an array could look like

This array tells us that  the volume of the tree at position $(3,4)$ is $15$
cubic feet and so on.
Any rectangular piece of land with trees at each corner can be
leased out.  In order to fix the lease price for any rectangular
plot of the forest the forest department needs to know the amount
of wood available inside the plot.
A rectangular plot is described by the positions of the trees in
its top left corner and the bottom right corner. For example the
positions $(2,2)$ and $(3,4)$ describes the following part
rectangular part of the above forest.

The total amount of wood available in this rectangular plot is $76$
cubic feet.  Similarly $(4,2)$ and $(4,2)$ describes the rectangle
with just one tree and its volume is $20$ cubic feet.
Your task is to write a program that helps the forest department
to compute the total volume of the trees insides any specfied
rectangular plot.

-----Input:-----
- The first line of the input contains two integers $M$ and $N$ indicating the number of rows and columns of trees in the forest. 
- The following $M$ lines have $N$ integers each. The $j^{th}$ integer on line $i+1$ denotes the volume (in cubic feet) of the $j^{th}$  tree on the $i^{th}$  row. 
- Line $M+2$ contains a single integer $C$ indicating the number of rectangles for which the total volume is to be computed. 
- Each of the following $C$ lines (line $M+2+1 ... M+2+C$) contain four integers $x_1, y_1, x_2$ and $y_2$ (with $x_1 \leq x_2$ and $y_1 \leq y_2$) and describes a rectangle. The rectangle has its top left corner at the tree in position $(x_1,y_1)$ and its bottom right corner at the tree at position $(x_2,y_2)$.

-----Output:-----
Your output must contain $C$ lines with one integer on each line. Line $i$ must contain the total volume of wood in the rectangle described on line $M+2+i$ in the input.

-----Constraints-----
- In $30 \%$ of inputs, $1 \leq C \leq 100$.
- In all inputs, $2 \leq N, M \leq 1000$ and $1 \leq C \leq 1000000$
- $0 \leq$ volume of wood in each tree $\leq 1000$
- $1 \leq x_1 \leq x_2 \leq M$
- $1 \leq y_1 \leq y_2 \leq N$

-----Sample Input-----
4 4 
3 4 15 23 
14 20 12 9
3 8 12 15
12 20 7 5
2
2 2 3 4 
4 2 4 2

-----Sample Output-----
76
20

Difficulty: UNKNOWN_DIFFICULTY

Solutions:
from sys import stdin
(n, m) = map(int, stdin.readline().split())
forest = []
matrix = []
for _ in range(n):
	forest.append(list(map(int, stdin.readline().split())))
	matrix.append([0] * m)
matrix[0][0] = forest[0][0]
for j in range(1, m):
	matrix[0][j] = matrix[0][j - 1] + forest[0][j]
for i in range(1, n):
	matrix[i][0] = matrix[i - 1][0] + forest[i][0]
for i in range(1, n):
	for j in range(1, m):
		matrix[i][j] = matrix[i - 1][j] + matrix[i][j - 1] - matrix[i - 1][j - 1] + forest[i][j]
c = int(input())
for _ in range(c):
	(x1, y1, x2, y2) = map(int, stdin.readline().split())
	x1 -= 1
	y1 -= 1
	x2 -= 1
	y2 -= 1
	appo = 0
	if x1 > 0:
		appo += matrix[x1 - 1][y2]
	if y1 > 0:
		appo += matrix[x2][y1 - 1]
	if x1 > 0 and y1 > 0:
		appo -= matrix[x1 - 1][y1 - 1]
	print(matrix[x2][y2] - appo)


(n, m) = map(int, input().split())
forest = []
matrix = []
for _ in range(n):
	forest.append(list(map(int, input().split())))
	matrix.append([0] * m)
matrix[0][0] = forest[0][0]
for j in range(1, m):
	matrix[0][j] = matrix[0][j - 1] + forest[0][j]
for i in range(1, n):
	matrix[i][0] = matrix[i - 1][0] + forest[i][0]
for i in range(1, n):
	for j in range(1, m):
		matrix[i][j] = matrix[i - 1][j] + matrix[i][j - 1] - matrix[i - 1][j - 1] + forest[i][j]
c = int(input())
for _ in range(c):
	(x1, y1, x2, y2) = map(int, input().split())
	x1 -= 1
	y1 -= 1
	x2 -= 1
	y2 -= 1
	appo = 0
	if x1 > 0:
		appo += matrix[x1 - 1][y2]
	if y1 > 0:
		appo += matrix[x2][y1 - 1]
	if x1 > 0 and y1 > 0:
		appo -= matrix[x1 - 1][y1 - 1]
	print(matrix[x2][y2] - appo)


(rows, columns) = map(int, input().split())
a = [[0 for j in range(columns + 1)] for i in range(rows + 1)]
for i in range(1, rows + 1):
	x = [int(i) for i in input().split()]
	s = 0
	for j in range(columns):
		s += x[j]
		a[i][j + 1] = s + a[i - 1][j + 1]
for i in range(int(input())):
	(x1, y1, x2, y2) = map(int, input().split())
	s = a[x2][y2] - a[x2][y1 - 1] - a[x1 - 1][y2] + a[x1 - 1][y1 - 1]
	print(s)


from numpy import *
(m, n) = list(map(int, input().split()))
trees = array([[None] * n] * m)
for i in range(m):
	trees[i] = list(map(int, input().split()))
c = int(input())
result = []
for _ in range(c):
	ans = 0
	(x1, y1, x2, y2) = list(map(int, input().split()))
	for i in range(x1 - 1, x2):
		temp = [trees[i, j] for j in range(y1 - 1, y2)]
		ans += sum(temp)
	result.append(ans)
for i in result:
	print(i)


a = input().split()
(n, m) = (int(a[0]), int(a[1]))
arr = []
for i in range(n):
	a = input().split()
	a = [int(j) for j in a]
	arr.append(a)
for t in range(int(input())):
	a = input().split()
	(x1, y1, x2, y2) = (int(a[0]), int(a[1]), int(a[2]), int(a[3]))
	total = 0
	for i in range(x1 - 1, x2):
		for j in range(y1 - 1, y2):
			total += arr[i][j]
	print(total)


while True:
	try:
		rolcol = input().split()
		row = int(rolcol[0])
		col = int(rolcol[1])
		tree = []
		for i in range(row):
			a = input().split()
			tree.append(a)
		sell = int(input())
		for i in range(sell):
			sum1 = 0
			pos = input().split()
			x1 = int(pos[0]) - 1
			y1 = int(pos[1]) - 1
			x2 = int(pos[2]) - 1
			y2 = int(pos[3]) - 1
			for j in range(x1, x2 + 1):
				for k in range(y1, y2 + 1):
					sum1 = sum1 + int(tree[j][k])
			print(sum1)
	except EOFError:
		break


from sys import stdin, stdout
(rows, columns) = map(int, stdin.readline().strip().split())
matrix = []
for _ in range(rows):
	row = list(map(int, stdin.readline().strip().split()))
	matrix.append(row)
C = int(stdin.readline().strip())
for _ in range(C):
	(tlx, tly, brx, bry) = map(int, stdin.readline().strip().split())
	s = 0
	for r in range(tlx - 1, brx):
		for c in range(tly - 1, bry):
			s += matrix[r][c]
	stdout.write(str(s) + '\n')


import numpy as np
l = input().split()
n = int(l[0])
m = int(l[1])
l = []
for _ in range(n):
	l.append(list(map(int, input().split()))[:])
tc = int(input())
arr = np.array(l)
for _ in range(tc):
	dim = input().split()
	r1 = int(dim[0])
	c1 = int(dim[1])
	r2 = int(dim[2])
	c2 = int(dim[3])
	t = arr[r1 - 1:r2, c1 - 1:c2]
	print(np.sum(t))


l = input().split()
n = int(l[0])
m = int(l[1])
l = []
for _ in range(n):
	l.append(list(map(int, input().split()))[:])
tc = int(input())
for _ in range(tc):
	dim = input().split()
	r1 = int(dim[0])
	c1 = int(dim[1])
	r2 = int(dim[2])
	c2 = int(dim[3])
	sum = 0
	for i in range(r1 - 1, r2):
		for j in range(c1 - 1, c2):
			sum += l[i][j]
	print(sum)


try:
	(m, n) = list(map(int, input().split()))
	vol = [[0] * (n + 1)]
	area = [[]]
	for i in range(m):
		area.append([0] + list(map(int, input().split())))
		vol.append([0] * (n + 1))
	for i in range(1, m + 1):
		for j in range(1, n + 1):
			vol[i][j] = vol[i - 1][j] + vol[i][j - 1] - vol[i - 1][j - 1] + area[i][j]
	t = int(input())
	for i in range(t):
		(x, y, p, q) = list(map(int, input().split()))
		print(vol[p][q] - vol[x - 1][q] - vol[p][y - 1] + vol[x - 1][y - 1])
except:
	pass


(n, m) = list(map(int, input().split()))
l = []
for i in range(n):
	x = list(map(int, input().split()))
	l.append(x)
t = int(input())
for _ in range(t):
	(x1, y1, x2, y2) = list(map(int, input().split()))
	vol = 0
	for i in range(x1 - 1, x2):
		for j in range(y1 - 1, y2):
			vol += l[i][j]
	print(vol)


(m, n) = map(int, input().split())
l = []
for _ in range(m):
	l.append(list(map(int, input().split())))
q = []
(m, n) = (0, 0)
for _ in range(int(input())):
	(y1, x1, y2, x2) = map(int, input().split())
	q.append([y1, x1, y2, x2])
	n = max(n, x2)
	m = max(m, y2)
dp = [[0 for _ in range(n + 1)] for _ in range(m + 1)]
dp[1][1] = l[0][0]
for i in range(1, n + 1):
	dp[1][i] = dp[1][i - 1] + l[0][i - 1]
for j in range(2, m + 1):
	t_sum = 0
	for i in range(1, n + 1):
		t_sum += l[j - 1][i - 1]
		dp[j][i] = t_sum + dp[j - 1][i]
for (y1, x1, y2, x2) in q:
	ans = dp[y2][x2] - dp[y2][x1 - 1] - dp[y1 - 1][x2] + dp[y1 - 1][x1 - 1]
	print(ans)