private void fill(char[][] board, int i, int j) {
        if (board[i][j] != 'O') return;
        board[i][j] = '#';
        Queue<Integer> q = new LinkedList<Integer>();
        q.add(encode(board, i, j));
        while (!q.isEmpty()) {
            int n = q.remove();
            int r = n / board[0].length, c = n % board[0].length;
            for (int k = 0; k < 4; ++k) {
                int x = r + dx[k], y = c + dy[k];
                if (x >= 0 && x < board.length && y >= 0 && y < board[0].length && board[x][y] == 'O') {
                    board[x][y] = '#';
                    q.add(encode(board, x, y));
                }
            }
        }
    }