Problems
Bessie is using her nifty telescope to take photos of all the stars in the night sky. Her telescope can capture an
Overnight, something strange happens to the stars in the sky. Every star either disappears or moves
Bessie took photos before and after the shifting positions, but after experimenting in Mootoshop, she accidentally superimposed one photo onto the other. Now, she can see white pixels where both photos were empty, gray pixels where stars existed in exactly one photo, and black pixels where there was a star in both photos. Bessie also remembers that no new stars moved into the frame of the second photo, so her first photo contains all of the stars in the night sky.
Given the final photo, determine the minimum possible number of stars in the sky before the shifting incident for
Input
The first line of input contains
The first line of each test case will contain
Then follow
It is guaranteed that the sum of
Output
For each test case, output the minimum number of stars that existed before the shifting, or −1 if impossible.
Subtask
| # | Score | Condition |
|---|---|---|
| #1 | 10 | |
| #2 | 20 | |
| #3 | 30 | |
| #4 | 40 | No additional constraints |
Example #1
1
3 0 0
WWB
BBB
GGG
7
In this example, there is no shifting. The first photo is as follows: (. for sky, * for star)
..*
***
***
The second photo, where the stars on the bottom row disappeared, is as follows:
..*
***
...
This is the only way to produce the superimposed photo, so the minimum possible number of initial stars is 7
Example #2
3
5 1 2
GWGWW
WGWWW
WBWGW
WWWWW
WWGWW
3 1 1
WWW
WBW
WWW
3 1 0
GGB
GGW
WWW
4
-1
4
In the first case, there were at least 4 stars at the start. If we let (r,c)
denote the intersection of the
In the second case, there is no arrangement of stars in the initial photo that can produce the middle black pixel given the shift.
In the third case, there were at least 4 stars at the start. One possibility is that they were initially at (1,1),(1,2),(1,3), and (2,1)
. In the second photo, the star originally at (1,1) disappeared and the star originally at (1,3) moved off frame. The other two stars shifted to the right by 1.