C++ | BFS | Queue✅🔥
Complexity
Time complexity: O(N*M)

Space complexity: O(N*M)
Code
# Complexity
- Time complexity: O(N*M)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O(N*M)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
class Solution {

private:

    void bfs(queue<pair<pair<int,int>,int>>& q,  vector<vector<int>>& vis, vector<vector<int>>& graph,int n,int m){

        while(!q.empty()){

            int row1 = q.front().first.first;   // for row
            int col1 = q.front().first.second;  // for col
            int step = q.front().second;        // for height
            q.pop();
            // graph[row1][col1] = step;

            int a[4] = {0, -1, 0, 1};           //traverse in all 4 directions
            int b[4] = {-1, 0 ,1 ,0};

            for(int i=0;i<4;i++){
                int nrow = row1+a[i];
                int ncol = col1+b[i];

                if(nrow>=0 and nrow<n and ncol>=0 and ncol<m and vis[nrow][ncol]==0){ 
                // check if cell is valid or not and vis[nrow][ncol]==1 is not taken as it has itself as min height
                    vis[nrow][ncol] = 1;
                    graph[nrow][ncol] = step + 1;
                    q.push({{nrow,ncol},step+1});    // if it has 0 then push into queue

                }
            }
        }
    }
public:
    vector<vector<int>> highestPeak(vector<vector<int>>& isWater) {

        int n = isWater.size();
        int  m = isWater[0].size();

        vector<vector<int>>vis(n,vector<int>(m,0)); // as replica of isWater matrix;

        vector<vector<int>>graph(n,vector<int>(m,0)); // to store the height ;

        queue<pair<pair<int,int>,int>>q;  // to sore the [[row, col], height]

        for(int i=0;i<n;i++){           // doing replica of isWater
            for(int j=0;j<m;j++){

                if(isWater[i][j]==1){
                    vis[i][j]=1;
                    q.push({{i,j},0});    // initial height is zero;
                }
            }
        }
        bfs(q,vis,graph,n,m);   // call to get all heights and the  -> inital queue is filled from above for loop.
        return graph;
        
    }
};
```
['C++']