#include <cstdio> #include <iostream> #include <sstream> #include <fstream> #include <iomanip> #include <algorithm> #include <cmath> #include <string> #include <vector> #include <list> #include <queue> #include <stack> #include <set> #include <map> #include <bitset> #include <numeric> #include <limits> #include <climits> #include <cfloat> #include <functional> using namespace std; bool makeMagicSquare(int n, vector<vector<int> >& square) { if(n < 3) return false; square.assign(n, vector<int>(n)); if(n % 2 == 1){ int y = 0; int x = n / 2; int a = 0; for(int i=0; i<n; ++i){ for(int j=0; j<n; ++j){ square[y][x] = ++ a; y = (y + n - 1) % n; x = (x + 1) % n; } y = (y + 2) % n; x = (x + n - 1) % n; } } else if(n % 4 == 0){ queue<int> q; int a = 0; for(int y=0; y<n; ++y){ for(int x=0; x<n; ++x){ if((y % 4 == x % 4) || (y % 4 == 3 - x % 4)) square[y][x] = ++ a; else q.push(++ a); } } for(int y=n-1; y>=0; --y){ for(int x=n-1; x>=0; --x){ if(!((y % 4 == x % 4) || (y % 4 == 3 - x % 4))){ square[y][x] = q.front(); q.pop(); } } } } else{ const int add[][4] = {{4, 1, 2, 3}, {1, 4, 3, 2}, {1, 4, 2, 3}}; int m = n / 2; vector<vector<int> > tmp; makeMagicSquare(m, tmp); for(int y=0; y<m; ++y){ for(int x=0; x<m; ++x){ int k; if(y < m / 2 || (y == m / 2 && x != m / 2) || (y == m / 2 + 1 && x == m / 2)) k = 0; else if(y > m / 2 + 1) k = 1; else k = 2; for(int i=0; i<4; ++i) square[y*2+i/2][x*2+i%2] = (tmp[y][x] - 1) * 4 + add[k][i]; } } } return true; } int main() { int n; cin >> n; vector<vector<int> > a; makeMagicSquare(n, a); for(int y=0; y<n; ++y){ for(int x=0; x<n; ++x){ cout << a[y][x] << ' '; } cout << endl; } return 0; }