2
boolean isClosed = true;4
public void solve(char[][] board) {6
int n = board[0].length;8
// To identify all those O which are adjacent and unbounded by 'X', we put a temporary value9
for (int i = 0; i < m; i++) {10
for (int j = 0; j < n; j++) {11
if (board[i][j] == 'O' && (i == 0 || j == 0 || i == m - 1 || j == n - 1)) {17
// revert the temperoray value and also replace remaining O with X18
for (int i = 0; i < m; i++) {19
for (int j = 0; j < n; j++) {20
if (board[i][j] == 'T') board[i][j] = 'O';21
else if (board[i][j] == 'O') board[i][j] = 'X';26
public void dfs(char[][] board, int i, int j) {27
if (i < 0 || j < 0 || i >= board.length || j >= board[0].length || board[i][j] != 'O') return;29
board[i][j] = 'T'; // to put a temperory mark/ to mark as visited31
dfs(board, i, j + 1); // Top32
dfs(board, i, j - 1); // Bottom33
dfs(board, i + 1, j); // Right34
dfs(board, i - 1, j); // Left