Straightforward Java BST
\nclass Solution {\n    int[][] dirs = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};\n    \n    public int[][] allCellsDistOrder(int R, int C, int r0, int c0) {\n        
```\nclass Solution {\n    int[][] dirs = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};\n    \n    public int[][] allCellsDistOrder(int R, int C, int r0, int c0) {\n        int[][] res = new int[R * C][2];\n        boolean[][] visited = new boolean[R][C]; \n        visited[r0][c0] = true;\n        Queue<int[]> queue = new ArrayDeque<>();\n        queue.offer(new int[]{r0, c0});\n        int i = 0;\n        while (!queue.isEmpty()) {\n            int[] cur = queue.poll();\n            res[i][0] = cur[0];\n            res[i++][1] = cur[1];\n            for (int[] dir : dirs) {\n                int row = cur[0] + dir[0];\n                int col = cur[1] + dir[1];\n                if (row >= 0 && row < R && col >= 0 && col < C && !visited[row][col]) {\n                    visited[row][col] = true;\n                    queue.offer(new int[]{row, col});\n                }\n            }\n        }\n        return res;\n    }\n}\n```
[]