I was solving this problem from SPOJ : https://www.spoj.com/problems/HAYBALE/

I was able to solve this problem with arrays but there exists a solution with bitmasks.

My solution (Python): https://ideone.com/0exgTg

Can someone please tell me how can I solve this problem with the help of bitmasks.