Need help in this code for Horses problem

https://www.codechef.com/viewsolution/55257476

It is showing wrong answer. I need help with any corrections.