SOURCE

function solve(board) {
    let row = board.length;
    let column = board[0].length;
    for(let i = 0; i < column; i++) {
        if(board[0][i] == 'O') {
            board[0][i] = 'A';
            search(0, i, board, row, column);
        }
        if(board[row - 1][i] == 'O') {
            board[row - 1][i] = 'A';
            search(0, row - 1, board, row, column);
        }
    }
    console.log(arr)
    for(let i = 1; i < row - 1; i++) {
        if(board[i][0] == 'O') {
            board[i][0] = 'A';
            search(i, 0, board, row, column);
        }
        if(board[i][column - 1] == 'O') {
            board[i][column - 1] = 'A';
            search(i, column - 1, board, row, column);
        }
    }
    console.log(arr)
    for(let i = 0; i < row; i++) {
        for(let j = 0; j < column; j++) {
            if(board[i][j] == 'A') {
                board[i][j] = 'O';
            }else {
                board[i][j] = 'X';
            }
        }
    }
};

function search(i, j, board, row, column) {
    if(i - 1 > 0 && board[i - 1][j] == 'O') {
        board[i - 1][j] = 'A';
        search(i - 1, j, board, row, column);
    }
    if(i + 1 < column && board[i + 1][j] == 'O') {
        board[i + 1][j] = 'A';
        search(i + 1, j, board, row, column);
    }
    if(j - 1 > 0 && board[i][j - 1] == 'O') {
        board[i][j - 1] = 'A';
        search(i, j - 1, board, row, column);
    }
    if(j + 1 < row && board[i][j + 1] == 'O') {
        board[i][j + 1] = 'A';
        search(i, j + 1, board, row, column);
    }
}

let answer = [["X","O","X","O","X","O","O","O","X","O"],["X","O","O","X","X","X","O","O","O","X"],["O","O","O","O","O","O","O","O","X","X"],["O","O","O","O","O","O","X","O","O","X"],["O","O","X","X","O","X","X","O","O","O"],["X","O","O","X","X","X","X","X","X","O"],["X","O","X","X","X","X","X","O","X","O"],["X","X","O","X","X","X","X","O","O","X"],["O","O","O","O","X","X","X","O","X","O"],["X","X","O","X","X","X","X","O","O","O"]];
let arr = [["X","O","X","O","X","O","O","O","X","O"],["X","O","O","X","X","X","O","O","O","X"],["O","O","O","O","O","O","O","O","X","X"],["O","O","O","O","O","O","X","O","O","X"],["O","O","X","X","O","X","X","O","O","O"],["X","O","O","X","X","X","O","X","X","O"],["X","O","X","O","O","X","X","O","X","O"],["X","X","O","X","X","O","X","O","O","X"],["O","O","O","O","X","O","X","O","X","O"],["X","X","O","X","X","X","X","O","O","O"]];
solve(arr)
console.log(arr)
console.log(arr == answer)
console 命令行工具 X clear

                    
>
console