C++ | BFS | Easy and intuitive
\nclass Solution {\npublic:\n    bool isSafe(int i,int j ,int r,int c){\n        if(i<0 || i >=r || j <0 || j >=c) return false;\n        return true;\n    }\n 
``` \nclass Solution {\npublic:\n    bool isSafe(int i,int j ,int r,int c){\n        if(i<0 || i >=r || j <0 || j >=c) return false;\n        return true;\n    }\n    bool isExit(int i,int j,int r ,int c){\n        if(i!=0 and j!=0 and i!=r-1 and j!=c-1) return false;\n         return true;  \n    }\n    int nearestExit(vector<vector<char>>& maze, vector<int>& entrance) {\n        int r = maze.size();\n        int c = maze[0].size();\n        queue<vector<int>>q;\n        q.push({0,entrance[0],entrance[1]});\n        int time,ei,ej;\n        vector<vector<int>>vis(r,(vector<int>(c,false)));\n        while(q.size()){\n            vector<int>tt = q.front();\n             ei = tt[1];\n             ej = tt[2];\n             time = tt[0];\n             q.pop();\n            if(isExit(ei,ej,r,c)){\n                if((entrance[0]!=ei or entrance[1]!=ej))\n                    return time;\n            }\n            if(isSafe(ei+1,ej,r,c) and !vis[ei+1][ej] and maze[ei+1][ej]!=\'+\'){\n                vis[ei+1][ej]=true;\n                q.push({time+1,ei+1,ej});\n            }\n            if(isSafe(ei-1,ej,r,c) and !vis[ei-1][ej] and maze[ei-1][ej]!=\'+\'){\n                vis[ei-1][ej]=true;\n                q.push({time+1,ei-1,ej});\n            }\n            if(isSafe(ei,ej+1,r,c) and !vis[ei][ej+1] and maze[ei][ej+1]!=\'+\'){\n                vis[ei][ej+1]=true;\n                q.push({time+1,ei,ej+1});\n            }\n            if(isSafe(ei,ej-1,r,c) and !vis[ei][ej-1]  and maze[ei][ej-1]!=\'+\'){\n                vis[ei][ej-1]=true;\n                q.push({time+1,ei,ej-1});\n            }\n        }\n        return -1;\n    }\n};\n```
[]