This is called connected component analysis or labeling in the image processing literature. There are many algorithms that try to make this more efficient. You’re only counting regions, not assigning a unique label (i.e. room number) to each region, so you can get away with a somewhat simpler algorithm than I’m used to seeing.
You’ve already got comments on your coding style, I won’t go there again. But you also asked about a 3D extension, and I can give suggestions for that.
A straight-forward extension of your code to 3D would require to store the previous plane (2D data for the previous z coordinate) as well as the previous line. This gets complicated quickly, and will make it impossible to generate an algorithm that works with an arbitrary number of dimensions.
If you are indeed after an algorithm that would work with any number of dimensions, I’d suggest storing the whole floor plan and using a standard labeling algorithm. They’re only a little bit more complicated that what you’re goingdoing, but assign a label to each room element. You’d have a Union-Find data structure to keep track of labels. For each new element you look at all connected elements (the neighbors in the grid) that have already been processed (this is a fixed list of offsets). If any of them has a label, assign the same label to this element. If another one also has a label, set the equivalence between the two labels in your Union-Find data structure.
The very efficient implementations optimize the order in which neighbors are examined, noting that certain neighbors need not be examined at all under certain configurations.
To learn more about these algorithms, look at YACCLAB. It is explicitly 2D, but you can see all the different approaches people have taken here. I wrote a dimensionality-independent version, you can see the code here.