#include <bits/stdc++.h>   using namespace std;   template <typename T>   T abs(T a) {     return a < 0 ? -a : a;   }   template <typename T>   T sqr(T a) {     return a * a;   }   const int INF = (int)1e9;   const long double EPS = 1e-9;   const long double PI = 3.1415926535897932384626433832795;   const int N = 4;   struct pt {     int first, second;     pt() { first = second = 0; }     pt(int cX, int cY) : first(cX), second(cY) {}   };   int n, m;   int used[N * N];   pt a[N * N];   int s;   int sum[3 * N];   int szst[N][N];   int st[N][N][3 * N];   int szfn[N][N];   int fn[N][N][3 * N];   int mn[N][N];   inline void put(int i, int j, int v) {     mn[i][j] = v;     for (int k = 0; k < int(szst[i][j]); ++k) sum[st[i][j][k]] += v;   }   inline void take(int i, int j) {     for (int k = 0; k < int(szst[i][j]); ++k) sum[st[i][j][k]] -= mn[i][j];   }   void solve(int x, int y) {     if (y == n) {       x++;       y = 0;     }     if (x == n) {       printf( %d n , s);       for (int i = 0; i < int(n); ++i) {         for (int j = 0; j < int(n); ++j) {           printf( %d , mn[i][j]);           if (j + 1 != n) printf(   );         }         puts(  );       }       exit(0);     }     for (int i = 0; i < int(m); ++i) {       if (used[i] < a[i].second) {         put(x, y, a[i].first);         used[i]++;         bool good = true;         for (int j = 0; j < int(szfn[x][y]); ++j)           if (sum[fn[x][y][j]] != s) {             good = false;             break;           }         if (good) solve(x, y + 1);         used[i]--;         take(x, y);       }     }   }   void pickRow(int idx) {     if (idx == n) {       s = sum[0];       solve(1, 0);       s = INF;       return;     }     for (int i = 0; i < int(m); ++i)       if (used[i] < a[i].second) {         used[i]++;         put(0, idx, a[i].first);         pickRow(idx + 1);         used[i]--;         take(0, idx);       }   }   int main() {     cin >> n;     vector<int> c;     c.resize(n * n);     for (int i = 0; i < int(n * n); ++i) {       scanf( %d , &c[i]);     }     sort((c).begin(), (c).end());     m = 0;     int cnt = 1;     for (int i = 1; i < n * n; ++i) {       if (c[i] != c[i - 1]) {         a[m++] = pt(c[i - 1], cnt);         cnt = 1;       } else         cnt++;     }     a[m++] = pt(c[n * n - 1], cnt);     for (int i = 0; i < int(n); ++i)       for (int j = 0; j < int(n); ++j) {         st[i][j][szst[i][j]++] = i;         st[i][j][szst[i][j]++] = n + j;         if (i == j) st[i][j][szst[i][j]++] = 2 * n;         if (i == n - j - 1) st[i][j][szst[i][j]++] = 2 * n + 1;         if (j == n - 1) fn[i][j][szfn[i][j]++] = i;         if (i == n - 1) fn[i][j][szfn[i][j]++] = n + j;         if (i == n - 1 && j == n - 1) fn[i][j][szfn[i][j]++] = 2 * n;         if (i == n - 1 && j == 0) fn[i][j][szfn[i][j]++] = 2 * n + 1;       }     random_shuffle(a, a + m);     s = INF;     pickRow(0);     return 0;   }   