Mankind has recently discovered the secret wealth in Mars. Many powerful countries are trying to get hold of the riches of Mars. The map of Mars can be represented as a 2-Dimensional grid consisting of rows and columns numbered from to and to . Cells can be represented with and . stands for the row and stands for the column. Each cell contains unit of wealth. Value of equal to means empty cell.
countries are preparing their armies to collect as much wealth as they can. All the countries will land their armies on the same day. One day on Mars contains hours and minutes(60 minutes per hour). The army of country will land on cell . The time of landing will be given to you in format . After landing on a cell, if there is any wealth in the cell they will immediately collect them. Otherwise, they will not continue their operation and return empty-handed. The time of collecting wealth is negligible. After collecting the wealth, the army will split and expand to the adjacent cells vertically and horizontally if there is any wealth in those cells. This process will be continued from each cell. If two armies try to advance in the same cell at the same time, the army with a smaller value of will get hold of the cell. Moving from one cell to another takes exactly one minute.
You have to calculate, how much wealth all the armies can collect and which army can collect how much wealth.
The first line of input will consist of two integers and .
Each of the next lines will contain integers representing the wealth of the cell.
The next line will contain integer .
line follows. line will contain the time of landing for the army in the format and the cell the army will land.
In the first line print the total units of wealth that can be collected by the armies of the earth.
For the next lines, print one integer each. In the line, print the total amount of wealth will be collected by the army.
Input | Output |
---|---|
4 4 1 2 1 2 0 3 0 5 0 1 7 4 10 0 5 3 3 00:22 4 1 24:36 1 4 24:36 3 2 | 44 10 18 16 |
At , the first army will land on the cell and collect units. All the armies combined will collect units of wealth. The first, second, and third armies will collect , and units respectively. |