my solution is based on DFS technique

```
public class Solution {
int n,m;
char [][] p;
public void solve(char[][] board) {
n = board.length;
if(n==0)return;
m = board[0].length;
if(m==0)return;
p = board;
for(int i = 1 ; i < n-1 ; i++)
for(int j = 1 ; j < m-1 ; j++)
if(p[i][j]=='O')
backTrack(i , j);
}
public boolean backTrack( int i , int j){
if(!valid(i,j))return false;
if(p[i][j]=='X'||p[i][j]=='.')return true;
if(!valid(i+1,j)||!valid(i,j+1)||!valid(i-1,j)||!valid(i,j-1))return false;
p[i][j] = '.';
if(backTrack(i+1,j)&&backTrack(i,j+1)&&backTrack(i-1,j)&&backTrack(i,j-1))
{
p[i][j] = 'X';
return true;
}else{
p[i][j]='O';
return false;
}
}
public boolean valid(int i , int j){
return i>=0&&j>=0&&i<n&&j<m;
}
}
```