Implementation
def spiralOrder(a)
n=a.length
m=a[0].length
t=0
b=m-1
r=n-1
l=0
dir=0
while t<=b && l<=r
if dir==0
for i in l..r
print "#{a[t][i]} "
end
t+=1
dir=1
elsif dir==1
for i in t..b
print "#{a[i][r]} "
end
r-=1
dir=2
elsif dir==2
for i in r.downto(l)
print "#{a[b][i]} "
end
b-=1
dir=3
else
for i in b.downto(t)
print "#{a[i][l]} "
end
l+=1
dir=0
end
end
end