O(N) Java BFS
\npublic int[][] allCellsDistOrder(int R, int C, int r0, int c0) {\n    boolean[][] visited = new boolean[R][C];\n    int[][] result = new int[R * C][2];\n    i
```\npublic int[][] allCellsDistOrder(int R, int C, int r0, int c0) {\n    boolean[][] visited = new boolean[R][C];\n    int[][] result = new int[R * C][2];\n    int i = 0;\n    Queue<int[]> queue = new LinkedList<int[]>();\n    queue.offer(new int[]{r0, c0});\n    while (!queue.isEmpty()) {\n      int[] cell = queue.poll();\n      int r = cell[0];\n      int c = cell[1];\n\n      if (r < 0 || r >= R || c < 0 || c >= C) {\n        continue;\n      }\n      if (visited[r][c]) {\n        continue;\n      }\n\n      result[i] = cell;\n      i++;\n      visited[r][c] = true;\n\n      queue.offer(new int[]{r, c - 1});\n      queue.offer(new int[]{r, c + 1});\n      queue.offer(new int[]{r - 1, c});\n      queue.offer(new int[]{r + 1, c});\n    }\n    return result;\n  }\n  ```
[]