Not sure if this is your problem but this jumps out at me:
Code:
if(blocks[k] == 1)
Isn't blocks a two-dimensional array?