DFS on upscaled grid
We can upscale the input grid to [n * 3][n * 3] grid and draw "lines" there. Then, we can paint empty regions using DFS and count them. Picture below says it al
We can upscale the input grid to `[n * 3][n * 3]` grid and draw "lines" there. Then, we can paint empty regions using DFS and count them. Picture below says it all. Note that `[n * 2][n * 2]`  grid does not work as "lines" are too thick to identify empty areas correctly.\n\n> This transform this problem into [200. Number of Islands](https://leetcode.com/problems/number-of-islands/), where lines (\'1\') are the water, and rest (\'0\') is the land.\n\nI came up with this solution after I drew a few examples, and because I was lazy and saw it as an easy way. \n\n![image](https://assets.leetcode.com/users/votrubac/image_1544935075.png)\n\n**C++**\n```\nint dfs(vector<vector<int>> &g, int i, int j) {\n    if (min(i, j) < 0 || max(i, j) >= g.size() || g[i][j] != 0)\n        return 0;\n    g[i][j] = 1;\n    return 1 + dfs(g, i - 1, j) + dfs(g, i + 1, j) + dfs(g, i, j - 1) + dfs(g, i, j + 1);\n}\nint regionsBySlashes(vector<string>& grid) {\n    int n = grid.size(), regions = 0;\n    vector<vector<int>> g(n * 3, vector<int>(n * 3, 0));\n    for (int i = 0; i < n; ++i)\n        for (int j = 0; j < n; ++j)\n            if (grid[i][j] == \'/\') \n                g[i * 3][j * 3 + 2] = g[i * 3 + 1][j * 3 + 1] = g[i * 3 + 2][j * 3] = 1;\n            else if (grid[i][j] == \'\\\\\') \n                g[i * 3][j * 3] = g[i * 3 + 1][j * 3 + 1] = g[i * 3 + 2][j * 3 + 2] = 1;\n    for (int i = 0; i < n * 3; ++i)\n        for (int j = 0; j < n * 3; ++j)\n            regions += dfs(g, i, j) ? 1 : 0;    \n    return regions;\n}\n```\n**Java**\n```java\nint dfs(int[][] g, int i, int j) {\n    if (Math.min(i, j) < 0 || Math.max(i, j) >= g.length || g[i][j] != 0)\n        return 0;\n    g[i][j] = 1;\n    return 1 + dfs(g, i - 1, j) + dfs(g, i + 1, j) + dfs(g, i, j - 1) + dfs(g, i, j + 1);\n}    \npublic int regionsBySlashes(String[] grid) {\n    int n = grid.length, regions = 0;\n    int[][] g = new int[n * 3][n * 3];\n    for (int i = 0; i < n; ++i)\n        for (int j = 0; j < n; ++j)\n            if (grid[i].charAt(j) == \'/\') \n                g[i * 3][j * 3 + 2] = g[i * 3 + 1][j * 3 + 1] = g[i * 3 + 2][j * 3] = 1;\n            else if (grid[i].charAt(j) == \'\\\\\') \n                g[i * 3][j * 3] = g[i * 3 + 1][j * 3 + 1] = g[i * 3 + 2][j * 3 + 2] = 1;\n    for (int i = 0; i < n * 3; ++i)\n        for (int j = 0; j < n * 3; ++j)\n            regions += dfs(g, i, j) > 0 ? 1 : 0;        \n    return regions;\n}\n```\n**Python 3**\n```python\nclass Solution:\n    def regionsBySlashes(self, grid: List[str]) -> int:\n        def dfs(i: int, j: int) -> int:\n            if min(i, j) < 0 or max(i, j) >= len(g) or g[i][j] != 0:\n                return 0\n            g[i][j] = 1\n            return 1 + dfs(i - 1, j) + dfs(i + 1, j) + dfs(i, j - 1) + dfs(i, j + 1)\n        n, regions  = len(grid), 0\n        g = [[0] * n * 3 for i in range(n * 3)]\n        for i in range(n):\n            for j in range(n):\n                if grid[i][j] == \'/\':\n                    g[i * 3][j * 3 + 2] = g[i * 3 + 1][j * 3 + 1] = g[i * 3 + 2][j * 3] = 1\n                elif grid[i][j] == \'\\\\\':\n                    g[i * 3][j * 3] = g[i * 3 + 1][j * 3 + 1] = g[i * 3 + 2][j * 3 + 2] = 1\n        for i in range(n * 3):\n            for j in range(n * 3):\n                regions += 1 if dfs(i, j) > 0 else 0\n        return regions\n```
['C', 'Java', 'Python3']