This page is still under construction.

Parts of this page are still being built. What you see may change.

Aquapark

Time limit1sMemory limit512 MB

Summary
Sum the grid values inside the Manhattan diamond of radius l_i around each lifeguard.
Level

Medium6 of 10

Topics
Prefix sum, Matrix
Solved
No attempts yet

Problem

An aquapark is shaped like a square with side length nn and is divided into n2n^2 unit cells of side 11. Each cell is either a pool or a walkway. A pool cell has a positive number of children playing in it; a walkway has none.

There are rr lifeguards on duty. Under the safety rules a lifeguard may move only parallel to the walls, so the distance between two cells (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2) is always the Manhattan distance ∣x1−x2∣+∣y1−y2∣|x_1 - x_2| + |y_1 - y_2|. Lifeguard ii is responsible for every pool that lies at distance at most lil_i from their position.

A lifeguard's workload is the total number of children in all the pools they guard. Determine the workload of every lifeguard.

Input

The first line contains two integers nn and rr (1≤n≤10001 \le n \le 1000, 1≤r≤n21 \le r \le n^2): the side length of the aquapark and the number of lifeguards.

Each of the next nn lines describes one row of the map. The ii-th line contains nn non-negative integers ai,1,ai,2,…,ai,na_{i,1}, a_{i,2}, \dots, a_{i,n} (0≤ai,j≤1060 \le a_{i,j} \le 10^6). If ai,j=0a_{i,j} = 0, cell (i,j)(i, j) is a walkway; otherwise it is a pool holding ai,ja_{i,j} children.

Each of the next rr lines describes one lifeguard with three integers xix_i, yiy_i, lil_i (1≤xi,yi≤n1 \le x_i, y_i \le n, 1≤li≤n1 \le l_i \le n): the row and column of the lifeguard's position, and the maximum distance to the pools they guard.

Output

Print exactly rr lines. The ii-th line must contain a single integer pip_i: the number of children guarded by lifeguard ii.

Hint

Examples5

  1. Example 1

    Input
    5 2
    6 3 0 0 9
    7 1 4 0 5
    0 5 0 0 2
    0 0 0 8 0
    1 2 0 0 0
    2 2 1
    4 5 2
    
    Expected output
    20
    15
    
  2. Example 2

    Input
    1 1
    7
    1 1 1
    
    Expected output
    7
    
  3. Example 3

    Input
    3 3
    0 0 0
    0 0 0
    0 0 0
    1 1 1
    2 2 3
    3 3 2
    
    Expected output
    0
    0
    0
    
  4. Example 4

    Input
    4 4
    1 2 3 4
    5 6 7 8
    9 10 11 12
    13 14 15 16
    2 2 1
    2 2 2
    2 3 1
    1 1 4
    
    Expected output
    30
    76
    35
    93
    
  5. Example 5

    Input
    3 4
    1 0 2
    0 3 0
    4 0 5
    1 1 1
    1 1 3
    3 3 3
    2 2 3
    
    Expected output
    1
    10
    14
    15