Section 23. Conditions or restrictions, unlimited as to time, by which the title or use of real property is affected, shall be limited to the term of thirty years after the date of the deed or other instrument or the date of the probate of the will creating them, except in cases of gifts or devises for public, charitable or religious purposes. This section shall not apply to conditions or restrictions existing on July sixteenth, eighteen hundred and eighty-seven, to those contained in a deed, grant or gift of the commonwealth, or to those having the benefit of section thirty-two.