We can perform DFS on the'O' on the boundary first, and modify all the'O's to other characters first. Here the'Y' character is taken.
After searching for the four edges, the remaining'O' is changed to an'X' character, and we then change the'Y' character to an'O' character to get the correct answer.