1. 


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
* FWP, Ausgewählte Probleme aus dem ACM Programming Contest, SS11
* Problem: 260 - Il Gioco dell'X
* Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=4&page=show_problem&problem=196
*
* @author Burgmair Stefan
* @author YYY
* @version 1.0, 05/05/2011
*
* Method : Ad-Hoc
* Status : Accepted
* Runtime: 0.248
*/

public class Main
{
static int n;
static boolean[][] board;
static boolean[][] isVisited;

public static void main(String[] args) throws IOException
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String input;
int gameCounter = 1;
do{
n = Integer.parseInt(reader.readLine());
if(n != 0){
board = new boolean[n][n];
isVisited = new boolean[n][n];
boolean blackHasWon = false;

for (int row = 0; row < n; row ++){
input = reader.readLine();
for (int col = 0; col < n; col ++){
isVisited[row][col] = false;
if (input.charAt(col) == 'b')
board[row][col] = true;
else
board[row][col] = false;
}
}

//Start beim ersten schwarzen Spielstein
for (int col = 0; col < n; col ++){
if(board[0][col] = true)
if(flooding(0, col)){
blackHasWon = true;
break;
}
}

//Methodenaufruf
if (blackHasWon)
System.out.println(gameCounter + " B");
else
System.out.println(gameCounter + " W");

gameCounter ++;
}
}while (reader.ready());
}

public static boolean flooding(int row, int col){
if (row + 1 == n)
return true;

//finde nachbarn
if (row - 1 >= 0 && col - 1 >= 0 )
if(board[row - 1][col - 1] == true && isVisited[row - 1][col - 1] == false){
isVisited[row - 1][col - 1] = true;
if (flooding(row - 1, col - 1))
return true;
}
if (row - 1 >= 0)
if(board[row - 1][col] == true && isVisited[row - 1][col] == false){
isVisited[row - 1][col] = true;
if (flooding(row - 1, col))
return true;
}
if (col - 1 >= 0 )
if(board[row][col - 1] == true && isVisited[row][col - 1] == false){
isVisited[row][col - 1] = true;
if (flooding(row, col - 1))
return true;
}
if (col + 1 < n)
if(board[row][col + 1] == true && isVisited[row][col + 1] == false){
isVisited[row][col + 1] = true;
if (flooding(row, col + 1))
return true;
}
if (row + 1 < n)
if(board[row + 1][col] == true && isVisited[row + 1][col] == false){
isVisited[row + 1][col] = true;
if (flooding(row + 1, col))
return true;
}
if (row + 1 < n && col + 1 < n)
if(board[row + 1][col + 1] == true && isVisited[row + 1][col + 1] == false){
isVisited[row + 1][col + 1] = true;
if (flooding(row + 1, col + 1))
return true;
}
return false;
}
}