Java DFS 9n^2 space
java\nclass Solution {\n    public int regionsBySlashes(String[] input) {\n        int N = input.length;\n        int[][] matrix = new int[3* N][3 * N];\n\n    
```java\nclass Solution {\n    public int regionsBySlashes(String[] input) {\n        int N = input.length;\n        int[][] matrix = new int[3* N][3 * N];\n\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < N; j++) {\n                if (input[i].charAt(j) == \'/\') {\n                    fillForwardSlash(matrix, i, j);\n                } else if (input[i].charAt(j) == \' \') {\n                    \n                } else {\n                    fillBackwardSlash(matrix, i, j);\n                }\n            }\n        }\n        \n        // for (int i = 0; i < matrix.length; i++)\n        // System.out.println(Arrays.toString(matrix[i]));\n\n        // here we have filled matrix\n        int count = 0;\n        for (int i = 0; i < matrix.length; i++) {\n            for (int j = 0; j < matrix.length; j++) {\n                if (matrix[i][j] == 0) {\n                    count++;\n                    fill(matrix, i, j);\n                }\n            }\n        }\n        return count;\n    }\n                                              \n    int[][] dirs = new int[][] {{1,0}, {0, 1}, {-1, 0}, {0, -1}};\n                                              \n    void fill(int[][] matrix, int i, int j) {\n        if (i < 0 || i == matrix.length || j < 0 \n            || j == matrix.length || matrix[i][j] == 1) return;\n        \n        matrix[i][j] = 1;\n        \n        for(var dir : dirs) {\n            fill(matrix, i + dir[0], j + dir[1]);\n        }\n    }\n\n    void fillForwardSlash(int[][] m, int row, int col) {\n        m[3 * row + 2][3 * col] = 1;\n        m[3 * row + 1][3 * col + 1] = 1;\n        m[3 * row][3 * col + 2] = 1;\n    }\n\n    void fillBackwardSlash(int[][] m, int row, int col) {\n        m[3 * row][3 * col] = 1;\n        m[3 * row + 1][3 * col + 1] = 1;\n        m[3 * row + 2][3 * col + 2] = 1;\n    }\n\n}\n```
[]