// #pragma GCC target("avx") // #pragma GCC optimize("O3") // #pragma GCC optimize("unroll-loops") #include using namespace std; #define rep(i,n) for(int i = 0; i < (int)n; i++) #define FOR(n) for(int i = 0; i < (int)n; i++) #define repi(i,a,b) for(int i = (int)a; i < (int)b; i++) #define all(x) x.begin(),x.end() //#define mp make_pair #define vi vector #define vvi vector #define vvvi vector #define vvvvi vector #define pii pair #define vpii vector> template void chmax(T &a, const T &b) {a = (a > b? a : b);} template void chmin(T &a, const T &b) {a = (a < b? a : b);} using ll = long long; using ld = long double; using ull = unsigned long long; const ll INF = numeric_limits::max() / 2; const ld pi = 3.1415926535897932384626433832795028; const ll mod = 998244353; int dx[] = {1, 0, -1, 0, -1, -1, 1, 1}; int dy[] = {0, 1, 0, -1, -1, 1, -1, 1}; #define int long long void solve() { int n, m; cin >> n >> m; vvi a(n, vi(n)); rep(i, n) rep(j, n) cin >> a[i][j]; vi th(n, 0), tw(n, 0); rep(i, n) { rep(j, n) { th[i] += a[i][j]; tw[j] += a[i][j]; } } vi repre; repre.push_back(*min_element(all(th))); repre.push_back(*max_element(all(th))); repre.push_back(*min_element(all(tw))); repre.push_back(*max_element(all(tw))); if(*max_element(all(repre)) != *min_element(all(repre))) { cout << -1 << endl; return; } auto encode = [&](int x, int y) -> int { return x * n + y; }; auto decode = [&](int xy) -> pii { return make_pair(xy/n, xy%n); }; vi b(n*n); rep(i, n) rep(j, n) { b[encode(i, j)] = a[i][j]; } vector> g(n*n); rep(i, n-1) { rep(j, n) { rep(k, n) if(j != k && a[i+1][k] != 0) { g[encode(i, j)].insert(encode(i+1, k)); // cout << encode(i, j) << "->" << encode(i+1, k) << endl; } } } stack st; vector used(n, false); auto dfs = [&](int v, auto self) -> bool { st.push(v); vi ers; bool flag = false; for(auto e : g[v]) if(!used[e%n]) { used[e%n] = true; if(self(e, self)) { b[e]--; if(b[e] == 0) { ers.push_back(e); } flag = true; break; }else { ers.push_back(e); } used[e%n]; } for(auto e : ers) g[v].erase(e); if(decode(v).first == n-1 || flag) { return true; }else { st.pop(); return false; } }; vvi perm; rep(i, n) { while(b[i] > 0) { rep(j, n) used[j] = (j == i); bool f = dfs(i, dfs); if(!f) { cout << -1 << endl; return; } perm.push_back({}); while(!st.empty()) { // cout << decode(st.top()).first << " " << decode(st.top()).second << endl; perm.back().push_back(st.top()); st.pop(); } b[i]--; } } for(auto e : perm) { vi p(n); for(auto f : e) { pii now = decode(f); p[now.first] = now.second; } for(auto f : p) cout << f+1 << " "; cout << "\n"; } } signed main() { cin.tie(nullptr); ios::sync_with_stdio(false); solve(); return 0; }