public int bestPosition(String[] board) {
        Set<Integer> badRows = new HashSet<Integer>();
        Set<Integer> badCols = new HashSet<Integer>();

        int boardLen = board.length;
        for (int i = 0; i < boardLen; i++) {
            String line = board[i];
            int lineLen = line.length();
            for (int j = 0; j < lineLen; j++) {
                if (line.charAt(j) == 'X') {
                    badRows.add(i);
                    badCols.add(j);
                }
            }
        }

        int minSafe = Integer.MAX_VALUE;
        boolean found = false;
        for (int i = 0; i < boardLen; i++) {
            if (badRows.contains(i)) {
                continue;
            }

            String line = board[i];
            int lineLen = line.length();
            for (int j = 0; j < lineLen; j++) {
                if (badCols.contains(j)) {
                    continue;
                }

                int safeCount = countSafe(board, badRows, badCols, i, j);
                if (safeCount < minSafe) {
                    minSafe = safeCount;
                    found = true;
                }
            }
        }

        return found ? minSafe : -1;
    }