unable to find the bug in the code for DRGHTS

i have been working on this problem(http://www.codechef.com/problems/DRGHTS) for about a week now, i thought a lot and tested this code many times with different inputs, and get correct result each time, but when i submit it, the result is WA,somebody plz hint where am i going wrong
heres my code with an example input : http://ideone.com/72P7ol.
thanx for ur time