Олимпиадный тренинг

Задача 18788. Going for gold - 1


One day the king decided to reward one of his wise men for his good work. He led him into a rectangular room measuring NxM, in each cell of which lay several kilograms gold. The king allowed the sage to go around several cells (moving from the cell where the sage is now to one of the four neighboring cells) and collect all the gold that gets in his way.
 
You have been given the route of the sage. It is required to determine how many kilograms of gold he collected.
 
Input
The
Inputs contains the plan of the room and the route of the sage. First, the number of rows N, then the number of columns M (1<=N<=20,1<=M<=20).
Then N lines are written with M numbers in each - the number of kilograms of gold that lies in this cell (a number from 0 to 50).
Next, the number X is written - how many cells the sage walked around. Next, the coordinates of these cells are recorded (the coordinates of a cell are two numbers: the first determines the row number, the second determines the column number, the upper left cell on the plan has coordinates (1,1), the lower right one - (N,M)).
It's guaranteed that the sage didn't go over the same cell twice.
 
Output
Output the number of kilograms of gold collected by the sage into the output file.
 
Examples
# Input Output
1
3 4
1 2 3 4
5 6 7 8
9 10 11 12
5
1 1
2 1
2 2
2 3
1 3
22