All perfect praise be to Allaah, The Lord of the Worlds. I testify that there is none worthy of worship except Allaah, and that Muhammad is His Slave and Messenger.
The place reserved for the housing of the Imaam on the third floor is considered a residence and not a Masjid, and it does not fall under the ruling of the Masjid. Therefore, there is nothing wrong with his wife staying in it when she is menstruating as she is not a resident in the Masjid but, rather, in a residence above the Masjid; and it is permissible that the residence of the Imaam be above the Masjid.
The Fatwa of the Permanent Committee reads: “There is no objection to the Masjid being under the residence if the Masjid and the residence were originally built on this basis. The same applies if the Masjid is established later under the residence; however, if the residence was established on top of the Masjid [i.e. it did not originally exist and it was the Masjid which was built first] then this is not permissible because whatever is above the Masjid is part of it (i.e. it is a Waqf [an endowment]).”
Besides, the Fiqh Encyclopedia reads: “The Shaafi'i, Maaliki and Hanbali Schools of Fiqh permitted making the top of the house a Masjid while making the lower part a residence and vice versa, because they are separate properties and it is permissible to endow them, so it is permissible to endow one of them apart from the other.”
For more benefit, please refer to Fatwa 82652.
Allaah Knows best.