Surrounded regions

Flip every 'O' to 'X' unless it connects back to the border. Flood fill from the border first, so you never have to guess.

Time O(rows × cols)Space O(rows × cols)Code C#, Java, C++ and Python
Watch it animatedStep through it, with the running line highlighted. Free, no sign-in needed to watch.

The idea

Flipping "surrounded" O's directly is awkward, because an O only counts as surrounded once you know none of its region touches the border. Flip the problem: flood fill from every border O first and mark that whole region safe. Anything still plain 'O' afterwards never reached the border, so it is surrounded and becomes 'X'.

Code

public static void Solve(char[][] board) {
    int rows = board.Length, cols = board[0].Length;
    for (int r = 0; r < rows; r++)
        for (int c = 0; c < cols; c++)
            if ((r == 0 || c == 0 || r == rows - 1 || c == cols - 1) && board[r][c] == 'O')
                Mark(board, r, c);
    for (int r = 0; r < rows; r++)
        for (int c = 0; c < cols; c++)
            if (board[r][c] == 'O') board[r][c] = 'X'; // never reached the border: surrounded
            else if (board[r][c] == '#') board[r][c] = 'O'; // reached the border: safe, restore it
}

static void Mark(char[][] board, int r, int c) {
    if (r < 0 || c < 0 || r >= board.Length || c >= board[0].Length || board[r][c] != 'O') return;
    board[r][c] = '#'; // safe: temporarily mark it
    Mark(board, r + 1, c); Mark(board, r - 1, c);
    Mark(board, r, c + 1); Mark(board, r, c - 1);
}
static void solve(char[][] board) {
    int rows = board.length, cols = board[0].length;
    for (int r = 0; r < rows; r++)
        for (int c = 0; c < cols; c++)
            if ((r == 0 || c == 0 || r == rows - 1 || c == cols - 1) && board[r][c] == 'O')
                mark(board, r, c);
    for (int r = 0; r < rows; r++)
        for (int c = 0; c < cols; c++)
            if (board[r][c] == 'O') board[r][c] = 'X'; // never reached the border: surrounded
            else if (board[r][c] == '#') board[r][c] = 'O'; // reached the border: safe, restore it
}

static void mark(char[][] board, int r, int c) {
    if (r < 0 || c < 0 || r >= board.length || c >= board[0].length || board[r][c] != 'O') return;
    board[r][c] = '#'; // safe: temporarily mark it
    mark(board, r + 1, c); mark(board, r - 1, c);
    mark(board, r, c + 1); mark(board, r, c - 1);
}
void mark(vector<vector<char>>& board, int r, int c) {
    if (r < 0 || c < 0 || r >= (int)board.size() || c >= (int)board[0].size() || board[r][c] != 'O') return;
    board[r][c] = '#'; // safe: temporarily mark it
    mark(board, r + 1, c); mark(board, r - 1, c);
    mark(board, r, c + 1); mark(board, r, c - 1);
}

void solve(vector<vector<char>>& board) {
    int rows = board.size(), cols = board[0].size();
    for (int r = 0; r < rows; r++)
        for (int c = 0; c < cols; c++)
            if ((r == 0 || c == 0 || r == rows - 1 || c == cols - 1) && board[r][c] == 'O')
                mark(board, r, c);
    for (int r = 0; r < rows; r++)
        for (int c = 0; c < cols; c++)
            if (board[r][c] == 'O') board[r][c] = 'X'; // never reached the border: surrounded
            else if (board[r][c] == '#') board[r][c] = 'O'; // reached the border: safe, restore it
}
def solve(board: list[list[str]]) -> None:
    rows, cols = len(board), len(board[0])
    for r in range(rows):
        for c in range(cols):
            if (r == 0 or c == 0 or r == rows - 1 or c == cols - 1) and board[r][c] == 'O':
                mark(board, r, c)
    for r in range(rows):
        for c in range(cols):
            if board[r][c] == 'O': board[r][c] = 'X'  # never reached the border: surrounded
            elif board[r][c] == '#': board[r][c] = 'O'  # reached the border: safe, restore it

def mark(board: list[list[str]], r: int, c: int) -> None:
    if r < 0 or c < 0 or r >= len(board) or c >= len(board[0]) or board[r][c] != 'O': return
    board[r][c] = '#'  # safe: temporarily mark it
    mark(board, r + 1, c); mark(board, r - 1, c)
    mark(board, r, c + 1); mark(board, r, c - 1)

Tips

In the .NET library

This border-first flood fill is the standard trick whenever "reachable from the edge" matters: the same idea solves "number of enclaves" and parts of "pacific atlantic water flow".