<p>An <code>n x n</code> grid is composed of <code>1 x 1</code> squares where each <code>1 x 1</code> square consists of a <code>&#39;/&#39;</code>, <code>&#39;\&#39;</code>, or blank space <code>&#39; &#39;</code>. These characters divide the square into contiguous regions.</p>

<p>Given the grid <code>grid</code> represented as a string array, return <em>the number of regions</em>.</p>

<p>Note that backslash characters are escaped, so a <code>&#39;\&#39;</code> is represented as <code>&#39;\\&#39;</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/15/1.png" style="width: 200px; height: 200px;" />
<pre>
<strong>Input:</strong> grid = [&quot; /&quot;,&quot;/ &quot;]
<strong>Output:</strong> 2
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/15/2.png" style="width: 200px; height: 198px;" />
<pre>
<strong>Input:</strong> grid = [&quot; /&quot;,&quot;  &quot;]
<strong>Output:</strong> 1
</pre>

<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/15/4.png" style="width: 200px; height: 200px;" />
<pre>
<strong>Input:</strong> grid = [&quot;/\\&quot;,&quot;\\/&quot;]
<strong>Output:</strong> 5
<strong>Explanation: </strong>Recall that because \ characters are escaped, &quot;\\/&quot; refers to \/, and &quot;/\\&quot; refers to /\.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == grid.length == grid[i].length</code></li>
	<li><code>1 &lt;= n &lt;= 30</code></li>
	<li><code>grid[i][j]</code> is either <code>&#39;/&#39;</code>, <code>&#39;\&#39;</code>, or <code>&#39; &#39;</code>.</li>
</ul>

class Solution:
    def regionsBySlashes(self, grid: List[str]) -> int:
        n = len(grid)
        cut = [   [  [ [0]*4 for _ in range(4) ] for _ in range(n + 2)  ] for _ in range(n + 2)   ]
        N, E, S, W = 0, 1, 2, 3

        for i in range(n):
            for j in range(n):
                c = grid[i][j]
                if c == '/':
                    cut[i][j][N][E] = 1
                    cut[i][j][E][N] = 1
                    cut[i][j][W][S] = 1
                    cut[i][j][S][W] = 1
                elif c == '\\':
                    cut[i][j][N][W] = 1
                    cut[i][j][W][N] = 1
                    cut[i][j][E][S] = 1
                    cut[i][j][S][E] = 1
        
        for j in range(n):
            cut[0][j][N][N] = 1
        for j in range(n):
            cut[n - 1][j][S][S] = 1
        for i in range(n):
            cut[i][0][W][W] = 1
        for i in range(n):
            cut[i][n - 1][E][E] = 1
        
        M = [  [ [0]*4 for _ in range(n) ] for _ in range(n)  ]
        def fill(i, j, d):
            if M[i][j][d]:
                return 0

            M[i][j][d] = 1
            if d == N:
                if not cut[i][j][N][N]:
                    fill(i - 1, j, S)
                if not cut[i][j][N][E]:
                    fill(i, j, E)
                if not cut[i][j][N][W]:
                    fill(i, j, W)
            elif d == E:
                if not cut[i][j][E][E]:
                    fill(i, j + 1, W)
                if not cut[i][j][E][N]:
                    fill(i, j, N)
                if not cut[i][j][E][S]:
                    fill(i, j, S)
            elif d == S:
                if not cut[i][j][S][S]:
                    fill(i + 1, j, N)
                if not cut[i][j][S][E]:
                    fill(i, j, E)
                if not cut[i][j][S][W]:
                    fill(i, j, W)
            elif d == W:
                if not cut[i][j][W][W]:
                    fill(i, j - 1, E)
                if not cut[i][j][W][N]:
                    fill(i, j, N)
                if not cut[i][j][W][S]:
                    fill(i, j, S)
            return 1
        
        cnt = 0
        for i in range(n):
            for j in range(n):
                for d in range(4):
                    if fill(i, j, d):
                        cnt += 1
        return cnt
22126