You are given an m x n matrix board containing letters β€˜X’ and β€˜O’, capture regions that are surrounded:

  • Connect: A cell is connected to adjacent cells horizontally or vertically.
  • Region: To form a region connect every β€˜O’ cell.
  • Surround: The region is surrounded with β€˜X’ cells if you can connect the region with β€˜X’ cells and none of the region cells are on the edge of the board.

A surrounded region is captured by replacing all β€˜O’s with β€˜X’s in the input matrix board.

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:

xogrid

In the above diagram, the bottom region is not captured because it is on the edge of the board and cannot be surrounded.

Example 2:

  • Input: board = [[β€œX”]]
  • Output: [[β€œX”]]

Constraints:

  • m == board.length
  • n == board[i].length
  • 1 <= m, n <= 200
  • board[i][j] is β€˜X’ or β€˜O’.