for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { if (i == 0 || i == row - 1 || j == 0 || j == col - 1) { if (board[i][j] == 'O') { deque.offer(new Integer[]{i, j}); isVisited[i][j] = true; } } } }