P12529 [XJTUPC 2025] Symmetrical Isolation: The Battle of Black and White

Description

You are playing a game called "Your Craft". In this game, you can freely create and destroy different types of blocks in a three-dimensional space filled with blocks. Now, you have a grid composed of $N \times M$ cells beneath your feet. At the beginning, you covered these cells with white carpets, and then you went to sleep. Unfortunately, while you were sleeping, the problem setter secretly replaced some of the white carpets with black carpets. When you woke up and opened the game again, you found that your masterpiece had been tainted, and you were very angry! So you decided to make the best of the situation by keeping the existing black carpets unchanged and dyeing some of the white carpets black to satisfy the following two conditions: - No two cells with black carpets can share a common edge, which means: - If the cell in the $i$-th row and $j$-th column contains a black carpet, then the cells in the $(i-1)$-th row and $j$-th column, $(i+1)$-th row and $j$-th column, $i$-th row and $(j-1)$-th column, and $i$-th row and $(j+1)$-th column cannot contain black carpets if they have carpets. - The final pattern must have at least one horizontal or vertical line of symmetry, which means at least one of the following two propositions must be satisfied: - For any $i \in [1,N]$ and $j \in [1,M]$, the color of the carpet in the $i$-th row and $j$-th column must be the same as the color of the carpet in the $(N-i+1)$-th row and $j$-th column; - For any $i \in [1,N]$ and $j \in [1,M]$, the color of the carpet in the $i$-th row and $j$-th column must be the same as the color of the carpet in the $i$-th row and $(M-j+1)$-th column. You need to determine whether you can achieve these two conditions. Note: You cannot destroy the existing black carpets.

Input Format

The first line contains two integers $N$ and $M$ ($1\le N,M\le 100$), separated by a space, representing the size of the grid. The next $N$ lines each contain a string $S$ of length $M$. In the string $S$, a character $\tt{W}$ indicates that this position has a white carpet, while a character $\tt{B}$ indicates that this position has a black carpet. No other characters will appear in the string.

Output Format

If you can satisfy the conditions given in the problem, output the string $\tt{Yes}$; otherwise, output the string $\tt{No}$. The answer is case insensitive. For example, the strings $\tt{yEs}$, $\tt{Yes}$, $\tt{yes}$, and $\tt{YES}$ will all be recognized as if you can satisfy the conditions.

Explanation/Hint

For the first sample: ![](https://cdn.luogu.com.cn/upload/image_hosting/e9hqggx4.png) The leftmost image shows the situation you saw when you woke up. You cannot achieve the conditions by changing white carpets to black carpets. The pictures in the middle and on the rightmost show two possible ways to replace the carpet, but they cannot satisfy the two requirements in the question (the middle image does not have a horizontal or vertical line of symmetry; in the rightmost image, the black carpets share a common edge). It can be proven that any way of changing the carpets does not satisfy the requirements, so output $\tt{No}$. For the second sample: ![](https://cdn.luogu.com.cn/upload/image_hosting/3o95kyxx.png) The image shows a valid way to change the carpets. You can change the white carpet in the lower left corner to black, which satisfies both requirements in the problem. Therefore, you should output $\tt{Yes}$. For the third sample: ![](https://cdn.luogu.com.cn/upload/image_hosting/si5x2zj6.png) This is one possible way to change the carpets, but it is not a valid one. Remember: only configurations with a horizontal or vertical line of symmetry are considered valid. For the fourth sample: ![](https://cdn.luogu.com.cn/upload/image_hosting/rik73puq.png) The situation you saw when you woke up is already valid, so you do not need to make any changes. For the fifth sample: When you woke up, there were already two black carpets sharing a common edge, so you cannot change the white carpets to meet the requirements. For the sixth sample: A pattern with all white carpets is also considered valid because it has a horizontal or vertical line of symmetry and does not have two black carpets sharing a common edge.