LeetCode 130 Surrounded Regions - Solution

The core idea

Instead of finding surrounded regions, find unsurrounded regions and protect them.

DFS/BFS from all 'O's on the border. Mark these as "safe" (temporarily change to 'S' or another marker).

After marking, all remaining 'O's are surrounded. Flip them to 'X'.

Finally, change 'S' back to 'O'.