N = 4
M = 5
l = []
for i in xrange(1, 1 + N*M):
l.append(i)
m = [[None]*M for i in xrange(N)]
step = [(0, 1), (1, 0), (0, -1), (-1, 0)]
d = 0
s = (0, 0)
for num in l:
m[s[0]][s[1]] = num
ns = (step[d][0] + s[0], step[d][1] + s[1])
if ns[0] >= N or ns[1] >= M or m[ns[0]][ns[1]] is not None:
d = (d + 1) % 4
ns = (step[d][0] + s[0], step[d][1] + s[1])
s = ns
for i in m:
print i