C#
Code
# Code
```csharp []
public class Solution {
    public int[][] BuildMatrix(int k, int[][] rowConditions, int[][] colConditions)
 {
     // Store the topologically sorted sequences.
     List<int> orderRows = TopoSort(rowConditions, k);
     List<int> orderColumns = TopoSort(colConditions, k);

     // If no topological sort exists, return an empty array.
     if (orderRows.Count == 0 || orderColumns.Count == 0)
         return new int[0][];

     int[][] matrix = new int[k][];
     for (int i = 0; i < k; i++)
     {
         matrix[i] = new int[k];
         for (int j = 0; j < k; j++)
         {
             if (orderRows[i] == orderColumns[j])
                 matrix[i][j] = orderRows[i];
         }
     }
     return matrix;
 }

 private List<int> TopoSort(int[][] edges, int n)
 {
     // Build adjacency list
     List<List<int>> adj = new List<List<int>>();
     for (int i = 0; i <= n; i++)
         adj.Add(new List<int>());

     foreach (int[] edge in edges)
         adj[edge[0]].Add(edge[1]);

     List<int> order = new List<int>();
     // 0: not visited, 1: visiting, 2: visited
     int[] visited = new int[n + 1];
     bool[] hasCycle = { false };

     // Perform DFS for each node
     for (int i = 1; i <= n; i++)
     {
         if (visited[i] == 0)
         {
             Dfs(i, adj, visited, order, hasCycle);
             // Return empty if cycle detected
             if (hasCycle[0]) return new List<int>();
         }
     }

     // Reverse to get the correct order
     order.Reverse();
     return order;
 }

 private void Dfs(int node, List<List<int>> adj, int[] visited, List<int> order, bool[] hasCycle)
 {
     visited[node] = 1; // Mark node as visiting
     foreach (int neighbor in adj[node])
     {
         if (visited[neighbor] == 0)
         {
             Dfs(neighbor, adj, visited, order, hasCycle);
             // Early exit if a cycle is detected
             if (hasCycle[0]) return;
         }
         else if (visited[neighbor] == 1)
         {
             // Cycle detected
             hasCycle[0] = true;
             return;
         }
     }
     // Mark node as visited
     visited[node] = 2;
     // Add node to the order
     order.Add(node);
 }
}
```
['C#']