Beautiful Archipelago

Time limit2sMemory limit128 MB

Problem

A country consists of many islands. Some of its islands have the same shape. Two islands are considered to have the same shape if one can be translated so that it exactly coincides with the other; rotations and reflections are not allowed. The beauty of the country is the number of unordered pairs of islands that have the same shape.

Because the sea level rises every year, this beauty changes over time.

The map is represented by an R x C grid of square cells. At the time the map is made, the sea level is 0, and each cell has a nonnegative height. The sea level rises by 1 every year. After T years, only cells whose height is greater than T remain land; all cells with height at most T, including cells inside an island, are underwater. Two land cells belong to the same island if one can be reached from the other by moving only up, down, left, and right through land cells.

Initial stateAfter 5 years

For each queried year T, determine the beauty of the country after T years.

Input

The first line contains the map size R and C (3 <= R, C <= 1000).

Each of the next R lines contains C nonnegative integers separated by spaces. Each value is at most 10^9 and represents the height of that cell at the time the map is made.

The next line contains the number of queried years Q (1 <= Q <= 100000).

The next line contains Q natural numbers in increasing order, each at most 10^9.

Output

Print Q lines. For each queried year, print the beauty of the country after that many years.