Python: DFS from Ocean, easy to understand
Divide the problem into the set of cell where water can run into the Pacific and the set of cell where water can run into the Atlantic. Then we join two sets to
Divide the problem into the set of cell where water can run into the Pacific and the set of cell where water can run into the Atlantic. Then we join two sets to get the result.\nFor the Pacific subset, start running from the ocean (edge where row = 0 or col = 0), and find all the cells which are greater or equal.\nSimilar for the Atlantic.\n```\nclass Solution(object):\n    def pacificAtlantic(self, matrix):\n        """\n        :type matrix: List[List[int]]\n        :rtype: List[List[int]]\n        """\n        results = []\n        if not len(matrix) or not len(matrix[0]):\n            return results\n        rows, cols = len(matrix), len(matrix[0])\n        directions = [[0, 1], [0, -1], [1, 0], [-1, 0]]\n        \n        def Pacific(mat):\n            visited = set()\n            for j in range(cols):\n                dfs(mat, 0, j, visited)\n            for i in range(rows):\n                dfs(mat, i, 0, visited)\n            return visited        \n        \n        def Atlantic(mat):\n            visited = set()\n            for j in reversed(range(cols)):\n                dfs(mat, rows-1, j, visited)\n            for i in reversed(range(rows)):\n                dfs(mat, i, cols-1, visited)\n            return visited\n        \n        def dfs(mat, i, j, visited):\n            if (i, j) in visited:\n                return\n            visited.add((i, j))\n            for direction in directions:\n                next_i, next_j = i+direction[0],j+direction[1]\n                if 0 <= next_i < rows and 0 <= next_j < cols and mat[next_i][next_j] >= mat[i][j]:\n                    dfs(mat, next_i, next_j, visited)\n        \n        atlantic = Atlantic(matrix)\n        pacific = Pacific(matrix)\n        for i, j in atlantic:\n            if (i,j) in pacific:\n                results.append([i,j])\n        return results\n```
[]