detect if objects surround another gameobject

I want to create a script that can detect if a game object is completely surrounded on its x axis. In other words. If an object is completely surrounded by cube objects then object is in jail. But if there is a opening anywhere then they are not in jail.

From what I understand surrounded on its X-axis means that there’s only two objects to the left and right of the object in between. In that case use raycasts or linecasts in those directions. For complete surroundings use A* algorithm.