CF1966B.Rectangle Filling

传统题 时间 2000 ms 内存 256 MiB 5 尝试 1 已通过 1 标签

Rectangle Filling

There is an n×mn \times m grid of white and black squares. In one operation, you can select any two squares of the same color, and color all squares in the subrectangle between them that color.

Formally, if you select positions (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2), both of which are currently the same color cc, set the color of all (x,y)(x, y) where min(x1,x2)xmax(x1,x2)\min(x_1, x_2) \le x \le \max(x_1, x_2) and min(y1,y2)ymax(y1,y2)\min(y_1, y_2) \le y \le \max(y_1, y_2) to cc.

This diagram shows a sequence of two possible operations on a grid:

Is it possible for all squares in the grid to be the same color, after performing any number of operations (possibly zero)?

Input

The first line of the input contains a single integer tt (1t1041 \le t \le 10^4) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers nn and mm (1n,m5001 \le n, m \le 500) — the number of rows and columns in the grid, respectively.

Each of the next nn lines contains mm characters 'W' and 'B' — the initial colors of the squares of the grid.

It is guaranteed that the sum of nmn\cdot m over all test cases does not exceed 31053\cdot 10^5.

Output

For each test case, print "YES" if it is possible to make all squares in the grid the same color, and "NO" otherwise.

You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.

Note

In the first example, it is impossible to ever change the color of any square with an operation, so we output NO.

The second example is the case pictured above. As shown in that diagram, it is possible for all squares to be white after two operations, so we output YES.

In the third and fourth examples, all squares are already the same color, so we output YES.

In the fifth example we can do everything in two operations. First, select positions (2,1)(2, 1) and (1,4)(1, 4) and color all squares with 1x21 \le x \le 2 and 1y41 \le y \le 4 to white. Then, select positions (2,1)(2, 1) and (3,4)(3, 4) and color all squares with 2x32 \le x \le 3 and 1y41 \le y \le 4 to white. After these two operations all squares are white.

Samples

8
2 1
W
B
6 6
WWWWBW
WBWWWW
BBBWWW
BWWWBB
WWBWBB
BBBWBW
1 1
W
2 2
BB
BB
3 4
BWBW
WBWB
BWBW
4 2
BB
BB
WW
WW
4 4
WWBW
BBWB
WWBB
BBBB
1 5
WBBWB
NO
YES
YES
YES
YES
NO
YES
NO

在线编程 IDE

建议全屏模式获得最佳体验