Two-Dimensional Range Sum

Compute the sum of each queried rectangle in an N by N table using a 2D prefix sum.

Medium4Prefix sumMatrixInterviewNo attempts yetTime limit1sMemory limit256 MB

Problem

An N×NN \times N table is filled with N×NN \times N numbers. Write a program that computes the sum of the entries from (x1,y1)(x_1, y_1) to (x2,y2)(x_2, y_2). Here (x,y)(x, y) means row xx, column yy.

For example, take N=4N = 4 and the table below.

1234
2345
3456
4567

The sum from (2,2)(2, 2) to (3,4)(3, 4) is 3+4+5+4+5+6=273+4+5+4+5+6 = 27, and the sum from (4,4)(4, 4) to (4,4)(4, 4) is 77.

Given the numbers in the table and the sum queries, write a program that answers them.

Input

The first line contains the table size NN and the number of sums to compute, MM. (1N10241 \le N \le 1024, 1M1000001 \le M \le 100000)

Each of the next NN lines holds one row of the table, starting from row 1. A line lists the NN numbers of that row, starting from column 1. Every number in the table is a natural number no larger than 1,000.

Each of the next MM lines holds four integers x1x_1, y1y_1, x2x_2, y2y_2. (1x1x2N1 \le x_1 \le x_2 \le N, 1y1y2N1 \le y_1 \le y_2 \le N)

Output

Print MM lines. On each line print the sum from (x1,y1)(x_1, y_1) to (x2,y2)(x_2, y_2), one per query, in input order.