import java.util.*;

public class Solution {
    
      public void solve(char[][] board) {
        if(board.length==0||board.length==1){
            return;
        }
        TreeSet<String> allBoundaryO = findAllBoundaryO(board);
        int yLimt=board.length;
        int xLimt=board[1].length;
        for(int i=0;i<yLimt;i++){
            for(int j=0;j<xLimt;j++){
                if('O'==board[i][j]&&!allBoundaryO.contains(i+"-"+j)){
                board[i][j]='X';
            }
            }
        }
    }

    public TreeSet<String> findAllBoundaryO(char[][] board) {
        TreeSet<String> result =new TreeSet<>();
        int yLimt = board.length-1;
        int xLimt = board[1].length-1;
        for (int i = 0; i <= xLimt; i++) {
            if('O'==board[yLimt][i]){
                result.add(yLimt+"-"+i);
                getThisNeiO(board,yLimt,i,result);
            }
            if('O'==board[0][i]){
                result.add( 0+"-"+i);
                getThisNeiO(board,0,i,result);
            }
        }

        for (int i = 0; i <= yLimt; i++) {
            if('O'==board[i][xLimt]){
                result.add(i+"-"+xLimt);
                getThisNeiO(board,i,xLimt,result);
            }
            if('O'==board[i][0]){
                result.add(i+"-"+0);
                getThisNeiO(board,i,0,result);
            }
        }
        return result;
    }

    private void getThisNeiO(char[][] board, int x, int y, TreeSet<String> result) {

        if(judgeIsValid(board,x-1,y) && 'O'==board[x-1][y]) {
            if(!result.contains(x-1+"-"+y)){
                result.add(x-1+"-"+y);
                getThisNeiO(board,x-1,y,result);
            }
        }
        if(judgeIsValid(board,x+1,y) && 'O'==board[x+1][y]){
            if(!result.contains(x+1+"-"+y)){
                result.add(x+1+"-"+y);
                getThisNeiO(board,x+1,y,result);
            }
        }
        if(judgeIsValid(board,x,y-1) && 'O'==board[x][y-1]){
            if(!result.contains(x+"-"+(y-1))){
                result.add(x+"-"+ (y-1));
                getThisNeiO(board,x,y-1,result);
            }
        }
        if(judgeIsValid(board,x,y+1) && 'O'==board[x][y+1]){
            if(!result.contains(x+"-"+(y+1))){
                result.add(x+"-"+(y+1));
                getThisNeiO(board,x,y+1,result);
            }
        }
    }

    public boolean judgeIsValid(char[][] board, int x,int y){
        int yLimt=board.length-1;
        int xLimt=board[1].length-1;

        if( x>=0 && x<=yLimt && y>=0 &&y<=xLimt){
            return true;
        }
        return false;
    }
      
}