Ok, first you need to read the help section for $chan in the mirc help file. That should explain it decently. /help $chan (pick the second option in the box that is displayed.)
$chan(0) is the amount of channel you are in.
$chan(1) is the first channel you're in. $chan(2) is he second and so forth.
What I did was to make a while loop that gets the total amount of channels you're in (using $chan(0)) and then loops through each on and checks to see if there are less that three nicks in it.