r,c=map(int,input().split()) a=sorted(sum([input().split()for _ in [0]*r],[])) for i in range(r):print(*a[i*c:-~i*c])