super clear  🐍 illustrated explanation
<-- please vote\n\n\n\n\nBFS solution\n\n    def allCellsDistOrder(self, rows: int, cols: int, rCenter: int, cCenter: int) -> List[List[int]]:\n        """ O(NM
<-- please vote\n\n![image](https://assets.leetcode.com/users/images/8f3860b6-dc65-4a78-b848-5a8a6a62db4b_1651839417.8394775.png)\n\n\nBFS solution\n\n    def allCellsDistOrder(self, rows: int, cols: int, rCenter: int, cCenter: int) -> List[List[int]]:\n        """ O(NM)TS """\n        grid = {(y, x): 1 for y in range(rows) for x in range(cols) if (y, x) != (rCenter, cCenter)}\n\n        def fn(level):\n            if level:\n                yield from level\n                yield from fn([pt for y, x in level for pt in ((y + 1, x), (y - 1, x), (y, x - 1), (y, x + 1)) if grid.pop(pt, False)])\n\n        return fn([(rCenter, cCenter)])\n\n    def allCellsDistOrder(self, rows: int, cols: int, rCenter: int, cCenter: int) -> List[List[int]]:\n        """ O(NM)TS """\n        grid = {(y, x): 1 for y in range(rows) for x in range(cols) if (y, x) != (rCenter, cCenter)}\n        level = [(rCenter, cCenter)]\n        while level:\n            yield from level\n            level = [(y, x) for y_, x_ in level for y, x in ((y_ + 1, x_), (y_ - 1, x_), (y_, x_ - 1), (y_, x_ + 1)) if grid.pop((y, x), False)]\n\nsort solution\n\n    def allCellsDistOrder(self, rows: int, cols: int, rCenter: int, cCenter: int) -> List[List[int]]:\n        """ O(NM log NM)T O(NM)S  """\n        arr = list(itertools.product(range(rows), range(cols)))\n        fn = lambda t: abs(t[0] - rCenter) + abs(t[1] - cCenter)\n        arr.sort(key=fn)\n        return arr
[]