BFS | C++
\nclass Solution {\npublic:\n    int m,n;\n    int dx[4]={1,0,-1,0};\n    int dy[4]={0,1,0,-1};\n    queue<pair<int,int>> pac;\n    queue<pair<int,int>> atl;\n 
```\nclass Solution {\npublic:\n    int m,n;\n    int dx[4]={1,0,-1,0};\n    int dy[4]={0,1,0,-1};\n    queue<pair<int,int>> pac;\n    queue<pair<int,int>> atl;\n    \n    bool isValid(int x, int y){\n        return x>=0 && x<m && y>=0 && y<n;\n    }\n    \n    void bfs(queue<pair<int,int>> &q, vector<vector<int>> &vis, vector<vector<int>>& matrix){\n        while(!q.empty()){\n            int x=q.front().first;\n            int y=q.front().second;\n            vis[x][y]=1;\n            q.pop();\n            for(int k=0;k<4;k++){\n                int xx=x+dx[k];\n                int yy=y+dy[k];\n                if(isValid(xx,yy) && matrix[x][y]<=matrix[xx][yy] && vis[xx][yy]==0){\n                    q.push({xx,yy});\n                }\n            }\n        }\n    }\n    \n        \n    vector<vector<int>> pacificAtlantic(vector<vector<int>>& matrix) {\n        vector<vector<int>> ans;\n        m=matrix.size();\n        if(m==0)\n            return ans;\n        n=matrix[0].size();\n        if(n==0)\n            return ans;\n        vector<vector<int>> visp(m, vector<int>(n,0));\n        vector<vector<int>> visq(m, vector<int>(n,0));\n        for(int i=m-1;i>=0;i--)\n            pac.push({i,0});\n        for(int i=n-1;i>=0;i--)\n            pac.push({0,i});\n        for(int i=m-1;i>=0;i--)\n            atl.push({i,n-1});\n        for(int i=n-1;i>=0;i--)\n            atl.push({m-1,i});\n        bfs(pac,visp,matrix);\n        bfs(atl,visq,matrix);\n        for(int i=0;i<m;i++){\n            for(int j=0;j<n;j++){\n                if(visp[i][j]==1 && visq[i][j]==1)\n                    ans.push_back({i,j});\n            }\n        }\n        return ans;\n    }\n};\n```
[]