added automatic zxy

Foreach is slow. In critical places, it's better to use a simple for loop.