import java.util.*;
public class Solution {
public void solve(char[][] board) {
if(board==null || board.length<3 || board[0].length<3) return;
Queue<int[]> queue = new LinkedList<>();
boolean[][] isVisited = new boolean[board.length][board[0].length];
Stack<int[]> stack = new Stack<>();
for(int i=1;i<board.length-1;i++){
for(int j=1;j<board[0].length-1;j++){
boolean tag = false;
if(board[i][j]=='O' && !isVisited[i][j]){
queue.offer(new int[]{i,j});
stack.push(new int[]{i,j});
while(!queue.isEmpty()){
int[] temp = queue.poll();
int m=temp[0],n=temp[1];
if(m==0 || m==board.length-1 || n==0 || n==board[0].length-1){
tag = true;
}
if(m>0 && board[m-1][n]=='O' && !isVisited[m-1][n]){
queue.offer(new int[]{m-1,n});
stack.push(new int[]{m-1,n});
isVisited[m-1][n]=true;
}
if(n>0 && board[m][n-1]=='O' && !isVisited[m][n-1]){
queue.offer(new int[]{m,n-1});
stack.push(new int[]{m,n-1});
isVisited[m][n-1]=true;
}
if(m<board.length-1 && board[m+1][n]=='O' && !isVisited[m+1][n]){
queue.offer(new int[]{m+1,n});
stack.push(new int[]{m+1,n});
isVisited[m+1][n]=true;
}
if(n<board[0].length-1 && board[m][n+1]=='O' && !isVisited[m][n+1]){
queue.offer(new int[]{m,n+1});
stack.push(new int[]{m,n+1});
isVisited[m][n+1]=true;
}
}
}
if(tag){
stack.clear();
}
while(!stack.isEmpty()){
int[] point = stack.pop();
int r=point[0],c=point[1];
board[r][c] = 'X';
}
}
}
}
}