/* -*- coding: utf-8 -*- * * 2263.cc: No.2263 Perms - yukicoder */ #include #include #include using namespace std; /* constant */ const int MAX_N = 50; const int MAX_M = 50; /* typedef */ using vi = vector; using vb = vector; /* global variables */ int as[MAX_N][MAX_N], ps[MAX_M][MAX_N]; vi nbrs[MAX_N * 2]; /* subroutines */ bool max_match_rec(const vi *nbrs, int u, vi &matches, vb &visited) { if (u < 0) return true; for (const int v: nbrs[u]) { if (! visited[v]) { visited[v] = true; if (max_match_rec(nbrs, matches[v], matches, visited)) { matches[u] = v; matches[v] = u; return true; } } } return false; } int max_match(const int n, int l, const vi *nbrs, vi &matches) { matches.assign(n, -1); int count = 0; for (int u = 0; u < l; u++) { vb visited(n, false); if (max_match_rec(nbrs, u, matches, visited)) count++; } return count; } /* main */ int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) scanf("%d", as[i] + j); bool ok = true; for (int i = 0; ok && i < n; i++) { int s = 0; for (int j = 0; j < n; j++) s += as[i][j]; ok = (s == m); } for (int j = 0; ok && j < n; j++) { int s = 0; for (int i = 0; i < n; i++) s += as[i][j]; ok = (s == m); } if (! ok) { puts("-1"); return 0; } int n2 = n * 2; for (int i = 0; i < m; i++) { for (int u = 0; u < n2; u++) nbrs[u].clear(); for (int u = 0; u < n; u++) for (int v = 0; v < n; v++) if (as[u][v] > 0) { nbrs[u].push_back(n + v); nbrs[n + v].push_back(u); } vi matches; int cnt = max_match(n2, n, nbrs, matches); if (cnt < n) { puts("-1"); return 0; } for (int j = 0; j < n; j++) { ps[i][j] = matches[j] - n; as[j][ps[i][j]]--; } } for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) printf("%d%c", ps[i][j] + 1, (j + 1 < n) ? ' ' : '\n'); return 0; }