 All Problems
Surrounded Regions
medium
graph
depth-first search
breadth-first search
union find
matrix
amazon
google
microsoft

Given an m x n matrix board containing 'X' and 'O', capture all regions that are 4-directionally surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region.

An 'O' region is NOT captured if it is connected to an 'O' on the border.

Example:

Input:
4 4
X X X X
X O O X
X X O X
X O X X
Output:
X X X X
X X X X
X X X X
X O X X

Input format: First line: m n. Next m lines: board rows (space-separated chars).

Output format: Modified board.

Run to check your code against the sample cases, or submit to run every case