I am not quite sure about your approach..
.. but my method is a bit different..
-- first i flagged all the rectangles that are connected to the main one directly/indirectly using dfs()
-- then I pushed all the x-coordintes of all the flagged rectangles into an array and sorted them
-- then I did the same thing with the y-coordinates.
-- then I simply added the areas of the rectangles that fall within the consecutive x and y coordinates.
And you said you used floodfill to mark the areas.. how do you know the upper limit of the cooordinates..