C# Solution | Graph, BFS
C#\npublic class Solution {\n    public int[][] BuildMatrix(int k, int[][] rowConditions, int[][] colConditions) {\n        Dictionary<int, HashSet<int>> rowMap
```C#\npublic class Solution {\n    public int[][] BuildMatrix(int k, int[][] rowConditions, int[][] colConditions) {\n        Dictionary<int, HashSet<int>> rowMap = new (), rowDepsMap = new (), colMap = new (), colDepsMap = new ();\n        for (int i = 0; i < k; i++)\n        {\n            rowMap[i] = new ();\n            rowDepsMap[i] = new ();\n            colMap[i] = new ();\n            colDepsMap[i] = new ();\n        }\n\n        FillData(rowConditions, rowMap, rowDepsMap);\n        FillData(colConditions, colMap, colDepsMap);\n\n        int[] rows = new int[k], cols = new int[k];\n        if (Iterate(rowMap, rowDepsMap, rows) == false || Iterate(colMap, colDepsMap, cols) == false) return new int[0][];\n\n        int[][] ans = new int[k][];\n        for (int i = 0; i < k; i++) ans[i] = new int[k];\n        for (int i = 0; i < k; i++) ans[rows[i]][cols[i]] = i + 1;\n\n        return ans;\n\n        void FillData(int[][] conditions, Dictionary<int, HashSet<int>> map, Dictionary<int, HashSet<int>> depsMap)\n        {\n            for (int i = 0; i < conditions.Length; i++)\n            {\n                map[conditions[i][0] - 1].Add(conditions[i][1] - 1);\n                depsMap[conditions[i][1] - 1].Add(conditions[i][0] - 1);\n            }\n        }\n\n        bool Iterate(Dictionary<int, HashSet<int>> map, Dictionary<int, HashSet<int>> depsMap, int[] res)\n        {\n            Queue<int> q = new ();\n            foreach (var (num, deps) in depsMap)\n            {\n                if (deps.Count == 0) q.Enqueue(num);\n            }\n\n            if (q.Count == 0) return false;\n\n            int index = 0;\n            while (q.Count > 0)\n            {\n                int current = q.Dequeue();\n                res[current] = index;\n\n                foreach (var next in map[current])\n                {\n                    depsMap[next].Remove(current);\n                    if (depsMap[next].Count == 0) q.Enqueue(next);\n                }\n\n                index++;\n            }\n\n            return index == k;\n        }\n    }\n}\n```
['Breadth-First Search', 'Graph']