Python BFS Solution O(n*m)
\nclass Solution:\n    def nearestExit(self, maze: List[List[str]], entrance: List[int]) -> int:\n        \n        ROWS, COLS = len(maze), len(maze[0])\n      
```\nclass Solution:\n    def nearestExit(self, maze: List[List[str]], entrance: List[int]) -> int:\n        \n        ROWS, COLS = len(maze), len(maze[0])\n        visited = set()\n        queue = deque()\n        queue.append((tuple(entrance), 0))\n        \n        while queue:\n            element = queue.popleft()\n            \n            position = element[0]\n            step = element[1]\n            \n            # Check if this position is invalid.\n            if (position[0] < 0 or position[0] > ROWS-1\n                or position[1] < 0 or position[1] > COLS-1\n                or position in visited or maze[position[0]][position[1]] == \'+\'):\n                continue\n            \n            # Check if this position is the answer.\n            if (position[0] == 0 or position[0] == ROWS-1\n                or position[1] == 0 or position[1] == COLS-1):\n                if step != 0 :\n                    return step\n            visited.add(position) \n            queue.append(((position[0]+1, position[1]), step+1))\n            queue.append(((position[0], position[1]+1), step+1))\n            queue.append(((position[0]-1, position[1]), step+1))\n            queue.append(((position[0], position[1]-1), step+1))\n\n        return -1\n```
['Breadth-First Search', 'Queue', 'Python']