There is an m x n rectangular island that borders both the Pacific Ocean and Atlantic Ocean. The Pacific Ocean touches the island's left and top edges, and the Atlantic Ocean touches the island's right and bottom edges.

The island is partitioned into a grid of square cells. You are given an m x n integer matrix heights where heights[r][c] represents the height above sea level of the cell at coordinate (r, c).

The island receives a lot of rain, and the rain water can flow to neighboring cells directly north, south, east, and west if the neighboring cell's height is less than or equal to the current cell's height. Water can flow from any cell adjacent to an ocean into the ocean.

Return a 2D list of grid coordinates result where result[i] = [ri, ci] denotes that rain water can flow from cell (ri, ci) to both the Pacific and Atlantic oceans.

&nbsp;
Example 1:

Input: heights = [[1,2,2,3,5],[3,2,3,4,4],[2,4,5,3,1],[6,7,1,4,5],[5,1,1,2,4]]
Output: [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]]


Example 2:

Input: heights = [[2,1],[1,2]]
Output: [[0,0],[0,1],[1,0],[1,1]]


&nbsp;
Constraints:


	m == heights.length
	n == heights[r].length
	1 &lt;= m, n &lt;= 200
	0 &lt;= heights[r][c] &lt;= 105


class Solution:
    def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
        # Purpose: find the cells that allow rain flow into the ocean
        # Method: DFS
        # Intuition: start from each border, check cell and neb, if OK, append to res
            
        # init: res, vis (pac, atl), ROW, COL
        res = []
        pac = set()
        atl = set()
        ROW = len(heights)
        COL = len(heights[0])

        # top and bottom row
        for col in range(COL):
            self.dfs(0, col, pac, heights[0][col], heights)
            self.dfs(ROW-1, col, atl, heights[ROW-1][col], heights)

        # left and right col
        for row in range(ROW):
            self.dfs(row, 0, pac, heights[row][0], heights)
            self.dfs(row, COL-1, atl, heights[row][COL-1], heights)

        # append to res
        for row in range(ROW):
            for col in range(COL):
                if (row, col) in pac and (row, col) in atl:
                    res.append([row, col])

        # return
        return res

    
    def dfs(self, row, col, vis, prev, heights):
        # hard-code definition
        try:
            cur = heights[row][col]
        except:
            pass

        # inbound, unvisited, increase from ocean
        if (0<=row<len(heights) and 0<=col<len(heights[0])) \
            and (cur >= prev) \
                and ((row, col) not in vis):

            # add to visited 
            vis.add((row, col))

            # check nebs
            self.dfs(row+1, col, vis, cur, heights)
            self.dfs(row-1, col, vis, cur, heights)
            self.dfs(row, col+1, vis, cur, heights)
            self.dfs(row, col-1, vis, cur, heights)
class Solution {
    public List<List<Integer>> pacificAtlantic(int[][] heights) {
        if (heights == null) return null;
        if (heights.length == 0) return null;
        if (heights[0].length == 0) return null;

        /** */
        boolean [][] po = new boolean[heights.length][heights[0].length];
        boolean [][] ao = new boolean[heights.length][heights[0].length];

        for (int i = 0; i < heights[0].length; i ++) {
            dfs(heights, i, 0, 0, po); // top
            dfs(heights, i, heights.length - 1, 0, ao); // bottom
        }

        for (int i = 0; i < heights.length; i ++) {
            dfs(heights, 0, i, 0, po); // left
            dfs(heights, heights[0].length - 1, i, 0, ao); // right
        }

        List<List<Integer>> ans = new ArrayList<>();
        for (int i = 0; i < heights.length; i ++) {
            for (int j = 0; j < heights[i].length; j ++) {
                if (ao[i][j] && po[i][j]) {
                    ArrayList<Integer> ar = new ArrayList<>();
                    ar.add(i);
                    ar.add(j);
                    ans.add(ar);
                }
            }
        }

        return ans;
    }

