Fling! is a popular puzzle game created by the well-known developers at CandyCane LLC.

The premise of the game is simple. You are given a certain number of balls on the screen to start. The goal is to fling one into another in order to knock the other off the screen. The puzzle is considered solved if you can do so while leaving only one ball remaining on the screen. Some might read this and think that it might not be too difficult, but the game gets challenging quickly. The problem is that you cannot fling two balls that are adjacent (i.e. next to) each other.

The first ball you choose can fling the 2nd ball if and only if..
    1) The two balls exist in same row or same column
    2) The two balls are not adjacent
    3) There is no other ball in between the two balls

If there exist a 3rd ball after the 2nd ball in the same line of action, the 2nd ball takes the position just before the third ball, pushes the 3rd ball and the 3rd ball gets flinged. (This continues till a ball gets knocked off the screen. Note that 2nd ball and 3rd ball can be adjacent).

Given a Fling! puzzle, just print "Yes" if it is a valid puzzle(solvable) or "No" otherwise.

For better understanding of gameplay you may have a look at this video. (optional)



The first line of the input consists of an integer t, the number of test cases. For each test case, the first line consists of two integers m and n, the number of rows and columns of the puzzle. Then follows the description of the board. A[i][j] is '.' if the cell is empty or 'B' if the cell has a ball.


For each test case print "Yes" if the puzzle is valid or "No" otherwise. (case-sensitive).

Input Constraints:



You can assume that the number of balls in the board is approximately equal to ( m x n ) /10

Sample Input:


5 5






5 4






3 4




1 1


Sample Output:





Added by:cegprakash
Time limit:5s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: BF GOSU

hide comments
2013-02-27 19:07:14 N.R.Samarasekara
Can we knock off B3 by swinging B1-> ??

Reply: Yes

Last edit: 2013-02-27 19:09:06
2013-02-27 18:44:36 Erik Grabljevec
what if 2nd and 3rd ball are next to each other, does 3rd ball still get knocked off ?


Last edit: 2013-12-12 22:21:46
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.