import sys input = sys.stdin.readline from collections import defaultdict,deque N,M=map(int,input().split()) A=[list(map(int,input().split())) for i in range(N)] ALIST=[] for tests in range(M): # dinic法 V=N*2+2 EDGE=[defaultdict(int) for i in range(V)] for i in range(N): for j in range(N): if A[i][j]>0: EDGE[i][N+j]=1 start=N*2 goal=N*2+1 for i in range(N): EDGE[start][i]=1 for i in range(N): EDGE[N+i][goal]=1 #print(EDGE) ANS=0 while True: # まずBFSする DIS=[-1]*V Q=deque([start]) DIS[start]=0 EDGE2=[[] for i in range(V)] while Q: x=Q.popleft() for to in EDGE[x]: if EDGE[x][to]==0: continue if DIS[to]==-1: DIS[to]=DIS[x]+1 Q.append(to) EDGE2[x].append(to) elif DIS[to]==DIS[x]+1: EDGE2[x].append(to) if DIS[goal]==-1: break # BFSしたときのEDGEを使ってDFSする MINCOST=[float("inf")]*V NOW=start ROUTE=[-1]*V while NOW!=-1: # DFS cost=MINCOST[NOW] if NOW==goal: ANS+=cost i=goal while i!=start: # goalからたどり,Routeを使ってEDGEの更新 j=ROUTE[i] if EDGE[j][i]==cost: NOW=j EDGE2[j].pop() EDGE[j][i]-=cost # 使ったルートをいけなく更新 MINCOST[j]-=cost EDGE[i][j]+=cost # 逆向きに進めるようにする. i=j continue if EDGE2[NOW]: to=EDGE2[NOW][-1] ROUTE[to]=NOW MINCOST[to]=min(cost,EDGE[NOW][to]) NOW=to else: if NOW==start: break EDGE2[ROUTE[NOW]].pop() NOW=ROUTE[NOW] #print(ANS) ANS=[-1]*N for i in range(N): for j in range(N): if EDGE[i+N][j]>0: ANS[j]=i+1 A[j][i]-=1 break if -1 in ANS: print(-1) exit() ALIST.append(ANS) for i in range(N): for j in range(N): if A[i][j]>0: print(-1) exit() for ans in ALIST: print(*ans)