Astral Superposition

시간 제한4초메모리 제한2048 MB

요약
별의 이동 전후 사진을 겹친 결과가 주어졌을 때, 가능한 최소 초기 별의 개수를 구하는 문제이다.
난이도

보통10점 중 6점

유형
그래프, 그리디, 유니온 파인드
정답자
아직 제출이 없습니다

문제

Bessie is using her nifty telescope to take photos of all the stars in the night sky. Her telescope can capture an N×NN \times N (1≤N≤10001 \leq N \leq 1000) photo of the stars where each pixel is either a star or empty sky. Each star will be represented by exactly one pixel, and no two distinct stars share the same pixel.

Overnight, something strange happens to the stars in the sky. Every star either disappears or moves AA pixels to the right, and BB pixels downwards (0≤A,B≤N0 \leq A,B \leq N). If a star disappears or moves beyond the photo boundary, it no longer appears in the second photo.

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 TT (1≤T≤10001 \leq T \leq 1000) independent test cases. If no arrangement of stars can produce the given final photo, output −1-1.

입력

The first line of input contains TT and TT test cases will follow.

The first line of each test case will contain NN AA BB.

Then follow NN lines each representing one row of the superimposed photo. The iith row from the top is represented by a string c_i,1c_i,2…c_i,Nc\_{i,1}c\_{i,2}\dots c\_{i,N}, where each c_i,j∈W,G,Bc\_{i,j} \in \\{W,G,B\\}, representing the colors white, gray, and black respectively.

It is guaranteed that the sum of N2N^2 over all test cases will not exceed 10710^7.

출력

For each test case, output the minimum number of stars that existed before the shifting, or −1-1 if impossible.

예제2

  1. 예제 1

    입력
    1
    3 0 0
    WWB
    BBB
    GGG
    
    예상 출력
    7
    
  2. 예제 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