OK, great. I guess CommonLounge has evolved a lot from the last time I opened it.
If you want to use Java, be prepared for lots of optimisation, Java has a much bigger library than C++, but that also makes it take a bit more time and memory.
Optimisations you probably need for contests
I don't prefer pointers at all. Look up Row Major Order and Column Major Order possibly for address calculation.
I found yesterday that both my original programs gave the correct output at least on the given testcases; had I not tried to edit them to be able to compile on that machine, and fail at it to get a zero possibly by some CE or other error as a result. Will also post my code after 31st to help me figure out if I was wrong somewhere in my logic.