Hilchos Lashon Hara, chapter 1, section 7

If one is sitting with a group of people saying forbidden things and cannot leave their company, and when he sits and keeps silence he seems like a madman to them, nonetheless, it is certainly forbidden to speak forbidden things with them, and regarding this Chazal say (Eideyos 5:6), “Better for a person to be called a madman his whole life, that to be called a wicked person for one hour before the Omnipresent.” He should arouse himself at such a time with all his strength to control himself, and he should be confident that for this he will receive fitting reward from heaven, as reward is according to the trouble involved. The more trouble one has to observe a mitzvah, so one receives more reward.
To this applies the statement of Chazal, “Every moment that a person shuts his mouth, he merits the hidden light that no angel or creature can conceive of.”