<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 = 3 * len(grid)
        repr_grid = [[1] * (n) for _ in range(n)]

        for r, row in enumerate(grid):
            c = 0
            row = list(row)
            for c, ch in enumerate(row):
                if ch == "\\":
                    self.fill_bw_slash(repr_grid, 3 * r, 3 * c)
                elif ch == "/":
                    self.fill_fw_slash(repr_grid, 3 * r + 2, 3 * c)

        def valid_rc(row: int, col: int):
            if 0 <= row < n and 0 <= col < n:
                return True

            return False

        def flood_island(row: int, col: int):
            repr_grid[row][col] = 0

            directions = [[-1, 0], [1, 0], [0, -1], [0, 1]]
            for dr, dc in directions:
                new_r = row + dr
                new_c = col + dc

                if valid_rc(new_r, new_c) and repr_grid[new_r][new_c]:
                    flood_island(new_r, new_c)

        num_islands = 0

        for r in range(n):
            for c in range(n):
                if repr_grid[r][c] == 1:
                    num_islands += 1
                    flood_island(r, c)

        return num_islands

    def fill_bw_slash(self, grid: List[List[int]], tl_x: int, tl_y: int):
        for _ in range(3):
            grid[tl_x][tl_y] = 0
            tl_x += 1
            tl_y += 1

    def fill_fw_slash(self, grid: List[List[int]], bl_x: int, bl_y: int):
        for _ in range(3):
            grid[bl_x][bl_y] = 0
            bl_x -= 1
            bl_y += 1

18211