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

Задача 22014. Going for gold - 3


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.
A sage is allowed to pass over the same square more than once. At the same time, he takes gold from it  only once - when he passes through the cell for the first time.

You are given the route of the sage. It is required to determine how many kilograms of gold he collected.

Input
The input data 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 (1<=X<=10000).
It is known that the sage began with a cell with coordinates (1, 1). Next, a X-1 number is written: where did the sage move:
  • the number 1 means that the sage took a step to the right,
  • the number 2 means that the sage took a step up,
  • the number 3 means that the sage took a step to the left,
  • The number 4 means that the sage took a step down.
 
It is known that the sage did not leave the labyrinth, while he could pass through the same cell several times. 

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
9
4 1 1 2 3 3 1 4
24