Whats wrong in the following code? CHFIMPRS

#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define forn(i,a,b) for(int i =a;i<b;i++)
#define fi first
#define se second
#define fast ios_base::sync_with_stdio(false);
using namespace std;


typedef long long int ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;


void solve(){
	int n,m;
	cin >> n >>m;
	int grid[n][m];
	map<int,int > count;
	forn(i,0,n){
		forn(j,0,m){
			cin >> grid[i][j];
			count[grid[i][j]]++;
		}
	}
	set<int> centre;
	set<int> anywhere;
	for(auto e:count){
		if(e.second >= 2)
			anywhere.insert(e.first);
		else{
			centre.insert(e.first);
			// anywhere.insert(e.first);
		}
	}
	// if(m%2==0){
	// 	if(centre.size()>0){
	// 		cout << "-1" <<  endl;return;
	// 	}
	// }

	int ans[n][m];
	memset(ans,0,sizeof(ans));

	forn(i,0,n){
		forn(j,0,m/2){ 
			if(anywhere.size()==0){
				// cout << i << endl;
				cout << -1 << endl;return;
			}
			int val = *anywhere.begin();
			ans[i][j] = *anywhere.begin();
			ans[i][m-1-j] = val;
			// cout << i << " " << j << " " << (m-1) << " " ;
			count[val]-=2;
			if(count[val]<2){
				anywhere.erase(val);
				if(count[val]==1)
					centre.insert(val);
			}
		}
		// cout << endl;

		if(m&1){
			if(centre.size()!=0){
				int valc = *centre.begin();
				ans[i][m/2] = valc;
				count[valc]--;
				if(count[valc]==0)
					centre.erase(valc);
			}
			else{
				if(anywhere.size()==0){
				// cout << i << endl;
// 
					cout << -1 << endl;return;
				}
				int valc = *anywhere.begin();
				ans[i][m/2] = valc;
				count[valc]--;
				if(count[valc]==1)
					{
						anywhere.erase(valc);
						centre.insert(valc);
					}
				if(count[valc]==0)
					anywhere.erase(valc);
			}
		}
	}
	forn(i,0,n){
		forn(j,0,m)
			cout << ans[i][j] << " ";
		cout << endl;
	}
}

int main(){
	fast;
	#ifndef ONLINE_JUDGE
		freopen("input.txt","r",stdin);
		freopen("output.txt","w",stdout);
	#endif

	int t;cin >> t;while(t--)
		solve();
}

was getting WA