Java 28ms BFS solution using one queue
I use two bits to save the information of pacific ocean and atlantic ocean.\n00: cannot reach any ocean\n01: can reach pacific ocean\n10: can reach atlantic oce
I use two bits to save the information of pacific ocean and atlantic ocean.\n`00`: cannot reach any ocean\n`01`: can reach pacific ocean\n`10`: can reach atlantic ocean\n`11`: can reach two oceans\n\n**Step 1**: Update the status of border cells and put them into the queue\n**Step 2**: Iterate the queue and explore the four directions. We only put a new cell into the queue if :\n- row and col index are valid\n- the height of the new cell is larger or equals to the height of the current cell\n- the new cell can benifit from the current cell (check status)\n\n```java\npublic class Solution {\n    public List<int[]> pacificAtlantic(int[][] matrix) {\n        List<int[]> res = new ArrayList<>();\n        int m = matrix.length;\n        if (m == 0) return res;\n        int n = matrix[0].length;\n        int[][] state = new int[m][n];\n        Queue<int[]> q = new LinkedList<>();\n        for (int i = 0; i < m; i++) {\n            state[i][0] |= 1;\n            if (i == m - 1 || n == 1) state[i][0] |= 2;\n            if (state[i][0] == 3) res.add(new int[]{i, 0});\n            q.add(new int[]{i, 0});\n            if (n > 1) {\n                state[i][n - 1] |= 2;\n                if (i == 0) state[i][n - 1] |= 1;\n                if (state[i][n - 1] == 3) res.add(new int[]{i, n - 1});\n                q.add(new int[]{i, n - 1});\n            }\n        }\n        for (int j = 1; j < n - 1; j++) {\n            state[0][j] |= 1;\n            if (m == 1) state[0][j] |= 2;\n            if (state[0][j] == 3) res.add(new int[]{0, j});\n            q.add(new int[]{0, j});\n            if (m > 1) {\n                state[m - 1][j] |= 2;\n                if (state[m - 1][j] == 3) res.add(new int[]{m - 1, j});\n                q.add(new int[]{m - 1, j});\n            }\n        }\n        int[][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\n        while (!q.isEmpty()) {\n            int[] cell = q.poll();\n            for (int[] dir : dirs) {\n                int row = cell[0] + dir[0];\n                int col = cell[1] + dir[1];\n                if (row < 0 || col < 0 || row == m || col == n || matrix[row][col] < matrix[cell[0]][cell[1]] || ((state[cell[0]][cell[1]] | state[row][col]) == state[row][col])) continue;\n                state[row][col] |= state[cell[0]][cell[1]];\n                if (state[row][col] == 3) res.add(new int[]{row, col});\n                q.add(new int[]{row, col});\n            }\n        }\n        return res;\n    }\n}\n```
[]