こう?
^([^m]*($|m($|[^a]|a($|[^x]))))*$