How to write constraint for generating a continuous mask

In reply to dave_59:

Thank you Dave. A tricky solution.