h,w=map(int,input().split()) a=[] for i in range(h):a+=[int(i)for i in list(input().split())] a.sort() l=[[a[w*j+i] for i in range(w)]for j in range(h)] for i in l: print(*i)