Surrounded Regions
You are given an m × n board containing:
'X' → Land / blocked cell
'O' → Region cell
Capture all regions of 'O' that are completely surrounded by 'X'.
An 'O' is considered surrounded if it is not connected to the boundary of the board.
You must modify the board in-place.
Important Idea
Any 'O' connected to an edge cannot be captured.
Example:
X X X X
X O O X
X X O X
X O X X
The top O region is surrounded:
X X X X
X O O X
X X O X
X O X X
So it becomes X.
But the bottom O touches the boundary, so it remains O.
Result:
X X X X
X X X X
X X X X
X O X X
Example 1
Input
board = 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
Explanation
The O cells in the middle are completely surrounded by X.
X X X X
X O O X
X X O X
They do not touch the boundary, so they are captured and changed to X.
The bottom O touches the boundary:
X O X X
↑
boundary
Therefore, it cannot be captured and remains O.
Example 2
Input
board = X X X X O X X X X
Output
X X X X X X X X X
Explanation
The only O is completely surrounded by X:
X
X O X
X
Therefore, it is captured and changed to X.
Constraints
Hints:
Hint 1
Don't start by searching for surrounded regions.
Instead, think about the O's that cannot be surrounded.
Hint 2
Any O connected to a boundary O can never be captured.
Author & Technical Reviewer
Technically reviewed by: ExamAdda Technical Review Team
Technical Reviewers, ExamAdda
Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.
Published
Aug 15, 2026
Last updated
Aug 16, 2026
Content Verification Methodology
Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.
Expected Output