#include<bits/stdc++.h> #define fst first #define snd second #define re register
usingnamespace std;
typedef pair<int,int> pii; constint N = 1e5 + 10; int n,m; int cnt[N]; pii del[N];
structanswer{ int a,b,pos; };
inlineintread(){ int r = 0,w = 1; char c = getchar(); while (c < '0' || c > '9'){ if (c == '-') w = -1; c = getchar(); } while (c >= '0' && c <= '9'){ r = (r << 3) + (r << 1) + (c ^ 48); c = getchar(); } return r * w; }
inlinevoidsolve(){ int num = 0; n = read(); m = read(); bool vis[n + 10][m + 10]; vector<answer> ans; for (re int i = 1;i <= n;i++){ cnt[i] = 0; for (re int j = 1;j <= m;j++){ int x; x = read(); if (x) vis[i][j] = true; else vis[i][j] = false; num += x; cnt[i] += x; } } if (num % n) returnputs("-1"),void(); num /= n; for (re int i = 1;i <= n;i++) del[i] = {cnt[i] - num,i}; sort(del + 1,del + n + 1); for (re int i = 1,j = n;i < j;){ int p = del[i].snd,q = del[j].snd; for (re int k = 1;k <= m && del[i].fst && del[j].fst;k++){ if (!vis[p][k] && vis[q][k]){ del[i].fst++; del[j].fst--; vis[p][k] = true; vis[q][k] = false; ans.push_back({p,q,k}); } } if (!del[i].fst) i++; if (!del[j].fst) j--; } printf("%d\n",ans.size()); for (auto p:ans) printf("%d %d %d\n",p.a,p.b,p.pos); }
intmain(){ int T; T = read(); while (T--) solve(); return0; }