BFS Solution without Vis. Matrix (commented)
\nclass Solution {\npublic:\n    int nearestExit(vector<vector<char>>& maze, vector<int>& entry) {\n        int n = maze.size(), m = maze[0].size();\n        ve
```\nclass Solution {\npublic:\n    int nearestExit(vector<vector<char>>& maze, vector<int>& entry) {\n        int n = maze.size(), m = maze[0].size();\n        vector<vector<int>> dist(n, vector <int> (m, 0));\n        queue <pair <int, int>> q;\n        int i = entry[0], j = entry[1];\n\t\tq.push({i, j});  // insert the entrance\n        maze[i][j] = \'+\';  // mark entrance visited\n\t\t\n\t\t// 4 direction\n        int    dx[] = {-1,  0,  1,  0};\n        int    dy[] = { 0,  1,  0, -1};\n        while(q.size()) {\n            int x = q.front().first, y = q.front().second; // current cell\n            q.pop();\n        \n            for(int i = 0; i<4; i++) {  // iterate over neighbours\n\t\t\t\tint xi = x+dx[i],  yi = y+dy[i];   // current neighbour\n\t\t\t\t\n                if(xi >= 0 && xi < n && yi >= 0 && yi < m) {   // if not out of the maze\n                    if(maze[xi][yi] == \'.\') {   // if empty\n                        q.push({xi, yi});\n                        dist[xi][yi] = dist[x][y] + 1;  // increment the dist from entrance\n                        maze[xi][yi] = \'+\';  // mark visited\n                        if(xi == 0 || xi == n-1 || yi == 0 || yi == m-1) {\n                            return dist[xi][yi];   // if on border, return res\n                        }\n                    }\n                }\n            }\n        }\n        return -1;\n    }\n};\n\n// TC: O(n*m)\n// SC: O(n*m)\n```
['Breadth-First Search', 'C++']