Problem with MDOLLS (SPOJ)

Here is a link to my solution and i cant figure out the mistake.
https://www.codechef.com/viewsolution/30766786.

Question : https://www.spoj.com/problems/MDOLLS/
It is a SPOJ question. I have just uploaded the solution to codechef for reference.

Please help,
Thanks.