4113: CF105 - D. Entertaining Geodetics
[Creator : ]
Description
The maps in the game are divided into square cells called Geo Panels. Some of these panels are painted. We shall assume that the Geo Panels without color are painted the transparent color.
Besides, the map has so-called Geo Symbols. They look like pyramids of different colors (including Geo Symbols of the transparent color). Each Geo Symbol is located on one Geo Panel, and each Geo Panel may contain no more than one Geo Symbol.
Geo Symbols can be eliminated. To understand better what happens when a Geo Symbol is eliminated, let us introduce some queue to which we will put the recently eliminated Geo Symbols.
Let's put at the head of the queue a Geo Symbol that was eliminated just now. Next, we will repeat the following operation:
Extract the Geo Symbol from the queue. Look at the color of the panel containing the given Geo Symbol. If it differs from transparent and differs from the color of the Geo Symbol, then all Geo Panels of this color are repainted in the color of the given Geo Symbol (transparent Geo Symbols repaint the Geo Panels transparent). Repainting is executed in an infinite spiral strictly in the following order starting from the panel, which contained the Geo Symbol:
Besides, the map has so-called Geo Symbols. They look like pyramids of different colors (including Geo Symbols of the transparent color). Each Geo Symbol is located on one Geo Panel, and each Geo Panel may contain no more than one Geo Symbol.
Geo Symbols can be eliminated. To understand better what happens when a Geo Symbol is eliminated, let us introduce some queue to which we will put the recently eliminated Geo Symbols.
Let's put at the head of the queue a Geo Symbol that was eliminated just now. Next, we will repeat the following operation:
Extract the Geo Symbol from the queue. Look at the color of the panel containing the given Geo Symbol. If it differs from transparent and differs from the color of the Geo Symbol, then all Geo Panels of this color are repainted in the color of the given Geo Symbol (transparent Geo Symbols repaint the Geo Panels transparent). Repainting is executed in an infinite spiral strictly in the following order starting from the panel, which contained the Geo Symbol:
In other words, we select all the panels that need to be repainted and find their numbers in the infinite spiral whose center is placed in the position of the given Geo Symbol. After that, we repaint them in the order of the number's increasing.
If a panel contains another Geo Symbol and this panel is being repainted, then the Geo Symbol is removed from the field and placed at the tail of the queue.
After repainting the Geo Symbol is completely eliminated and the next Geo Symbol is taken from the head of the queue (if there is any) and the process repeats. The process ends if the queue is empty.
See the sample analysis for better understanding.
You know the colors of all the Geo Panels and the location of all the Geo Symbols. Determine the number of repaintings, which will occur if you destroy one of the Geo Symbols.
Input
The first line contains two integers $n, m\ (1 ≤n,m≤ 300)$ — the height and the width of the map (in cells).
Then follow $n$ line; each of them contains $m$ numbers — the Geo Panels' colors.
Then follow $n$ more lines; each of them contains $m$ numbers — the Geo Symbols' description. $-1$ means that the given position contains no Geo Symbol. Otherwise, the number represents the color of the Geo Symbol in the given position.
All colors are integers from $0$ to $10^9$. $0$ represents the transparent color.
The last line contains two integers $x, y\ (1 ≤x≤n,\ 1 ≤y≤m)$ — the row and the column where the Geo Symbol is placed that needs to be eliminated. The rows are numbered from top to bottom, the columns are numbered from left to right. Coordinates are 1-based. It is guaranteed that the position with coordinates $(x,y)$ contains a Geo Symbol.
Then follow $n$ line; each of them contains $m$ numbers — the Geo Panels' colors.
Then follow $n$ more lines; each of them contains $m$ numbers — the Geo Symbols' desc
All colors are integers from $0$ to $10^9$. $0$ represents the transparent color.
The last line contains two integers $x, y\ (1 ≤x≤n,\ 1 ≤y≤m)$ — the row and the column where the Geo Symbol is placed that needs to be eliminated. The rows are numbered from top to bottom, the columns are numbered from left to right. Coordinates are 1-based. It is guaranteed that the position with coordinates $(x,y)$ contains a Geo Symbol.
Output
Print the single number — the total number of repaintings after the Geo Symbol is eliminated.
Sample 1 Input
5 5
9 0 1 1 0
0 0 3 2 0
1 1 1 3 0
1 1 1 3 0
0 1 2 0 3
-1 1 -1 3 -1
-1 -1 -1 0 -1
-1 -1 -1 -1 -1
-1 2 3 -1 -1
-1 -1 -1 -1 2
4 2
Sample 1 Output
35
All actions of the sample you can see on the following picture: