#K64272. Tic-Tac-Toe Winner Checker

    ID: 31939 Type: Default 1000ms 256MiB

Tic-Tac-Toe Winner Checker

Tic-Tac-Toe Winner Checker

You are given several Tic-Tac-Toe (or generalized MxM board) games. For each game, determine the winner based on the following condition: a player wins if one of the rows, columns, or one of the two main diagonals is filled entirely with their token (X or O).

If neither player has such a winning line, the result is DRAW.

Formally, given a square board of size \(M \times M\), a player wins if either:

  • There exists a row where \(\forall j,\ board[i][j] = \text{token}\),
  • There exists a column where \(\forall i,\ board[i][j] = \text{token}\), or
  • One of the main diagonals is entirely filled with \(\text{token}\) (i.e., \(\forall i,\ board[i][i] = \text{token}\) or \(\forall i,\ board[i][M-i-1] = \text{token}\)).

Check for X first; if X wins, output X. Otherwise, check for O and output O if they win. If neither condition is met, output DRAW.

inputFormat

The input is read from stdin and has the following format:

T
M
row1
row2
... 
rowM
M
row1
row2
... 
rowM
...

Here, the first line contains a single integer \(T\) — the number of games to evaluate. For each game, the first number \(M\) denotes the board size, followed by \(M\) lines each containing a string of length \(M\) comprised of characters 'X', 'O', or '.' (a dot indicating an empty cell).

outputFormat

For each test case (game), output a single line to stdout containing the result: X, O, or DRAW.

## sample
5
3
XOX
OX.
O.X
3
XOO
XXX
.O.
3
OOO
.X.
X.X
3
XOO
.X.
X.O
3
...
...
...
X

X O DRAW DRAW

</p>