($N,$K)=glob<>; @_=($N*$N,(0)x~-$K); / .* /,${$`}[$&]++or++${$`},$_[$'-1]+=$%=$N-${$`^v17},$_[0]-=$%for reverse<>; $,=$/;print@_