fix edge case in quary

fix an edge case where at the end of a row it can get itself into an
inconsistent state if it is unable to turn