Sponsored
Use these hints if you're stuck. Try solving on your own first.
Use a DFS to find every square in the component. Then for each square, color it if it has a neighbor that is outside the grid or a different color.
Solve with full IDE support and test cases