Hello Reyner:
A few important things:
1. You don't have to check for input correctness. If the text says 3<=C,R<=1000, it will be.
2. You don't have to read all input and then write all output, that is, you can read test case 1, process it and write result... then read test case 2...
Good luck,
Yandry.