Python3 Breadth First Search Solution
\n# Complexity\n- Time complexity: O(n)\n\n- Space complexity: O(n)\n\n# Code\n\nclass Solution:\n    def nearestExit(self, maze: List[List[str]], entrance: Lis
\n# Complexity\n- Time complexity: O(n)\n\n- Space complexity: O(n)\n\n# Code\n```\nclass Solution:\n    def nearestExit(self, maze: List[List[str]], entrance: List[int]) -> int:\n        m = len(maze)\n        n = len(maze[0])\n\n        queue = collections.deque()\n        queue.append((entrance[0], entrance[1], 0))\n        visited = set()\n        visited.add((entrance[0], entrance[1]))\n\n        while queue:\n            for _ in range(len(queue)):\n                r, c, level = queue.popleft()\n\n                if [r, c] != entrance:\n                    if r == 0 or r == m-1 or c == 0 or c == n-1:\n                        return level\n\n                for nr, nc in [(r, c+1), (r, c-1), (r+1, c), (r-1, c)]:\n                    if 0 <= nr < m and 0 <= nc < n and (nr, nc) not in visited and maze[nr][nc] == \'.\':\n                        queue.append((nr, nc, level + 1))\n                        visited.add((nr, nc))\n\n        return -1\n        \n```
['Python3']