#include using namespace std; using ll = long long; using ld = long double; using ull = unsigned long long; #define rep(i,n) for(ll i=0;i T div_floor(T a, T b) { return a / b - ((a ^ b) < 0 && a % b); } template T div_ceil(T a, T b) { return a / b + ((a ^ b) > 0 && a % b); } template inline bool chmin(T &x, U y) { return (y < x) ? (x = y, true) : false; } template inline bool chmax(T &x, U y) { return (x < y) ? (x = y, true) : false; } template ostream &operator<<(ostream &os, const vector &a){ if (a.empty()) return os; os << a.front(); for (auto e : a | views::drop(1)){ os << ' ' << e; } return os; } void dump(auto ...vs){ ((cout << vs << ' '), ...) << endl; } vector> trans(vector> A,bool t){ if (!t){ return A; } ll H=A.size(); ll W=A[0].size(); vector> B(W,vector (H)); rep(i,H){ rep(j,W){ B[j][i]=A[i][j]; } } return B; } vector> solveC(ll H,ll W){ bool swapped=false; if (H>W){ swap(H,W); swapped=true; } if (H==2)return {}; if (H==3){ if (W%4==0){ vector> R(H,vector (W,0)); vector> base={{0,1,1,1},{0,1,0,1},{0,0,0,1}}; rep(bj,W/4){ rep(i,3){ rep(j,4){ R[i][bj*4+j]=base[i][j]+bj*2; } } } return trans(R,swapped); } else if (W%4==2){ vector> R(H,vector (W,0)); vector> base={{0,1,1,1},{0,1,0,1},{0,0,0,1}}; vector> basel={{0,1,1,1,1,2},{0,1,0,2,1,2},{0,0,0,2,2,2}}; rep(bj,(W-6)/4){ rep(i,3){ rep(j,4){ R[i][bj*4+j]=base[i][j]+bj*2; } } } rep(i,3){ rep(j,6){ R[i][W-6+j]=basel[i][j]+(W-6)/2; } } return trans(R,swapped); } else{ return {}; } } if (H==4){ vector> R(H,vector (W,0)); rep(j,W){ if (j==0){ continue; } R[1][j]=1; } R[2][W-1]=1; rep(j,W){ R[3][j]=1; } return trans(R,swapped); } if (H==5){ vector> R(H,vector (W,0)); vector line0={0,0,1,1,1}; vector line1={0,2,2,2,1}; vector line2={0,0,0,0,0}; vector linen={0,2,1,2,1}; rep(i,5){ R[i][0]=line0[i]; } rep(i,5){ R[i][W-2]=line1[i]; } rep(i,5){ R[i][W-1]=line2[i]; } for (ll j=1;j> R(H,vector (W,0)); vector> I=solveC(H-2,W-1); rep(i,H-2){ rep(j,W-1){ R[i+1][j+1]=I[i][j]+1; } } return trans(R,swapped); } vector> solveT(ll H,ll W){ bool swapped=false; if (H>W){ swap(H,W); swapped=true; } if (H==2)return {}; if (H==3){ if (W<=5)return {}; vector> R(H,vector (W,0)); vector> base={{0,1,1,1},{0,0,1,2},{0,2,2,2}}; rep(i,3){ rep(j,4){ R[i][j]=base[i][j]; } } for (ll j=4;j> R(H,vector (W,0)); vector> basel={{0,2},{0,0},{0,1},{1,1}}; vector> baser={{2,2},{2,3},{3,3},{1,3}}; rep(i,4){ rep(j,2){ R[i][j]=basel[i][j]; } } rep(i,4){ rep(j,2){ R[i][W-2+j]=baser[i][j]; } } for (ll j=2;j> R(H,vector (W,0)); vector> basel={{0,0},{2,0},{2,2},{2,1},{1,1}}; vector> baser={{0,3},{3,3},{4,3},{4,4},{1,1}}; rep(i,5){ rep(j,2){ R[i][j]=basel[i][j]; } } rep(i,5){ rep(j,2){ R[i][W-2+j]=baser[i][j]; } } for (ll j=2;j> R(H,vector (W,0)); vector> base={{0,0},{1,0},{1,1}}; rep(i,3){ rep(j,2){ R[i][j]=base[i][j]; } } for (ll i=3;i> I=solveT(H-2,W-2); rep(i,H-2){ rep(j,W-2){ R[i+1][j+2]=I[i][j]+3; } } return trans(R,swapped); } vector> solveCT(ll H,ll W){ bool swapped=false; if (H>W){ swap(H,W); swapped=true; } if (H==2)return {}; if (H==3){ vector> R(H,vector (W,0)); for (ll j=1;j> R(H,vector (W,0)); vector> baser={{0,2},{2,2},{1,2},{1,1}}; R[3][0]=1; rep(i,4){ rep(j,2){ R[i][W-2+j]=baser[i][j]; } } for (ll j=1;j> R(H,vector (W,0)); vector> I=solveCT(H-2,W-1); rep(i,H-2){ rep(j,W-1){ R[i+1][j+1]=I[i][j]+1; } } return trans(R,swapped); } void output(vector> A){ ll H=A.size(); if (H==0){ cout<<-1<<'\n'; return; } ll W=A[0].size(); ll m=0; rep(i,H){ rep(j,W){ A[i][j]++; chmax(m,A[i][j]); } } cout<>H>>W; vector> res; res=solveC(H,W); output(res); res=solveT(H,W); output(res); res=solveCT(H,W); output(res); return; } int main() { cin.tie(0)->sync_with_stdio(0); ll T=1; while (T--){ solve(); } return 0; }