Thanks for the quick reply leppy.
In my algorithm, i check whether the adjacent element in a row or column contain a one(i check for a 1 to the left,right and directly below the current element), if they contain then only i push them into the queue. Also, afterwards i change the element's value to 2. So how can i possibly push the same item into the queue more than once?
I didn't get this part. Could you please explain.