This is the basic setup, note that instead of a grey scale image, the mask is coloured.
By changing the colour for Value_2 in the Color_Math node, you can recover each individual mask area, as required.
This gives a theoretical potential for up to 2^24 - 1 masks in a single image.