Easy BFS solution (self explanatory with code comments) - Beats 100%
Intuition\n Describe your first thoughts on how to solve this problem. \nStarting from the entrance position, perform BFS until we find a boundary cell. Return 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nStarting from the entrance position, perform BFS until we find a boundary cell. Return -1 if we don\'t \n# Complexity\n- Time complexity: O(m x n) - traversing each cell only once \n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(m x n) - worst case we will store mxn entries in the queue \n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public int nearestExit(char[][] maze, int[] entrance) {\n         return bfs(maze, entrance); // bfs is better than dfs because we will be exploring all the possible paths in a single iteration, giving us the minimum path in least amount of exploration. \n\n    }\n\n    \n    private int bfs(char[][] maze, int[] entrance){\n        int m = maze.length, n = maze[0].length; \n\n        // Directions array {Right, left, down, up}\n        int[] rowoffset = {0, 0, 1, -1};\n        int[] coloffset = {1, -1, 0, 0};\n\n        maze[entrance[0]][entrance[1]] = \'+\'; // marking the entry as visited\n        \n       \n        Queue<Cell> q = new LinkedList<>();\n        int steps = 0;\n        \n        //Adding all the neighbors of entry so we don\'t consider entry as exit\n        for(int i=0; i<4; i++){\n            int x = entrance[0] + rowoffset[i];\n            int y = entrance[1] + coloffset[i];\n            if(isWithinBoundary(x, y, m, n) && !isWall(x, y, maze)){\n                q.add(new Cell(x, y)); \n                maze[x][y] = \'+\';\n            }\n        }\n        // BFS template\n            // Add to the root to the queue\n            // forEach node in Queue : \n                // pop a node\n                // find  and add valid neighbors to the queue\n                // Repeat until queue is not empty\n            // Return -1, if we come out of the loop\n                \n        while(!q.isEmpty()){\n            steps++; \n            int size = q.size(); \n            for( int i=0; i<size; i++){\n                Cell cell = q.poll(); \n                for(int j=0; j<4; j++){\n                    int x = cell.x + rowoffset[j];\n                    int y = cell.y + coloffset[j];\n                    if(!isWithinBoundary(x, y, m, n)) return steps; \n                    else {\n                        if(!isWall(x, y, maze)){\n                            q.add(new Cell(x, y));\n                            maze[x][y] = \'+\';\n                        }\n                    }\n                }\n            }\n        }\n        return -1;\n\n    }\n\n    private boolean isWithinBoundary(int x, int y, int m, int n){\n        if(x < 0 || x>= m) return false;\n        if( y< 0 || y >= n) return false; \n        return true; \n    }\n\n    private boolean isWall(int x, int y, char[][] maze){\n        return maze[x][y] == \'+\';\n    }\n\n    // Helper class to easily manage the cell coordinates\n    class Cell {\n        int x;\n        int y;\n\n        public Cell(int x_, int y_){\n            x = x_;\n            y = y_; \n        }\n    }\n}\n```
['Breadth-First Search', 'Java']