class Solution { public: int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0}, n, m; void dfs(int row, int col, vector> &board) { board[row][col] = 'o'; for (int i = 0; i < 4; ++i) if (row + dx[i] >= 0 && row + dx[i] < n && col + dy[i] >= 0 && col + dy[i] < m && board[row + dx[i]][col + dy[i]] == 'O') dfs(row + dx[i], col + dy[i], board); } void solve(vector> &board) { n = board.size(), m = n ? board[0].size() : 0; if (!(m & n)) return; for (int i = 0; i < n; ++i) { if (board[i][0] == 'O') dfs(i, 0, board); if (board[i][m - 1] == 'O') dfs(i, m - 1, board); } for (int j = 0; j < m; ++j) { if (board[0][j] == 'O') dfs(0, j, board); if (board[n - 1][j] == 'O') dfs(n - 1, j, board); } for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) if (board[i][j] == 'O') board[i][j] = 'X'; else if (board[i][j] == 'o') board[i][j] = 'O'; } };