#include using namespace std; int main() { int n; cin >> n; const int n2 = n * n; auto magicSquare = [=](auto eval) { for (int y = 0; y < n; ++y) { for (int x = 0; x < n; ++x) { cout << eval(x, y); if (x < n - 1) { cout << ' '; } } cout << endl; } }; auto evenMagic = [=](int xbase, int stride) { return [=](int x, int y) { int ybase = ((xbase - x) * 2 + n) % n; int yadd = (y - ybase + n) % n * stride; return ((xbase - x) * n + n2 + yadd) % n2 + 1; }; }; if (n & 1) { magicSquare(evenMagic(n >> 1, n + 1)); } else if (n & 2) { magicSquare([even = evenMagic(n >> 2, (n >> 1) + 1), halfn = n >> 1](int x, int y) { const int halfx = x >> 1; const int halfy = y >> 1; const bool isSwap = halfx == halfn && (halfy == halfn || halfy == halfn + 1); static const int L[][2] = { { 4, 1 },{ 2, 3 } }; static const int U[][2] = { { 1, 4 },{ 2, 3 } }; static const int X[][2] = { { 1, 4 },{ 3, 2 } }; auto m = halfy <= halfn + 1 ? (halfy == halfn + 1) ^ isSwap ? U : L : X; const int val = (even(halfx, halfy) - 1) << 2; return val + m[y & 1][x & 1]; }); } else { magicSquare([n2, i = 0](int x, int y) mutable { const bool isDiagonal = (x & 3) == (y & 3) || 3 - (x & 3) == (y & 3); return isDiagonal ? ++i : n2 - i++; }); } return 0; }