The Goal

Win a fair match of two color-swapped games by claiming more total territory than your opponent. Add your territory scores from both games; the player with the higher combined score wins.

 Rules

A fair match consists of exactly two games with the players swapping colors and first move. Both games start from an empty 16 by 16 board and use identical rules. In game 1, player 0 is Black; in game 2, player 1 is Black. Black moves first in each game, then the players alternate.

The two independent games advance in parallel. On a match turn, each player receives at most one position and plays one move on the indicated board. While both games are active, both players act on different boards. If one game finishes first, play continues only on the remaining board. The match ends when both games are finished.

On your turn, place one stone of your color on an empty cell. Stones never move or disappear. The first stone may be placed anywhere. Every later placement must have no stone in any of its eight neighboring cells, including diagonals. A cell is legal only when it is empty and no existing stone is within Chebyshev distance 1.

There is no pass. If the current player has no legal placement, that individual game ends immediately; the other player receives no extra turn on that board. Empty cells at the end are forbidden cells. A timeout, malformed output or illegal placement forfeits the entire match. If both players forfeit on the same match turn, the match is a draw.

 Territory Scoring

Score every one of the 256 cells, including occupied cells. For each cell, group all stones into layers by squared Euclidean distance: dr*dr + dc*dc. Inspect layers from nearest to farthest. In a layer, compare the number of Black and White stones. If one color has more stones, that color receives one point for the cell and scoring of that cell stops. If the counts are equal, skip the layer and inspect the next one. If every layer is tied, the cell is unclaimed.

Each player's final match score is the sum of their raw territory points from both games, following their own color in each game. Compare these totals, not the number of individual games won. Equal combined totals are a draw. An occupied cell is normally awarded to its own stone because its distance to that stone is zero.

 Example

If the nearest distance layer for a cell contains one Black and one White stone, that layer is tied and gives no points. Continue to the next distance layer; its first unequal count decides ownership, for exactly one point.

Match example: player 0 scores 130 against 126 in game 1, then 120 against 136 in game 2. The totals are 250 against 262, so player 1 wins the match even though each won one individual game.

 Game Input

The input describes only the game on which you must act now. Read gameId and side on every turn. If you cache search state, keep separate state for each game.
Input

Line 1: five integers: gameId (1 or 2), side (1 for Black, 2 for White on this board), ply (stones already placed on this board), myScore and opponentScore (territory totals from completed games only; both are zero before either game finishes).

Next 16 lines: one board row each. B is a Black stone, W is a White stone, and . is empty.

Next line: legalActionCount.

Next legalActionCount lines: one legal action index per line.

Output
Output one legal action index in [0, 255]. The action maps to row = action / 16 and column = action % 16.
Constraints
Each board is always 16 by 16. There are two games per match. A final non-forfeit score is in [0, 512]. Response time is at most 1 second for your first decision of the match and 50 milliseconds for each later decision, including your first decision on the other board.