again major optimization, precomputation of squares in a specific
direction.