BFS Clean c# solution
\n    public int NearestExit(char[][] maze, int[] entrance) {\n        var dirs = new [] {0, 1, 0, -1, 0};\n        var q = new Queue<(int, int, int)>();\n     
```\n    public int NearestExit(char[][] maze, int[] entrance) {\n        var dirs = new [] {0, 1, 0, -1, 0};\n        var q = new Queue<(int, int, int)>();\n        q.Enqueue((entrance[0], entrance[1], 0));\n        \n        while(q.Any())\n        {\n            var (i, j, dist) = q.Dequeue();\n            if(i < 0 || i == maze.Length) continue;\n            if(j < 0 || j == maze[i].Length) continue;\n            if(maze[i][j] != \'.\') continue;\n            maze[i][j] = \'_\';\n            if(dist > 0 && (i == 0 || i == maze.Length - 1)) return dist;\n            if(dist > 0 && (j == 0 || j == maze[i].Length - 1)) return dist;\n            \n            for(var k = 0; k < dirs.Length - 1; k++)\n                q.Enqueue((i + dirs[k], j + dirs[k + 1], dist + 1));\n        }\n        \n        return -1;\n    }\n```\nTime complexety O(n*m).\nSpace complexy O(1) if we can modify the maze array and (n*m) if not.
['Breadth-First Search', 'C#']