This question already has an answer here:
I am a non-EU passport and I do not need a visa for either Schengen countries or San Marino. I want to travel and stay in San Marino for a few days, and I would like these days to not count towards the limit of 90 days in Schengen area (because well, San Marino is not a part of Schengen area). How can I register the fact that I have exited the Schengen area with the authorities (i.e. get an exist stamp in passport)?
Related issue: how do citizens and permanent residents of San Marino prove they have not overstayed in Schengen area (it's interesting because the only way to travel to San Marino is through a Schengen country)