Prior to the passage of the twenty-second amendment, Grover Cleveland served non-consecutive terms (and Franklin Delano Roosevelt served four terms). So clearly it was legal then.
The active portion of the amendment's text is:
Section 1. No person shall be elected to the office of the President more than twice, and no person who has held the office of President, or acted as President, for more than two years of a term to which some other person was elected President shall be elected to the office of the President more than once.
Section 2 is just about when the amendment will be considered ratified. That was satisfied, so we don't need to care about that or the unquoted portion of Section 1 (the one person covered under that is now dead).
So no person shall be elected more than twice. Nor may a person who was raised to the office from the vice-presidency for at least two years be elected more than once. The amendment says nothing about consecutive terms. It applies whether the person wants a third consecutive term or a non-consecutive term.
There is some dispute about whether someone could be appointed vice president after serving more than six years as president, as that wouldn't be elected. If so, presumably that person could serve almost four years. But there's an argument that that should not be allowed. That argument says that someone who has been president is no longer eligible or qualified to be president. Which makes sense but is not how the amendments literally read.
The basic problem there is that the twenty-second amendment talks only about election and doesn't prohibit people in the line of succession from taking office. Meanwhile, the twenty-fifth adds a new method of appointment for vice presidents without clarifying whether such appointees are limited by the twenty-second amendment.
The dispute is basically over whether they should read the provisions literally, in which case this is a loophole. Or should they naturally extend the various provisions to come up with a reasonable system, in which case they'd make some new rule, like no one who has already served six years or more as president is eligible to become president again. In which case this would be a presidential qualification and no one could be appointed or elected vice president who didn't meet it.