Java Solution
Code\n\nclass Solution {\n    public int[][] buildMatrix(int k, int[][] rowConditions, int[][] colConditions) {\n        List<Integer>[] rowGraph = new ArrayLis
# Code\n```\nclass Solution {\n    public int[][] buildMatrix(int k, int[][] rowConditions, int[][] colConditions) {\n        List<Integer>[] rowGraph = new ArrayList[k + 1]; \n        for(int i = 1 ; i < rowGraph.length; i ++) {\n            rowGraph[i] = new ArrayList();\n        }\n        for(int [] rowCondition : rowConditions){ \n            rowGraph[rowCondition[0]].add(rowCondition[1]); \n        }\n\n        List<Integer>[] colGraph = new ArrayList[k + 1]; \n        for(int i = 1 ; i < colGraph.length; i ++) {\n            colGraph[i] = new ArrayList();\n        }\n        for(int [] colCondition : colConditions){\n            colGraph[colCondition[0]].add(colCondition[1]); \n        }\n\n        int[] visited = new int[k + 1];\n        Deque<Integer> queue = new LinkedList<>(); \n        for(int i = 1; i < rowGraph.length; i++){ \n            if(!topSort(rowGraph, i, visited, queue)){\n                return new int[0][0];\n            }\n        }\n\n        \n        int[] rowOrder = new int[k];\n        int[] rowIndexMap = new int[k + 1]; \n        for(int i = 0; i < k; i++){ \n            int node = queue.pollLast(); \n            rowOrder[i] = node; //\n            rowIndexMap[node] = i;\n        }\n\n        visited = new int[k + 1];\n        queue = new LinkedList();\n        for(int i = 1; i < colGraph.length; i++){\n            if(!topSort(colGraph, i, visited, queue)){\n                return new int[0][0];\n            }\n        }\n\n        int[] colOrder = new int[k];\n        int[] colIndexMap = new int[k+1];\n        for(int i = 0; i < k; i++){\n            int node = queue.pollLast();\n            colOrder[i] = node;\n            colIndexMap[node] = i;\n        }\n\n        int[][] result = new int[k][k];\n        \n        for(int i = 1; i <= k; i++){\n            result[rowIndexMap[i]][colIndexMap[i]] = i;\n        }\n\n        return result;\n\n    }\n\n    public boolean topSort(List<Integer>[] graph, int node, int[] visited, Deque<Integer> queue){\n        if(visited[node] == 2) {\n            return false;\n        }\n        if(visited[node] == 0){\n            visited[node] = 2;\n            for(int child : graph[node]){\n                if(!topSort(graph, child, visited, queue)){\n                    return false;\n                }\n            }\n            visited[node] = 1;\n            queue.add(node);\n        }\n        return true;\n    }\n}\n```
['Java']