QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 512 MB Total points: 100

#8742. Black and White

Statistics

Xiao I and Xiao J are playing a game again.

The game is played on an $n \times m$ grid. We describe the cell in the $x$-th row and $y$-th column as $(x, y)$, where $1 \le x \le n$ and $1 \le y \le m$. Initially, at least one cell on the grid is white, and the others are black.

Xiao I and Xiao J take turns, with Xiao I going first. In each turn, the current player must choose exactly one white cell and paint it black.

Two cells are considered adjacent if and only if they share an edge. If, after a move, the cell $(1, 1)$ is not white, or the cell $(n, m)$ is not white, or it is impossible to travel from $(1, 1)$ to $(n, m)$ by moving only through adjacent white cells (i.e., $(1, 1)$ and $(n, m)$ are no longer in the same connected component of white cells), the current player loses, and the other player wins.

As a spectator, you want to determine who will win, assuming both Xiao I and Xiao J play optimally.

Input

The input is read from standard input.

This problem contains multiple test cases. The first line contains an integer $T$ ($1 \le T \le 100$), representing the number of test cases. The following lines describe each test case. For each test case:

  • The first line contains two integers $n, m$ ($2 \le n, m \le 1000$), representing the dimensions of the grid.
  • The next $n$ lines each contain a string of length $m$ consisting of characters 'B' and 'W', describing the initial state of the grid. A character 'B' at the $i$-th row and $j$-th column means $(i, j)$ is initially black, and 'W' means $(i, j)$ is initially white. It is guaranteed that there is at least one white cell on the initial grid.

It is guaranteed that the sum of $n \times m$ over all test cases in a single test file does not exceed $5 \times 10^6$.

Output

Output to standard output.

For each test case, output a single character on a new line. If Xiao I wins, output 'I'; otherwise, if Xiao J wins, output 'J'.

Examples

Input 1

2
2 2
WB
WW
2 2
WW
WW

Output 1

J
I

Note

For the first test case, Xiao I can only paint $(2, 1)$ black, but painting $(2, 1)$ black makes it impossible to travel from $(1, 1)$ to $(2, 2)$ using only adjacent white cells. Therefore, no matter what move Xiao I makes, they will lose, so the output is 'J'.

For the second test case, Xiao I can paint $(1, 2)$ black, and no matter what move Xiao J makes, they will lose, so the output is 'I'.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.