Monster pathfinding is based on movement cost. When a monster decides on a route to take, the pathfinding portion of the AI looks at the movement cost of everything between the monster and where it wants to go. Base terrain has the default cost, paths and roads lower movement cost, resources like crystal raise it, as does water (fire elementals refuse to go into it as it harms them) and tar. Forest and mountains have quite high costs, and walls can as well. Gates don't have as high a cost as walls, so will get targeted over walls more often.
A newer addition to this is that each mob that's planned a path through a tile, whether they're actually in it or not, slightly raises the movement cost of that tile, causing them to spread out. You may find yourself needing to go with 2 or even 3 thick walls in some places, depending on all the variables.
Water in the maze will keep fire elementals from going through, even if they have to dig through mountains. If using that, you need to leave them a path, or fully cut off all potential routes with water.