#include using namespace std; using pii = pair; using ll = long long; #define rep(i, j) for(int i=0; i < (int)(j); i++) #define rrep(i, j) for(int i=(int)(j-1); i >=0; i--) #define repeat(i, j, k) for(int i = (j); i < (int)(k); i++) #define all(v) v.begin(),v.end() #define debug(x) cerr << #x << " : " << x << endl template bool set_min(T &a, const T &b) { return a > b ? a = b, true : false; } template bool set_max(T &a, const T &b) { return a < b ? a = b, true : false; } // vector template istream& operator >> (istream &is , vector &v) { for(T &a : v) is >> a; return is; } template ostream& operator << (ostream &os , const vector &v) { for(const T &t : v) os << "\t" << t; return os << endl; } // pair template ostream& operator << (ostream &os , const pair &v) { return os << "<" << v.first << ", " << v.second << ">"; } const int INF = 1 << 30; const ll INFL = 1LL << 60; class Solver { public: vector> odd_magic_square(int N) { assert(N % 2); vector> G(N, vector(N)); int nx = N / 2; int ny = 0; rep(i, N * N) { G[ny][nx] = i + 1; while(i != N * N - 1 and G[ny][nx]) { int nny = (ny + N - 1) % N; int nnx = (nx + 1) % N; if(G[nny][nnx]) ny = (ny + 1) % N; else nx = nnx, ny = nny; } } return G; } vector> four_times_magic_square(int N) { assert(N % 4 == 0); vector> G(N, vector(N)); vector used(N * N); rep(y, N) rep(x, N) { if(x % 4 == y % 4 or x % 4 == 3 - y % 4) { int n = y * N + x + 1; G[y][x] = n; used[n] = true; } } int n = 1; rrep(y, N) rrep(x, N) { if(not(x % 4 == y % 4 or x % 4 == 3 - y % 4)) { while(used[n]) n++; G[y][x] = n; used[n] = true; } } return G; } vector> four_times_plus_two_magic_square(int N) { assert(N % 4 == 2); int NN = N / 2; vector> G1 = odd_magic_square(NN); for(auto &l : G1) for(int &i : l) i = (i - 1) * 4; vector> G(N, vector(N)); const vector> L = { {4, 1}, {2, 3} }, U = { {1, 4}, {2, 3}, }, X = { {1, 4}, {3, 2} }; int center = NN / 2; for(int y = 0; y < N; y += 2) for(int x = 0; x < N; x += 2) { vector> A; int yy = y / 2, xx = x / 2; if(yy <= center) A = L; else if(yy == center + 1) A = U; else A = X; if(yy == center and xx == center) A = U; if(yy == center + 1 and xx == center) A = L; rep(dy, 2) rep(dx, 2) { G[y + dy][x + dx] = G1[yy][xx] + A[dy][dx]; } } return G; } bool solve() { int N; cin >> N; vector> ans; if(N % 2) { ans = odd_magic_square(N); } else if(N % 4 == 0) { ans = four_times_magic_square(N); } else if(N % 4 == 2) { ans = four_times_plus_two_magic_square(N); } else { assert(0); // 2 } rep(y, N) { rep(x, N) cout << ans[y][x] << (x == N - 1 ? "\n" : " "); } return 0; } }; int main() { cin.tie(0); ios::sync_with_stdio(false); Solver s; s.solve(); return 0; }