Problem
You get a board board with m rows and n columns. Each cell holds the letter "X" or the letter "O".
A region is a group of "O" cells that connect horizontally or vertically. A region is surrounded when none of its cells is on the edge of the board.
Change every "O" in a surrounded region to "X". Do not change the other cells. Change board in place and do not return a value.
Example 1
- Input:
- board = [["X","X","X","X","X"],["X","O","O","X","X"],["X","X","O","X","O"],["X","O","X","X","O"],["X","X","X","O","X"]]
- Output:
- [["X","X","X","X","X"],["X","X","X","X","X"],["X","X","X","X","O"],["X","X","X","X","O"],["X","X","X","O","X"]]
- Explanation:
- The regions at the right edge and at the bottom edge do not change. The other two regions are surrounded, so their cells change to
"X".
Example 2
- Input:
- board = [["X","X","X","X"],["X","O","X","X"],["X","O","O","X"],["X","X","O","X"]]
- Output:
- [["X","X","X","X"],["X","O","X","X"],["X","O","O","X"],["X","X","O","X"]]
- Explanation:
- All the
"O"cells make one region. One of its cells is on the bottom edge, so the region is not surrounded.
Constraints
- m == board.length
- n == board[i].length
- 1 <= m, n <= 200
- board[i][j] is "X" or "O"
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
Sources · 1 reference
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 1 record
No company association
Leetcode 130. Surrounded Regions
Reported date not specified · 0 source reports
Source snapshot: Nov 12, 2026
View source