I recently posted a Python based solution to separate disjointed polysurfaces, which may help you wrap your head around the logic.
Comments are pretty robust so you can review. Before you start that be sure to review Dynamo’s (often overlooked) topology classes in the geometry library. Otherwise it’ll make zero sense.