For each 'O', BFS/DFS to check if it reaches the border. If not, it's surrounded.
This checks many 'O's repeatedly, especially if they're part of the same region.
Can you flip the approach?
The trap
For each 'O', BFS/DFS to check if it reaches the border. If not, it's surrounded.
This checks many 'O's repeatedly, especially if they're part of the same region.
Can you flip the approach?