Wrong answer for FENCE problem (LRNDSA03)

Can anyone tell me why my program is giving wrong answers for some of the test sets?
My Solution