<p>There is a strange printer with the following two special requirements:</p>

<ul>
	<li>On each turn, the printer will print a solid rectangular pattern of a single color on the grid. This will cover up the existing colors in the rectangle.</li>
	<li>Once the printer has used a color for the above operation, <strong>the same color cannot be used again</strong>.</li>
</ul>

<p>You are given a <code>m x n</code> matrix <code>targetGrid</code>, where <code>targetGrid[row][col]</code> is the color in the position <code>(row, col)</code> of the grid.</p>

<p>Return <code>true</code><em> if it is possible to print the matrix </em><code>targetGrid</code><em>,</em><em> otherwise, return </em><code>false</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/23/print1.jpg" style="width: 600px; height: 175px;" />
<pre>
<strong>Input:</strong> targetGrid = [[1,1,1,1],[1,2,2,1],[1,2,2,1],[1,1,1,1]]
<strong>Output:</strong> true
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/23/print2.jpg" style="width: 600px; height: 367px;" />
<pre>
<strong>Input:</strong> targetGrid = [[1,1,1,1],[1,1,3,3],[1,1,3,4],[5,5,1,4]]
<strong>Output:</strong> true
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> targetGrid = [[1,2,1],[2,1,2],[1,2,1]]
<strong>Output:</strong> false
<strong>Explanation:</strong> It is impossible to form targetGrid because it is not allowed to print the same color in different turns.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m == targetGrid.length</code></li>
	<li><code>n == targetGrid[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 60</code></li>
	<li><code>1 &lt;= targetGrid[row][col] &lt;= 60</code></li>
</ul>

from typing import List
from collections import defaultdict, deque

class Solution:
    def isPrintable(self, targetGrid: List[List[int]]) -> bool:
        m, n = len(targetGrid), len(targetGrid[0])
        
        # Step 1: Find the bounding box for each color
        color_bounds = {}
        for i in range(m):
            for j in range(n):
                color = targetGrid[i][j]
                if color not in color_bounds:
                    color_bounds[color] = [i, i, j, j]  # min_row, max_row, min_col, max_col
                else:
                    color_bounds[color][0] = min(color_bounds[color][0], i)
                    color_bounds[color][1] = max(color_bounds[color][1], i)
                    color_bounds[color][2] = min(color_bounds[color][2], j)
                    color_bounds[color][3] = max(color_bounds[color][3], j)
        
        # Step 2: Build the dependency graph
        graph = defaultdict(set)
        indegree = defaultdict(int)
        
        for color, (min_row, max_row, min_col, max_col) in color_bounds.items():
            for i in range(min_row, max_row + 1):
                for j in range(min_col, max_col + 1):
                    if targetGrid[i][j] != color:
                        if targetGrid[i][j] not in graph[color]:
                            graph[color].add(targetGrid[i][j])
                            indegree[targetGrid[i][j]] += 1
        
        # Step 3: Topological sort to check if we can print all colors
        zero_indegree = deque([color for color in color_bounds if indegree[color] == 0])
        printed_colors = 0
        
        while zero_indegree:
            color = zero_indegree.popleft()
            printed_colors += 1
            for neighbor in graph[color]:
                indegree[neighbor] -= 1
                if indegree[neighbor] == 0:
                    zero_indegree.append(neighbor)
        
        # If we have printed all colors, return True
        return printed_colors == len(color_bounds)
16800