http://www.codechef.com/viewsolution/4879394
You need to check vertically as well since the beam enters vertically. Good Luck!!