Jujisu

Given an N by M grid of populations, answer K queries for the sum of people inside each requested rectangle.

Medium4Prefix sumArrayMatrixImplementationInterviewNo attempts yetTime limit2sMemory limit512 MB

Problem

King Jingyeong of the Square Kingdom wants to rule his land more easily, so he plans to bundle many 1×11 \times 1 unit districts into one large administrative region called a jujisu (州地數, a count of a village's land). To decide how to bundle them he needs a reference table of how many people live inside a rectangular range.

Population of each unit district and three rectangular ranges

The king is a very solemn man, so he announces a rectangular range with four numbers and nothing more.

Suppose people live as in the picture above. When the king wants to know how many people live in the rectangular range of Figure 1, he calls out the four integers 1 1 3 2. In the same way Figure 2 is 1 1 1 4 and Figure 3 is 1 1 4 4.

Write a program that builds this reference table for the king.

Input

The first line has the size of the land, NN and MM (1N,M10241 \le N, M \le 1024).

Each of the next NN lines has MM integers. The jj-th number on the ii-th line is how many people live in the unit district at row ii, column jj. At least one person lives in every unit district, and no unit district holds more than 100 people.

The next line has KK, the number of rectangular ranges the king asks about (1K1000001 \le K \le 100\,000).

Each of the next KK lines has four integers x1x_1, y1y_1, x2x_2, y2y_2 that describe a rectangular range (1x1x2N1 \le x_1 \le x_2 \le N, 1y1y2M1 \le y_1 \le y_2 \le M). The range holds every unit district from row x1x_1 to row x2x_2 and from column y1y_1 to column y2y_2.

Output

Print KK lines. On the ii-th line print the total number of people living inside the ii-th rectangular range, in the order the ranges are given.