#include #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--) #define all(x) (x).begin(), (x).end() #define sz(x) int(x.size()) using namespace std; using ll = long long; constexpr int INF = 1e9; constexpr ll LINF = 1e18; string YesNo(bool cond) { return cond ? "Yes" : "No"; } string YESNO(bool cond) { return cond ? "YES" : "NO"; } template bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } return false; } template bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } return false; } template T bisect(T ok, T ng, const F& f) { while (abs(ok - ng) > 1) { T mid = min(ok, ng) + (abs(ok - ng) >> 1); (f(mid) ? ok : ng) = mid; } return ok; } template T bisect_double(T ok, T ng, const F& f, int iter = 100) { while (iter--) { T mid = (ok + ng) / 2; (f(mid) ? ok : ng) = mid; } return ok; } template vector make_vec(size_t a) { return vector(a); } template auto make_vec(size_t a, Ts... ts) { return vector(ts...))>(a, make_vec(ts...)); } template istream& operator>>(istream& is, vector& v) { for (int i = 0; i < int(v.size()); i++) { is >> v[i]; } return is; } template ostream& operator<<(ostream& os, const vector& v) { for (int i = 0; i < int(v.size()); i++) { os << v[i]; if (i < sz(v) - 1) os << ' '; } return os; } int main() { int ti = clock(); int n, m; cin >> n >> m; vector a(n, vector(n)); cin >> a; vector> b(n); bool ok = true; rep(i, n) { int x = 0, y = 0; rep(j, n) { x += a[i][j], y += a[j][i]; rep(_, a[i][j]) b[i].push_back(j); } ok &= x == m; ok &= y == m; } if (!ok) { cout << -1 << '\n'; return 0; } auto f = [&]() -> vector> { vector ans(m, vector(n, -1)); mt19937_64 rng(random_device{}()); rep(j, n) { shuffle(all(b[j]), rng); for (int x : b[j]) { ok = false; rep(i, m) if (ans[i][j] == -1 && !count(all(ans[i]), x)) { ans[i][j] = x; ok = true; break; } if (!ok) { return {}; } } } return ans; }; while (clock() - ti < 1.5 * CLOCKS_PER_SEC / 100) { vector ans = f(); if (!ans.empty()) { rep(i, m) rep(j, n) ans[i][j]++; rep(i, m) cout << ans[i] << '\n'; return 0; } } cout << -1 << '\n'; }