C++  BFS solution || commented
A similar problem -\n1765. Map of highest peak\n\nclass Solution {\npublic:\n    int nearestExit(vector<vector<char>>& maze, vector<int>& e) {\n        queue<pa
A similar problem -\n[1765. Map of highest peak](https://leetcode.com/problems/map-of-highest-peak/)\n```\nclass Solution {\npublic:\n    int nearestExit(vector<vector<char>>& maze, vector<int>& e) {\n        queue<pair<int,int>> q;\n        q.push({e[0],e[1]});\n\t\t\n\t\t//current moves\n        int moves=1;\n        int rows=maze.size();\n        int cols=maze[0].size();\n        \n\t\t//to move in all directions\n        vector<vector<int>> offsets={{0,1},{1,0},{0,-1},{-1,0}};\n\t\t\n        //mark the entrance  as visited\n        maze[e[0]][e[1]]=\'+\';\n        while(!q.empty())\n        {\n            int l=q.size();\n\t\t\t//for every node in the queue visit all of it\'s adjacent nodes which are not visited yet\n            for(int k=0;k<l;k++)\n            {\n                auto [i,j]=q.front();\n                q.pop();\n                \n\t\t\t\t//try all 4 directions from the current cell\n                for(int l=0;l<4;l++)\n                {\n                    int x=i+offsets[l][0];\n                    int y=j+offsets[l][1];\n\t\t\t\t\t//a invalid move\n                    if(x<0 || y<0 || x>=rows || y>=cols || maze[x][y]==\'+\')\n                        continue;\n\t\t\t\t\t//if we have reached the exit then current moves are the min moves to reach the exit\n                    if(x==0 || y==0 || x==rows-1 || y==cols-1)\n                        return moves;\n\t\t\t\t\t//block the cell as we have visited\n                    maze[x][y]=\'+\';\n                    q.push({x,y});\n                }\n            }\n\t\t\t//increment the moves\n            moves++;\n            \n        }\n        return -1;\n    }\n};\n```\n**Upvote if this helps you :)**
[]