Detailed review of M's character in Wikipedia showed that Ian had different real life people in mind while portraying M. Wikipedia states:
Fleming based much of M's character on Rear Admiral John Godfrey, who was Fleming's superior at the Naval Intelligence Division during World War II. After Fleming's death, Godfrey complained "He turned me into that unsavoury character, M.
Despite that different people are named, each one of them being a male. In the films also every characters for M was filmed as male before Judi Dench came into the scene.
So what might be the reason producers suddenly changed their view for M as male and introduced a female as M?
EDIT : After @jampez77 comment I also found this wikipedia link on Judi Dench, where it is told that
the producers brought in Dame Judi Dench to take over as the new M. The character is based on Stella Rimington, the real-life head of MI5 between 1992 and 1996.
So my question is now why did the producers took the risk to change the gender of such an important character in the movie? As it is seen viewers do not like it when anything vital is changed in the movie of any famous novel.