s=gets p (x=""+s).gsub(/\w/){a=[i=j=$`.size] c=proc{$&==x[j,1]&&([j-1,j+1,j-9,j+9].-a<9;s[i]=32)}while x!=s&&$.-=1 puts s,"#{-$.} chains"