from collections import deque class mf_graph: n=1 g=[[] for i in range(1)] pos=[] def __init__(self,N): self.n=N self.g=[[] for i in range(N)] self.pos=[] def add_edge(self,From,To,cap): assert 0<=From and From=0:continue level[to]=level[v]+1 if to==t:return level que.append(to) return level def dfs(v,up): if (v==s):return up res=0 level_v=level[v] for i in range(Iter[v],len(self.g[v])): to,rev,cap=self.g[v][i] if (level_v<=level[to] or self.g[to][rev][2]==0):continue d=dfs(to,min(up-res,self.g[to][rev][2])) if d<=0:continue self.g[v][i][2]+=d self.g[to][rev][2]-=d res+=d if res==up:return res level[v]=self.n return res flow=0 while(flow0): p=que.popleft() visited[p]=True for to,rev,cap in self.g[p]: if cap and not(visited[to]): visited[to]=True que.append(to) return visited n,m=map(int,input().split()) a=[] for i in range(n): a.append(list(map(int,input().split()))) for i in range(n): sm=0 for j in range(n): sm+=a[i][j] if sm!=m: exit(print(-1)) for j in range(n): sm=0 for i in range(n): sm+=a[i][j] if sm!=m: exit(print(-1)) for _ in range(m): g=mf_graph(2*n+2) S=2*n T=2*n+1 for i in range(n): g.add_edge(S,i,1) g.add_edge(i+n,T,1) for j in range(n): if a[i][j]>0: g.add_edge(i,n+j,1) g.flow(S,T,n) p=[-1]*n for e in g.edges(): frm=e[0] to=e[1]-n flow=e[3] if 0<=frm