    private void dfs(int[][] heights, int x, int y, int h, boolean[][] v) {
        if (x < 0 || y < 0 || x >= heights[0].length || y >= heights.length) return;
        if (v[y][x] || heights[y][x] < h) return;
        v[y][x] = true;
        /** left */
        dfs(heights, x - 1, y, heights[y][x], v);

        /** right */
        dfs(heights, x + 1, y, heights[y][x], v);

        /** up */
        dfs(heights, x, y - 1, heights[y][x], v);

        /** down */
        dfs(heights, x, y + 1, heights[y][x], v);
    }
}
class Solution {
public:
    void dfs(vector<vector<int>>& heights,vector<vector<bool>>&v,int i,int j)
    {
        int m=heights.size();
        int n=heights[0].size();
        v[i][j]=true;
        if(i-1>=0&&v[i-1][j]!=true&&heights[i-1][j]>=heights[i][j])
        {
            dfs(heights,v,i-1,j);
        }
         if(i+1<m&&v[i+1][j]!=true&&heights[i+1][j]>=heights[i][j])
        {
            dfs(heights,v,i+1,j);
        }
         if(j-1>=0&&v[i][j-1]!=true&&heights[i][j-1]>=heights[i][j])
        {
            dfs(heights,v,i,j-1);
        }
         if(j+1<n&&v[i][j+1]!=true&&heights[i][j+1]>=heights[i][j])
        {
            dfs(heights,v,i,j+1);
        }
    }
    vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {
        int m=heights.size();
        vector<vector<int>>ans;
        if(m==0)
        {
            return ans;
        }
        int n=heights[0].size();
        if(n==0)
        {
            return ans;
        }
        vector<vector<bool>>pa(m,vector<bool>(n));
        vector<vector<bool>>at(m,vector<bool>(n));
        for(int i=0;i<m;i++)
        {
            dfs(heights,pa,i,0);
            dfs(heights,at,i,n-1);
        }
        for(int j=0;j<n;j++)
        {
             dfs(heights,pa,0,j);
            dfs(heights,at,m-1,j);
        }
        for(int i=0;i<m;i++)
        {
            vector<int>p;
            for(int j=0;j<n;j++)
            {
                if(pa[i][j]&&at[i][j])
                {

                    p.push_back(i);
                    p.push_back(j);
                    ans.push_back(p);
                    p.clear();
                }

            }

        }
        return ans;

    }
};
`/**
 * @param {number[][]} heights
 * @return {number[][]}
 */
var pacificAtlantic = function(heights) {
    let atlantic = new Set();
    let pacific = new Set();
    let rows = heights.length;
    let cols = heights[0].length;

    for (let c = 0; c < cols; c++) {
        explore(heights, 0, c, pacific, heights[0][c]); // dfs from top row
        explore(heights, rows - 1, c, atlantic, heights[rows - 1][c]); // dfs from bottom row
    }

    for (let r = 0; r < rows; r++) {
        explore(heights, r, 0, pacific, heights[r][0]); // dfs from left most column
        explore(heights, r, cols - 1, atlantic, heights[r][cols - 1]); // dfs from right most column
    }

    // check if water can flow to both atlantic and pacific ocean.
    let res = [];
    for (let r = 0; r < rows; r++) {
        for (let c = 0; c < cols; c++) {
            let pos = r + ',' + c;
            if (atlantic.has(pos) && pacific.has(pos)) {
                res.push([r, c]);
            }
        }
    }

    return res;
};

function explore(heights, r, c, visited, prevHeight) {
    let rowInbound = 0 <= r && r < heights.length;
    let colInbound = 0 <= c && c < heights[0].length;
    if (!rowInbound || !colInbound) return;

    // height must be higher than prev height. water can only flow downwards not upwards. duh.
    // if it's the first value then it's just the same value so it's not less than so it will not return.
    if (heights[r][c] < prevHeight) return;

    let pos = r + ',' + c;
    if (visited.has(pos)) return;
    visited.add(pos)

    explore(heights, r + 1, c, visited, heights[r][c]) // below
    explore(heights, r - 1, c, visited, heights[r][c]) // above
    explore(heights, r, c + 1, visited, heights[r][c]) // right
    explore(heights, r, c - 1, visited, heights[r][c]) // left
}`
Pacific Atlantic Water Flow