I have a binary image that represents a number in MATLAB:

I’d like to fill all the digits. The desired result is:

The only thing I found was the imfill function, but that wasn’t really helpfull since I’ve lost my inner data (the 9’s inner circle for example).
The problem is how to distinguish the holes from the digits. A possible ad hoc solution is filtering them by the area of the pixels inside.
After step(1):
After step(2):