Maybe array out of dimension? My idea about that task was to go through every starting field and adding the first field height-next field height and then if there is field larger then the first field you break and mark variable as end=true and then you check if end is true and zb>apmax apmax=zb ...