problems / 278

Alice and Bob's Finished Board

easy matrix arrays simulation 1000 ms · 256 MB

Alice and Bob's Finished Board

Alice and Bob played tic-tac-toe on a huge n × n board. Alice wrote the letter A in the cells she took, Bob wrote B, and some cells were never claimed and are still empty. Nobody kept track of the game while it was going on, so now they stare at the finished board and argue about who actually won.

A player has a line if at least one of the following is filled entirely with their letter (no empty cells and no letters of the other player):

  • one of the n rows,
  • one of the n columns,
  • the main diagonal, from the top-left corner to the bottom-right corner,
  • the anti-diagonal, from the top-right corner to the bottom-left corner.

Other diagonals do not count. Given the board, find out who has a line. The board may not be one a real game could end with; just look at the marks as they are.

Input

The first line contains an integer n (1 ≤ n ≤ 1000), the size of the board.

Each of the next n lines contains a string of exactly n characters, each of them A, B or . (an empty cell). The i-th line describes the i-th row from the top.

Output

Print one word:

  • ALICE if only Alice has a line,
  • BOB if only Bob has a line,
  • BOTH if both of them have a line,
  • NOBODY if neither of them has a line.

Notes

In the first example, Alice has the anti-diagonal (top-right to bottom-left). Bob's left column ends with Alice's A, so it is not his.

In the second example, Alice has the top row and Bob has the bottom row, so the answer is BOTH.

In the third example, the board is a single cell containing B: that one cell is a whole row by itself, so Bob has a line.

Example 1

Input

3
B.A
BA.
AB.

Output

ALICE

Example 2

Input

4
AAAA
.B.A
AB..
BBBB

Output

BOTH

Example 3

Input

1
B

Output

BOB

Hints · reveal one at a time if you're stuck

Hint 1

Only rows, columns and the two corner-to-corner diagonals count. How many lines is that, and how many cells would you read if you simply checked each one for n = 1000?

Hint 2

A line is a player's only if every cell holds their letter: a row with no B but with an empty . belongs to nobody. Check both players, and don't stop at the first line you find, since the answer may be BOTH.

report an issue Prefer your own editor? sooks prep 278 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.