Under construction

Still being built. Things may change

T - Covering

Time limit1sMemory limit512 MB

Summary
Place one T-tetromino per special cell, centered on it, without overlap or leaving the grid, to maximize the total covered weight.
Level

Hard9 of 10

Topics
Graph, Matrix
Solved
No attempts yet

Problem

If you have ever played Tetris, you might know that one of the figures looks as follows:

We call this figure a T-tetromino. A tetromino is a connected geometric figure made of four cells. The cell marked with × is the center cell.

Manca draws a rectangular grid with mm rows and nn columns and writes a number into each cell. Rows are numbered from 0 to m−1m-1, and columns from 0 to n−1n-1. She also marks some cells as special, for example by painting them red. Then she asks her friend Nika to place T-tetrominoes on the grid under these conditions:

  • The number of T-tetrominoes must equal the number of special cells. The center cell of each T-tetromino must lie on a special cell.
  • No two T-tetrominoes may overlap.
  • Every T-tetromino must lie completely inside the grid.

A T-tetromino has four orientations: ┬, ┴, ├, and ┤.

If the conditions cannot be met, Nika answers No. Otherwise, she finds a placement that maximizes the sum of the numbers in the cells covered by the T-tetrominoes, and tells Manca that maximum sum.

Write a program that helps Nika solve this puzzle.

Input

The first line contains integers mm and nn. Each of the following mm lines contains nn integers in the interval [0,1000][0, 1000]. The jj-th integer in the ii-th line is the number written in the cell at row ii and column jj.

The next line contains an integer kk with k∈{1,…,mn}k \in \{1, \ldots, mn\}. Each of the following kk lines contains integers ri∈{0,…,m−1}r_i \in \{0, \ldots, m-1\} and ci∈{0,…,n−1}c_i \in \{0, \ldots, n-1\}, the row and column of the ii-th special cell. The list of special cells has no duplicates.

Output

Print the maximum possible sum of the numbers in the cells covered by the T-tetrominoes. If no valid placement exists, print No.

Constraints

1≤mn≤1061 \le mn \le 10^6

Examples2

  1. Example 1

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

    Input
    5 6
    7 3 8 1 0 9
    4 6 2 5 8 3
    1 9 7 3 9 5
    2 6 8 4 5 7
    3 8 2 7 3 6
    3
    1 1
    2 2
    3 3
    
    Expected output
    No