Your code is very difficult to read without nicely indented formatting. Please use code tags (use the "Code" button in the editor) in the future.
Your for loops have a stray semicolon after the parens that are causing behavior you do not expect (See the arrow below where I point out where the semicolon is in your for loops. Because of the semicolon, the code in the curly braces is not being executed as part of the for loop, instead it would be executed after the for loop.
for ( ... ) ;
<--
Edit: Ack! I must have gone for a
coffee break while I posted this, Amit posted an answer before mine posted.