import java.util.*; public class Stake { public static Scanner in; public static int caseNum,n; public static char[][] board; public static int count[]; public static ArrayList moves; public static final int MAX = 1; public static final int MIN = 0; public static int bestScore = 0; public static Pair bestFirst = null; public static void main(String[] args) { in = new Scanner(System.in); caseNum = 0; while ((n = in.nextInt()) > 0) { caseNum++; board = new char[n][n]; count = new int[2]; count[0] = count[1] = 0; moves = new ArrayList(); for (int row = 0; row < n; row++) { String r = in.next(); for (int col = 0; col < n; col++) { board[row][col] = r.charAt(col); if (board[row][col] == '0') count[0]++; else if (board[row][col] == '1') count[1]++; else moves.add(new Pair(row,col)); } } solve(); System.out.printf("(%d,%d) %d\n",bestFirst.row,bestFirst.col,bestScore); } } public static void solve() { if (count[0] > count[1]) { bestScore = ab(1,MAX,Integer.MIN_VALUE, Integer.MAX_VALUE,moves,0); } else { bestScore = ab(0,MAX,Integer.MIN_VALUE,Integer.MAX_VALUE,moves,0); } } public static int ab(int player,int type,int alpha, int beta, ArrayList moves,int level) { int temp; int turn; if (moves.size() == 0) { int v = evaluate(player) -evaluate(1-player); return v; } else { if (type == MAX) { for (int i = 0; i < moves.size(); i++) { Pair move = moves.get(i); moves.remove(i); board[move.row][move.col] = (char)('0'+player); temp = ab(player,MIN,alpha,beta,moves,level+1); board[move.row][move.col] = '.'; moves.add(i,move); if (level == 0 && temp > alpha && alpha < beta) bestFirst = move; alpha = Math.max(alpha,temp); if (alpha >= beta) return beta; } return alpha; } else { for (int i = 0; i < moves.size(); i++) { Pair move = moves.get(i); moves.remove(i); board[move.row][move.col] = (char)('0'+1-player); temp = ab(player,MAX,alpha,beta,moves,level+1); board[move.row][move.col] = '.'; moves.add(i,move); beta = Math.min(beta,temp); if (beta <= alpha) return alpha; } return beta; } } } public static int evaluate(int i) { char c = (char)('0'+i); int max = 0; char temp[][] = new char[n][n]; for (int row = 0; row < n; row++) for (int col = 0; col < n; col++) temp[row][col] = board[row][col]; for (int row = 0; row < n; row++) { for (int col = 0; col < n; col++) { if (temp[row][col] == c) { max = Math.max(max,fill(temp,c,row,col)); } } } return max; } public static int fill(char temp[][], char c, int row, int col) { int sum = 1; temp[row][col] = '.'; if (row -1 >= 0 && temp[row-1][col] == c) sum += fill(temp,c,row-1,col); if (row +1 < n && temp[row+1][col] == c) sum += fill(temp,c,row+1,col); if (col -1 >= 0 && temp[row][col-1] == c) sum += fill(temp,c,row,col-1); if (col +1 < n && temp[row][col+1] == c) sum += fill(temp,c,row,col+1); return sum; } public static void display() { for (int row = 0; row < n; row++) { for (int col = 0; col < n; col++) System.out.print(board[row][col]); System.out.println(); } } } class Pair { public int row, col; public Pair(int r, int c) { row = r; col = c; } }