The Smart Beaver from ABBYY loves puzzles. One of his favorite puzzles is the magic square. He has recently had an idea to automate the solution of this puzzle. The Beaver decided to offer this challenge to the ABBYY Cup contestants.The magic square is a matrix of size n × n. The elements of this matrix are integers. The sum of numbers in each row of the matrix is equal to some number s. The sum of numbers in each column of the matrix is also equal to s. In addition, the sum of the elements on the main diagonal is equal to s and the sum of elements on the secondary diagonal is equal to s. Examples of magic squares are given in the following figure:   Magic squares  You are given a set of n2 integers ai. It is required to place these numbers into a square matrix of size n × n so that they form a magic square. Note that each number must occur in the matrix exactly the same number of times as it occurs in the original set.It is guaranteed that a solution exists!

The first input line contains a single integer n. The next line contains n2 integers ai ( - 108 ≤ ai ≤ 108), separated by single spaces. The input limitations for getting 20 points are:    1 ≤ n ≤ 3  The input limitations for getting 50 points are:    1 ≤ n ≤ 4  It is guaranteed that there are no more than 9 distinct numbers among ai.  The input limitations for getting 100 points are:    1 ≤ n ≤ 4

The first line of the output should contain a single integer s. In each of the following n lines print n integers, separated by spaces and describing the resulting magic square. In the resulting magic square the sums in the rows, columns and diagonals must be equal to s. If there are multiple solutions, you are allowed to print any of them.

#include <iostream>
#include <string>
#include <map>
#include <math.h>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <set>
#include <queue>
#include <sstream>

using namespace std;

#define sqr(x) ((x)*(x))
#define PB(a) push_back(a)
#define MP(a) make_pair(a)
#define ll long long

int gcd(int a, int b) {
	while (b) b^=a^=b^=a%=b;
	return a;
}

const int maxn=20;

int n,sum;
int a[maxn],b[maxn];
int sx[3], sy[3], sd[3];

bool p[maxn];
bool g[maxn];

map<int,int> mp;
map<int,bool> ff;

int f(int x) {
	if (x==0 || x==1) return 1;
	return x*f(x-1);
}

void dfs(int x) {
	if (x==n*n) {
		if (b[0]+b[5]+b[10]+b[15]==sum && b[12]+b[13]+b[14]+b[15]==sum && b[3]+b[7]+b[11]+b[15]==sum) {
			cout << sum << endl;
			for (int i=0; i<n*n; i++) {
				cout << b[i] << ' ';
				if ((i+1)%n==0) cout << endl;
			}
			exit(0);
		}
		return;
	}
	if (!g[x]) {
		for (int i=0; i<n*n; i++) if (!p[i] && mp[a[i]]>0) {
			b[x]=a[i];
			mp[a[i]]--;
			bool f=true;
			if ((x+2)%n==0) {
				int k=b[x]+b[x-1]+b[x-2];
				if (mp[sum-k]>0) {
					mp[sum-k]--;
					b[x+1]=sum-k;
					g[x+1]=true;
				} else f=false;
			}
			if (x/n==2) {
				int k=b[x]+b[x-4]+b[x-8];
				if (mp[sum-k]>0) {
					mp[sum-k]--;
					b[x+4]=sum-k;
					g[x+4]=true;
				} else f=false;
			}
			if (x==9 && b[3]+b[6]+b[9]+b[12]!=sum) f=false;
			if (f) dfs(x+1);
			if (g[x+1]) {
				g[x+1]=false;
				mp[sum-(b[x]+b[x-1]+b[x-2])]++;
				b[x+1]=0;
			}
			if (g[x+4]) {
				g[x+4]=false;
				mp[sum-(b[x]+b[x-4]+b[x-8])]++;
				b[x+4]=0;
			}
			mp[a[i]]++;
			b[x]=0;
			g[x]=false;
		}
	}
	else dfs(x+1);
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	cin >> n;
	for (int i=0; i<n*n; i++)
		cin >> a[i];
	if (n<=3) {
	for (int oo=0; oo<f(n*n); oo++) {
		int s1=0;
		for (int i=0; i<3; i++) sx[i]=sy[i]=sd[i]=0;

		for (int i=0; i<n; i++) {
			for (int j=0; j<n; j++) {
				sx[i]+=a[i+n*j];
				sy[i]+=a[j+n*i];
			}
		}
		if (n==1) {
			sd[0]=sd[1]=a[0];
		} else if (n==2) {
			sd[0]=a[0]+a[3];
			sd[1]=a[1]+a[2];
		} else {
			sd[0]=a[0]+a[4]+a[8];
			sd[1]=a[2]+a[4]+a[6];
		}
		bool f=true;
		for (int i=0; i<n; i++)
			f=f && sx[0]==sx[i] && sx[0]==sy[i];
		f=f && sx[0]==sd[1] && sx[0]==sd[0];
		if (f) {
			cout << sx[0] << endl;
			for (int i=0; i<n*n; i++) {
				cout << a[i] << ' ';
				if ((i+1)%n==0) cout << endl;
			}
			return 0;
		}
		next_permutation(a,a+9);
	}
	}
	else {
		sum=0;
		for (int i=0; i<n*n; i++) mp[a[i]]++, ff[a[i]]=true, sum+=a[i];
		for (int i=0; i<n*n; i++)
			for (int j=i+1; j<n*n; j++)
				if (a[i]==a[j]) p[j]=true;
		sum/=n;
		dfs(0);
	}
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

mt19937 mt_rand(chrono::system_clock::now().time_since_epoch().count());

const int N = 22;

int n, tot;
int target;
int id[N];
int a[N];
int b[N][N];

inline int eval() {
  for (int i = 0; i < tot; i++) {
    b[i / n][i % n] = a[id[i]];
  }
  int ret = 0;
  for (int i = 0; i < n; i++) {
    int sum = 0;
    for (int j = 0; j < n; j++) {
      sum += b[i][j];
    }
    ret = max(ret, abs(sum - target));
    sum = 0;
    for (int j = 0; j < n; j++) {
      sum += b[j][i];
    }
    ret = max(ret, abs(sum - target));
  }
  int sum = 0;
  for (int i = 0; i < n; i++) {
    sum += b[i][i];
  }
  ret = max(ret, abs(sum - target));
  sum = 0;
  for (int i = 0; i < n; i++) {
    sum += b[i][n - i - 1];
  }
  ret = max(ret, abs(sum - target));
  return ret;
}

int main() {
  //freopen("input.txt", "r", stdin);
  //freopen("output.txt", "w", stdout);
  scanf("%d", &n);
  tot = n * n;
  for (int i = 0; i < tot; i++) {
    scanf("%d", a + i);
    id[i] = i;
    target += a[i];
  }
  target /= n;
  for (;;) {
    int x = mt_rand() % tot, y = mt_rand() % tot;
    int h = eval();
    swap(id[x], id[y]);
    int hh = eval();
    if (hh > h) {
      swap(id[x], id[y]);
    } else {
      h = hh;
    }
    if (h == 0) {
      break;
    }
  }
  for (int i = 0; i < tot; i++) {
    b[i / n][i % n] = a[id[i]];
  }
  printf("%d\n", target);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      printf("%d ", b[i][j]);
    }
    printf("\n");
  }
  return 0;
}

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <ctime>

using namespace std;

int n, S[4][4], ans, N, B[9];
bool got_it;
vector <int> A;

bool TestRow(int i) {
	int sum = 0;
	for(int j = 0; j < n; ++j)
		sum += S[i][j];
	return sum == ans;
}

bool TestCol(int j) {
	int sum = 0;
	for(int i = 0; i < n; ++i)
		sum += S[i][j];
	return sum == ans;
}

bool TestDiag() {
	int sum = 0;
	for(int i = 0; i < n; ++i)
		sum += S[i][n - 1 - i];
	return sum == ans;
}

bool TestMainDiag() {
	int sum = 0;
	for(int i = 0; i < n; ++i)
		sum += S[i][i];
	return sum == ans;
}

void rec(int x, int y) {
	if(x > 0) {
		if(x == n) {
			if(TestMainDiag() && TestRow(n - 1) && TestCol(n - 1))
				got_it = true;
			return;
		}
		else if(x == n-1) {
			if(y > 0)
				if(!TestCol(y-1) || (y == 1 && !TestDiag()))
					return;
		}
		if(y == 0) {
			if(!TestRow(x - 1))
				return;
		}
	}

	for(size_t i = 0; i < A.size(); ++i) {
		if(B[i] > 0) {
			S[x][y] = A[i];
			--B[i];
			if(y + 1 == n)
				rec(x + 1, 0);
			else
				rec(x, y + 1);
			if(got_it)
				return;
			++B[i];
		}
	}
}

int main() {
	//freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);

	cin >> n;
	N = n*n;
	int a;
	for(int i = 0; i < N; ++i) {
		cin >> a;
		ans += a;
		bool f = false;
		for(size_t j = 0; j < A.size(); ++j)
			if(A[j] == a) {
				B[j]++;
				f = true;
				break;
			}
		if(!f) {
			A.push_back(a);
			B[A.size() - 1]++;
		}
	}

	ans /= n;

	rec(0, 0);

	cout << ans << endl;
	for(int i = 0; i < n; ++i) {
		for(int j = 0; j < n; ++j)
			cout << S[i][j] << " ";
		cout << endl;
	}

	return 0;
}

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <stdlib.h>
#include <time.h>

using namespace std;

#define onlylove "My Sky"
#define mysky "TT"

#define X first
#define Y second
#define sz(A) int(A.size())
#define mpair make_pair
#define LL(x) (x << 1)
#define RR(x) ((x << 1) | 1 )
#define sqr(x) ((x)*(x))
#define Rep(i , n) for(int i = 0 , _a = n ; i < _a ; i++)
#define For( i , a , b) for(int _a = a , _b = b , i = _a ; i <= _b ; i++)
#define Ford(i , a , b) for(int _a = a , _b = b , i = _a ; i >= _b ; i--)

typedef long long ll;
typedef pair<int , int> pt;

const int maxn = 5;
int n;
int a[20];
ll target;

void ReadData() {
    cin >> n;
    Rep(i , sqr(n)) cin >> a[i];
}

int Code(int x , int y) {
    return x * n + y;
}

ll f(int a[]) {
    ll chinh = 0 , phu = 0;
    ll cot[5] , hang[5];
    ll res = 0;
    Rep(i , n) {
        hang[i] = 0;
        Rep(j , n) hang[i] += a[Code(i , j)];
        res += abs(hang[i] - target);
    }
    Rep(j , n) {
        cot[j] = 0;
        Rep(i , n) cot[j] += a[Code(i , j)];
        res += abs(cot[j] - target);
        Rep(i , n) if (i == j) chinh += a[Code(i , j)];
        Rep(i , n) if (i + j == n - 1) phu += a[Code(i , j)];
    }
    res += abs(chinh - target) + abs(phu - target);
    return res;
}

int pos[20];
int b[20];

void Rand(int a[20]) {
    Rep(i , sqr(n)) {
        a[i] = i;
    }
    random_shuffle(a , a + sqr(n));
}

void Process() {
    Rep(i , sqr(n)) target += a[i];
    target /= n;
    cout << target << "\n";
    Rep(step , 1500) {
        Rand(pos);
        For(i , 0 , sqr(n) - 1) b[i] = a[pos[i]];
        ll last = f(b);
        Rep(leo , 5000) {
            int p1 = rand() % sqr(n) , p2 = rand() % sqr(n);
            if (p1 == p2) p2 = (p2 + 1) % sqr(n);
            swap(b[p1] , b[p2]);
            ll cur = f(b);
            if (cur > last)  {
                swap(b[p1] , b[p2]);
            } else {
                last = cur;
            }
        }
        if (last == 0) {
            Rep(i , n){
                Rep(j , n) cout << b[Code(i , j)] << " "; cout << "\n";
            }
            return;
        }
    }
}

int main() {
    std ::ios_base::sync_with_stdio(false);
    cin.tie(0);

    srand(time(NULL));
   // freopen("input.inp","r",stdin);
  //  freopen("out1.out" , "w" , stdout);

    ReadData(); Process();

    return 0;
}

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cmath>
#include <memory.h>
#include <string>
#include <set>
#include <map>
#include <queue>
using namespace std;
#define forn(i,n) for(int i=0;i<(n);++i)
#define forv(i,v) forn(i,(int)(v).size())
#define all(v) (v).begin(),(v).end()

int n;
int rt[16];
int s;

int f(){
    int r=0;
    forn(i, n){
        int qw = 0;
        for(int j=i*n;j<(i+1)*n;++j){
            qw += rt[j];
        }
        r+=abs(qw-s);
    }
    forn(i, n){
        int qw = 0;
        for(int j=i;j<n*n;j+=n){
            qw += rt[j];
        }
        r+=abs(qw-s);
    }
    {
        int qw = 0;
        for(int i=0;i<n*n;i+=n+1)qw += rt[i];
        r+=abs(qw-s);
    }
    int qw = 0;
    int j=0;
    for(int i=n-1;i<n*n;i+=n-1,++j){
        if (j==n)break;
        qw += rt[i];
    }
    r+=abs(qw-s);
    return r;
}

void print(){
    cout << s << endl;
    forn(i,n*n){
        cout<<rt[i]<<" ";
        if (i%n==n-1)cout<<endl;
    }
    exit(0);
}

int main(){
    cin >> n;
    s=0;
    forn(i,n*n){
        cin>>rt[i];
        s+=rt[i];
    }
    s=s/n;
    for(;;){
        random_shuffle(rt, rt+n*n);
        forn(i, 5){
            int q = f();
            if (q==0)
                print();
            forn(i1, n*n){
                for(int i2=i1+1;i2<n*n;++i2){
                    swap(rt[i1], rt[i2]);
                    int tr = f();
                    if (tr>=q){
                        swap(rt[i1], rt[i2]);
                    }else{
                        q = tr;
                    }
                }
            }
l1:;
        }
    }

    return 0;
}

#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int n;

bool isMagical(const vector<int>& a) {
    auto it = a.begin();
    int b[4][4];
    for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) {
        b[i][j] = *it;
        ++it;
    }

    set<int> sums;
    int d1 = 0, d2 = 0;
    for (int i = 0; i < n; ++i) {
        d1 += b[i][i];
        d2 += b[i][n-i-1];
        int h = 0, v = 0;
        for (int j = 0; j < n; ++j) {
            h += b[i][j];
            v += b[j][i];
        }
        sums.insert(h);
        sums.insert(v);
    }

    sums.insert(d1);
    sums.insert(d2);

    return sums.size() == 1;
}

int main() {
    cin >> n;
    vector<int> a(n*n);
    for (int i = 0; i < n*n; ++i)
        cin >> a[i];
    sort(a.begin(), a.end());
    do {
        if (isMagical(a)) {
            int s = accumulate(a.begin(), a.begin() + n, 0);
            cout << s << endl;
            for (int i = 0; i < n*n; ++i) {
                cout << a[i];
                if (i%n == n-1)
                    cout << endl;
                else
                    cout << " ";
            }
            return 0;
        }
    } while (next_permutation(a.begin(), a.end()));
    return 0;
}

#include <cstdio>
#include <map>
#include <algorithm>
using namespace std;

int total;
int ps[32];
int N;

int num[32];
int rs[32];
int board[8][8];
int p1[32];
int p2[32];

int pick(int x)
{
    if(!rs[x])
	return 0;
    if(--rs[x] == 0)
    {
	p2[p1[x]] = p2[x];
	p1[p2[x]] = p1[x];
    }
    return x;
}
void put(int x)
{
    if(rs[x]++ == 0)
    {
	p2[p1[x]] = x;
	p1[p2[x]] = x;
    }
}

int find(int v)
{
    int l = 0, r = total;
    while(l + 1 < r)
    {
	int m = (l + r) / 2;
	if(num[m] == v)
	    return pick(m);
	else if(num[m] < v)
	    l = m;
	else
	    r = m;
    }
    return 0;
}

bool go(int x, int y, int g)
{
    if(x == N)
	throw g;//return true;
    if(x == N - 1)
    {
	int v = 0;
	for(int i = 0; i < N - 1; i++)
	    v += board[i][y];
	int p = g - v;

	int z = find(p);
	if(z)
	    put(z);
	if(!z)
	    return false;

	if(y == 0)
	{
	    v = 0;
	    for(int i = 0; i < N - 1; i++)
		v += board[i][N - 1 - i];
	    if(g - v != p)
		return false;
	}
	if(y == N - 1)
	{
	    v = 0;
	    for(int i = 0; i < N - 1; i++)
		v += board[i][i];
	    if(g - v != p)
		return false;

	    v = 0;
	    for(int i = 0; i < N - 1; i++)
		v += board[N - 1][i];
	    if(g - v != p)
		return false;
	}

	pick(z);
	board[x][y] = p;
	if(go(x + (y == N - 1), (y == N - 1)? 0: (y + 1), g))
	    return true;
	put(z);
    }
    else if(y == N - 1 && (x != 0))
    {
        int v = 0;
	for(int i = 0; i < N - 1; i++)
	    v += board[x][i];
	int p = g - v;
	int z = find(p);
	if(z)
	    put(z);
	if(!z)
	    return false;
	pick(z);
	board[x][y] = p;
	if(go(x + 1, 0, g))
	    return true;
	put(z);
    }
    else
    {
	for(int z = p2[0]; z; z = p2[z])
	{
	    board[x][y] = num[z];
	    pick(z);
	    if(y == N - 1)
	    {
		g = 0;
		for(int i = 0; i < N; i++)
		    g += board[0][i];
		if(go(x + 1, 0, g))
		    return true;
	    }
	    else
		if(go(x, y + 1, g))
		    return true;
	    put(z);
	}
    }
    return false;
}

int main()
{
    scanf("%d", &N);
    for(int i = 0; i < N * N; i++)
	scanf("%d", ps + i);

    if(N == 1)
	printf("%d\n%d\n", ps[0], ps[0]);

    sort(ps, ps + N * N);

    total = 1;
    for(int i = 0; i < N * N; i++)
	if(!i || ps[i] != num[total - 1])
	{
	    num[total] = ps[i];
	    rs[total] = 1;
	    total++;
	}
	else
	    rs[total - 1]++;

    p2[0] = 1;
    for(int i = 1; i < total; i++)
    {
	if(i)
	    p1[i] = i - 1;
	p2[i] = i + 1;
    }
    p2[total - 1] = 0;

    try
    {
    go(0, 0, 0);
    int v = 0;
    for(int i = 0; i < N; i++)
	v += board[0][i];
    }
    catch(int v)
    {
	printf("%d\n", v);
	for(int i = 0; i < N; i++, printf("\n"))
	    for(int j = 0; j < N; j++)
	    {
		if(j)
		    printf(" ");
		printf("%d", board[i][j]);
	    }

    }

    return 0;
}

#include <bits/stdc++.h>
#define dbg(x) cerr << ">>> " << x << endl;
#define _ << " , " <<
#define endl '\n'

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;

int arr[50], n, s;
int ma[5][5];
int sum_row[5];
int sum_col[5];
map<int, int> freq;
void go(int x, int y) {

  if (x == n) {
    int s1 = 0, s2 = 0;
    for (int i = 0; i < n; ++i) {
      s1 += ma[i][i];
      s2 += ma[n - i - 1][i];
    }
    if (s1 != s || s2 != s) return;

    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        cout << ma[i][j];
        if (j + 1 < n) cout << ' ';
      }
      cout << endl;
    }
    exit(0);
  }
  if (x == n - 1 && y == n - 1) {
    if (sum_row[x] != sum_col[y]) return;
  }
  if (x == n - 1) {
    if (freq.count(s - sum_col[y]) && freq[s - sum_col[y]] > 0) {
      ma[x][y] = s - sum_col[y];
      --freq[ma[x][y]];
      sum_row[x] += ma[x][y];
      if (y == n - 1) go(x + 1, 0);
      else go(x, y + 1);
      ++freq[ma[x][y]];
      sum_row[x] -= ma[x][y];
    }
    return;
  }

  if (y == n - 1) {
    if (freq.count(s - sum_row[x]) && freq[s - sum_row[x]] > 0) {
      ma[x][y] = s - sum_row[x];
      --freq[ma[x][y]];
      sum_col[y] += ma[x][y];
      go(x + 1, 0);
      ++freq[ma[x][y]];
      sum_col[y] -= ma[x][y];
    }
    return;
  }

  for (auto &v : freq) {
    if (v.second > 0) {
      ma[x][y] = v.first;
      --v.second;
      sum_row[x] += v.first;
      sum_col[y] += v.first;
      go(x, y + 1);
      ++v.second;
      sum_row[x] -= v.first;
      sum_col[y] -= v.first;
    }
  }
}

int main() {
  // ios::sync_with_stdio(0); cin.tie(0);
  cin >> n;
  for (int i = 0; i < n * n; ++i) {
    cin >> arr[i];
    ++freq[arr[i]];
    s += arr[i];
  }
  s /= n;
  cout << s << endl;
  go(0, 0);
  return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <string.h>

using namespace std;

int b[30],c[30];
int a[4][4],j;
int n,sum,sum1;
int s[30];
int d[30],r[30];

void get(int num){
/*    ans++;
    if (ans%1000000==0)
        cerr << ans << " " << a[0][0] <<  endl;*/
    if (num==2*n && s[0]!=s[1])
        return;
    if (num==3*n && s[0]!=s[2])
        return;
    if (num==4*n && s[0]!=s[3])
        return;
    if (num==(n-1)*n + 2 && d[0]!=d[1])
        return;
    if (num==(n-1)*n + 3 && d[0]!=d[2])
        return;
    if (num==(n-1)*n + 4 && d[0]!=d[3])
        return;
    if (num==n*n){
        sum = 0;
        sum1 = 0;
        for (int i=0; i<n; i++)
            sum1+=a[i][i];
        for (int i=0; i<n; i++)
            sum += a[n-1-i][i];
        if (sum!=sum1)
            return;
        cout << sum << endl;
        for (int i=0; i<n; i++){
            for (int j=0; j<n; j++)
                cout << a[i][j] << " ";
            cout << endl;
        }
        exit(0);
    }
    for (int i=0; i<=j; i++)
        if (c[i]){
            c[i]--;
            a[num/n][num%n] = r[i];
            s[num/n] += r[i];
            d[num%n] += r[i];
            get(num+1);
            s[num/n] -= r[i];
            d[num%n] -= r[i];
            c[i]++;
        }

}

int main(void){
    //freopen("input.txt","r",stdin);//freopen("output.txt","w",stdout);

    cin >> n;
    for (int i=0; i<n*n; i++)
        cin >> b[i];

    sort(b,b+n*n);
    j = 0;
    c[0] = 1;
    r[0] = b[0];
    for (int i=1; i<n*n; i++)
        if (b[i]==b[i-1])
            c[j]++;
        else{
            j++;
            r[j]=b[i];
            c[j]++;
        }

    get(0);

    return 0;
}

#include <bits/stdc++.h>

using namespace std;

const int maxn = 20;

int a[maxn], b[maxn], ans[maxn][maxn], mark[maxn];

void bt(int k,int n,int s) {
	if (k == n * n) {

		int sum1 = 0, sum2 = 0;
		for (int i = 0; i < n; i++)
			sum1 += a[b[i * n + i]], sum2 += a[b[i * n + (n - 1 - i)]];
		if (sum1 != s || sum2 != s)
			return;

		for (int i = 0; i < n * n; i++)
			ans[i / n][i % n] = a[b[i]];

		cout << s << '\n';
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++)
				cout << ans[i][j] << ' ';
			cout << '\n';
		}
		exit(0);
	}
	if (k > 0 && k % n == 0) {
		int sum = 0;
		for (int i = 0; i < n; i++)
			sum += a[b[k - 1 - i]];
		if (sum != s)
			return;
	}
	if (k > n * (n - 1)) {
		int sum = 0, c = (k - 1) % n;
		for (int i = 0; i < n; i++)
			sum += a[b[i * n + c]];
		if (sum != s)
			return;
	}
	for (int i = 0; i < n * n; i++)
		if (!mark[i]) {
			mark[i] = true;
			b[k] = i;
			bt(k + 1, n, s);
			mark[i] = false;
		}
}

int main() {
	srand(701);
	int n, s = 0;
	cin >> n;
	for (int i = 0; i < n * n; i++)
		cin >> a[i], s += a[i];
	random_shuffle(a, a + n * n);
	s /= n;
	bt(0, n, s);
	return 0;
}

#include<iostream>
#include<algorithm>

using namespace std;

int mat[3][3],a[10],s,n;

bool check(int x[][3])
{
  int b=0,c;
  for(int i=0;i<n;i++)
    b+=x[i][0];
  for(int i=0;i<n;i++)
  {
    c=0;
    for(int j=0;j<n;j++)
      c+=x[i][j];
    if(c!=b)
      return 0;
  }
  for(int i=1;i<n;i++)
  {
    c=0;
    for(int j=0;j<n;j++)
      c+=x[j][i];
    if(c!=b)
      return 0;
  }
  c=0;
  for(int i=0,j=0;i<n;i++,j++)
  {
    c+=x[i][j];
  }
  if(c!=b)
    return 0;
  c=0;
  for(int i=0,j=n-1;i<n;i++,j--)
  {
    c+=x[i][j];
  }
  if(c!=b)
    return 0;
  return 1;
}

int main()
{
  cin>>n;
  for(int i=0;i<n*n;i++)
    cin>>a[i];
  int x[3][3];
  sort(a,a+n*n);
  do{
    for(int i=0,k=0;i<n;i++)
    {
      for(int j=0;j<n;j++,k++)
	x[i][j]=a[k];
    }
    if(check(x))
    {
      for(int i=0;i<n;i++)
      {
	int c=0;
	for(int j=0;j<n;j++)
	{
	  c+=x[i][j];
	  mat[i][j]=x[i][j];
	}
	s=c;
      }
    }
  }while(next_permutation(a,a+n*n));
  cout<<s<<"\n";
  for(int i=0;i<n;i++)
  {
    for(int j=0;j<n;j++)
      cout<<mat[i][j]<<" ";
    cout<<"\n";
  }
  return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,nn,a[10];
	cin>>n,nn=n*n;
	for (int i=0;i<nn;i++) cin>>a[i];
	sort(a,a+nn);
	do{
		int s1[4],s2[4],s3=0,s4=0;
		for (int i=0;i<n;i++) s1[i]=s2[i]=0,s3+=a[i*n+i],s4+=a[i*n+n-i-1];
		for (int i=0;i<nn;i++) s2[i%n]+=a[i];
		for (int i=0;i<nn;i++) s1[i/n]+=a[i];
		bool flag=true;
		for (int i=0;i<n;i++) if (s1[i]!=s2[i] || s1[i]!=s1[0]) flag=false;
		if (flag && s3==s1[0] && s4==s1[0])
		{
			cout<<s1[0]<<'\n';
			for (int i=0;i<n;i++)
			{
				for (int j=0;j<n;j++) if (j!=n-1) cout<<a[i*n+j]<<' '; else cout<<a[i*n+j];
				if (i!=n-1) cout<<'\n';
			}
			return 0;
		}
	}while(next_permutation(a,a+nn));
}

#define _CRT_SECURE_NO_DEPRECATE
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <iostream>
#include <iterator>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <sstream>
#include <fstream>
#include <ctime>
#include <cstring>
#include <unordered_set>
#include <unordered_map>
#pragma comment(linker, "/STACK:66777216")
using namespace std;
#define pb push_back
#define ppb pop_back
#define pi 3.1415926535897932384626433832795028841971
#define mp make_pair
#define x first
#define y second
#define pii pair<int,int>
#define pdd pair<double,double>
#define INF 1000000000
#define FOR(i,a,b) for (int _n(b), i(a); i <= _n; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;i--)
#define all(c) (c).begin(), (c).end()
#define SORT(c) sort(all(c))
#define rep(i,n) FOR(i,1,(n))
#define rept(i,n) FOR(i,0,(n)-1)
#define L(s) (int)((s).size())
#define C(a) memset((a),0,sizeof(a))
#define VI vector <int>
#define ll long long

int a,b,c,d,i,j,n,m,k;
void brute() {
	int mas[4][4];
	VI num;
	C(mas);
	rept(i, n * n) {
		scanf("%d", &a);
		num.pb(a);
	}
	SORT(num);
	do {
		a = 0;
		rept(i, n) {
			rept(j, n) {
				mas[i][j] = num[a++];
			}
		}

		int s = -INF;
		bool ok = 1;
		rept(i, n) {
			int sum = 0;
			rept(j, n) sum += mas[i][j];
			if (s == -INF) s = sum; else
			if (sum != s) {
				ok = 0;
				break;
			}

			sum = 0;
			rept(j, n) sum += mas[j][i];
			if (sum != s) {
				ok = 0;
				break;
			}
		}
		int sum = 0;
		rept(i, n) sum += mas[i][i];
		if (sum != s) ok = 0;
		sum = 0;
		rept(i, n) sum += mas[n - i - 1][i];
		if (sum != s) ok = 0;
		if (ok == 1) {
			printf("%d\n", s);
			rept(i, n) {
				rept(j, n) {
					if (j) printf(" ");
					printf("%d", mas[i][j]);
				}
				printf("\n");
			}
			exit(0);
		}
	} while (next_permutation(all(num)));
}

int mas[16];
int sum[2001], num[1 << 16], inv[2001];
VI perm[2001][24];
unordered_map<unsigned ll, unsigned ll> q;

void die(unsigned ll rh, int n1, int n2, int cp1, int cp2, int sum) {
	int p2 = rh % 24;
	rh /= 24;
	int p1 = rh % 24;
	rh /= 24;
	int v2 = rh % k;
	rh /= k;
	int v1 = rh;

	printf("%d\n", sum);
	rept(i, 4) {
		VI cur;
		if (!i) cur = perm[n1][cp1]; else
		if (i == 1) cur = perm[n2][cp2]; else
		if (i == 2) cur = perm[v2][p2]; else
		cur = perm[v1][p1];
		rept(j, 4) {
			if (j) printf(" ");
			printf("%d", cur[j]);
		}
		printf("\n");
	}
	exit(0);
}
int main() {
	srand(25091992);
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);

	scanf("%d", &n);
	if (n <= 3) brute();
	rept(i, n * n) {
		scanf("%d", &mas[i]);
	}
	random_shuffle(mas, mas + n * n);

	rept(mask, 1 << (n * n)) {
		c = 0; d = 0;
		rept(i, n * n) if (mask & 1 << i) {
			c += mas[i];
			++d;
		}
		if (d != 4) continue;
		sum[k] = c;
		inv[k] = mask;
		num[mask] = ++k;

		VI cur;
		rept(i, n * n) if (mask & 1 << i) cur.pb(mas[i]);
		SORT(cur);
		a = 0;
		do {
			perm[k - 1][a++] = cur;
		} while (next_permutation(all(cur)));
	}
	int ts = 0;
	rept(i, n * n) ts += mas[i];

	rept(i, k) {
		if (ts != sum[i] * 4) continue;
		rept(j, k) {
			if (inv[i] & inv[j]) continue;
			if (sum[i] != sum[j]) continue;

			int s = sum[i];
			rept(p1, 24) {
				if (L(perm[i][p1]) == 0) break;
				const VI &cp1 = perm[i][p1];
				rept(p2, 24) {
					if (perm[j][p2].empty()) break;
					const VI &cp2 = perm[j][p2];

					unsigned ll h = 1, rh = 1;
					rept(z, 4) {
						h = h * 1234567891 + cp1[z] + cp2[z];
						rh = rh * 1234567891 + s - cp1[z] - cp2[z];
					}
					h = h * 1234567891 + cp1[0] + cp2[1];
					rh = rh * 1234567891 + s - (cp1[3] + cp2[2]);

					h = h * 1234567891 + cp1[3] + cp2[2];
					rh = rh * 1234567891 + s - (cp1[0] + cp2[1]);

					h = h * 1234567891 * (inv[i] | inv[j]);
					rh = rh * 1234567891 * ((~(inv[i] | inv[j])) & ((1 << (n * n)) - 1));

					if (q.count(rh)) {
						die(q[rh], i, j, p1, p2, s);
					}

					if (i >= j) continue;
					unsigned ll val = (ll)k * 24 * 24 * i + (ll)24 * 24 * j + (ll)24 * p1 + p2;
					q[h] = val;
				}
			}
		}
	}
}

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <time.h>
#define filename "bunkers"
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double PI = acos(-1.);
const ll inf = 10000000000000LL;
const int MAXN = 200000 + 13;
#define TB ios_base::sync_with_stdio(0);

struct str {
	int id, h;
	char f;
} ;
int n, a[5][5], s=0;

int check(int a[], int b[], int c[]) {
    for (int i=0; i<n; ++i)
        if (a[i] != s || b[i] != s) return 0;
    if (c[0] != s || c[1] != s) return 0;
    return 1;
}

void formmat() {
    srand(time(NULL));
    int r1, c1, r2, c2;
    int rsum[5]={}, csum[5]={}, d[2]={};
    for (int i=0; i<n; ++i) {
        d[0] += a[i][i], d[1] += a[n-1-i][i];
        for (int j=0; j<n; ++j)
            rsum[i] += a[i][j], csum[j] += a[i][j];
    }
    while (666) {
        if (check(rsum, csum, d)) break;
        r1 = rand() % n; c1 = rand() % n;
        r2 = rand() % n; c2 = rand() % n;
        rsum[r1] += a[r2][c2]-a[r1][c1];
        csum[c1] += a[r2][c2]-a[r1][c1];
        rsum[r2] += a[r1][c1]-a[r2][c2];
        csum[c2] += a[r1][c1]-a[r2][c2];
        swap(a[r1][c1], a[r2][c2]);
        d[0] = d[1] = 0;
        for (int i=0; i<n; ++i)
            d[0] += a[i][i], d[1] += a[i][n-1-i];
    }
}

int main() {
//	freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
//	freopen(filename".in", "r", stdin); freopen(filename".out", "w", stdout);
    cin >> n;
    for (int i=0; i<n*n; ++i) cin >> a[i/n][i%n], s += a[i/n][i%n];
    s /= n;
    cout << s << endl;
    formmat();
    for (int i=0; i<n; ++i, cout << endl)
        for (int j=0; j<n; ++j) cout << a[i][j] << ' ';
    return 0;
}

#include <iostream>
#include <algorithm>

using namespace std;

int a[100];
bool used[100];
int table[100][100];
int sum[100][100];
int n, n2;

bool check()
{
    int s = 0;
    for (int i = 0; i < n; i++)
    {
        s += table[0][i];
    }
    for (int i = 0; i < n; i++)
    {
        int cs = 0;
        for (int j = 0; j < n; j++)
        {
            cs += table[i][j];
        }
        if (cs != s)
            return false;
    }
    for (int j = 0; j < n; j++)
    {
        int cs = 0;
        for (int i = 0; i < n; i++)
            cs += table[i][j];
        if (cs != s)
            return false;
    }
    int cs = 0;
    for (int i = 0; i < n; i++)
        cs += table[i][i];
    if (cs != s)
        return false;
    cs = 0;
    for (int i = 0; i < n; i++)
        cs += table[i][n - i - 1];
    if  (cs != s)
        return false;
    return true;
}

bool solve(int cnt)
{
    if (cnt == n2 && check())
        return true;
    if (cnt == n2)
        return false;
    int i = cnt / n;
    int j = cnt % n;
    for (int k = 0; k < n2; k++)
    {
        if (!used[k])
        {
            table[i][j] = a[k];
            sum[i][j] = (i != 0 ? sum[i - 1][j] : 0) + (j != 0 ? sum[i][j - 1] : 0) - ((i != 0 && j != 0) ? sum[i - 1][j - 1] : 0) + a[k];
            if (i != 0 && j == n - 1 && sum[i][j] - sum[i - 1][j] > sum[0][n - 1])
                return false;
            if (i != 0 && i == n - 1 && (sum[i][j] - (j != 0 ? sum[i][j - 1] : 0)) > sum[0][n - 1])
                return false;
            used[k] = true;
            if (solve(cnt + 1))
                return true;
            used[k] = false;
        }
    }
}

int main()
{
    cin >> n;
    n2 = n * n;
    for (int i = 0; i < n2; i++)
        cin >> a[i];
    sort(a, a + n2);
    solve(0);
    int s = 0;
    for (int i = 0; i < n; i++)
        s += table[0][i];
    cout << s << endl;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
            cout << table[i][j] << " ";
        cout << endl;
    }
    return 0;
}

#include <cstdio>
#include <map>
#include <algorithm>
using namespace std;

int total;
int ps[32];
int N;

int num[32];
int rs[32];
int board[8][8];
int p1[32];
int p2[32];

int pick(int x)
{
    if(!rs[x])
	return 0;
    if(--rs[x] == 0)
    {
	p2[p1[x]] = p2[x];
	p1[p2[x]] = p1[x];
    }
    return x;
}
void put(int x)
{
    if(rs[x]++ == 0)
    {
	p2[p1[x]] = x;
	p1[p2[x]] = x;
    }
}

int find(int v)
{
    int l = 0, r = total;
    while(l + 1 < r)
    {
	int m = (l + r) / 2;
	if(num[m] == v)
	    return pick(m);
	else if(num[m] < v)
	    l = m;
	else
	    r = m;
    }
    return 0;
}

bool go(int x, int y, int g)
{
    if(x == N)
	return true;
    if(x == N - 1)
    {
	int v = 0;
	for(int i = 0; i < N - 1; i++)
	    v += board[i][y];
	int p = g - v;

	int z = find(p);
	if(z)
	    put(z);
	if(!z)
	    return false;

	if(y == 0)
	{
	    v = 0;
	    for(int i = 0; i < N - 1; i++)
		v += board[i][N - 1 - i];
	    if(g - v != p)
		return false;
	}
	if(y == N - 1)
	{
	    v = 0;
	    for(int i = 0; i < N - 1; i++)
		v += board[i][i];
	    if(g - v != p)
		return false;

	    v = 0;
	    for(int i = 0; i < N - 1; i++)
		v += board[N - 1][i];
	    if(g - v != p)
		return false;
	}

	pick(z);
	board[x][y] = p;
	if(go(x + (y == N - 1), (y == N - 1)? 0: (y + 1), g))
	    return true;
	put(z);
    }
    else if(y == N - 1 && (x != 0))
    {
        int v = 0;
	for(int i = 0; i < N - 1; i++)
	    v += board[x][i];
	int p = g - v;
	int z = find(p);
	if(z)
	    put(z);
	if(!z)
	    return false;
	pick(z);
	board[x][y] = p;
	if(go(x + 1, 0, g))
	    return true;
	put(z);
    }
    else
    {
	for(int z = p2[0]; z; z = p2[z])
	{
	    board[x][y] = num[z];
	    pick(z);
	    if(y == N - 1)
	    {
		g = 0;
		for(int i = 0; i < N; i++)
		    g += board[0][i];
		if(go(x + 1, 0, g))
		    return true;
	    }
	    else
		if(go(x, y + 1, g))
		    return true;
	    put(z);
	}
    }
    return false;
}

int main()
{
    scanf("%d", &N);
    for(int i = 0; i < N * N; i++)
	scanf("%d", ps + i);

    if(N == 1)
    {
	printf("%d\n%d\n", ps[0], ps[0]);
	return 0;
    }

    sort(ps, ps + N * N);

    total = 1;
    for(int i = 0; i < N * N; i++)
	if(!i || ps[i] != num[total - 1])
	{
	    num[total] = ps[i];
	    rs[total] = 1;
	    total++;
	}
	else
	    rs[total - 1]++;

    p2[0] = 1;
    for(int i = 1; i < total; i++)
    {
	if(i)
	    p1[i] = i - 1;
	p2[i] = i + 1;
    }
    p2[total - 1] = 0;

    go(0, 0, 0);
    int v = 0;
    for(int i = 0; i < N; i++)
	v += board[0][i];
	printf("%d\n", v);
	for(int i = 0; i < N; i++, printf("\n"))
	    for(int j = 0; j < N; j++)
	    {
		if(j)
		    printf(" ");
		printf("%d", board[i][j]);
	    }

    return 0;
}

#define _CRT_SECURE_NO_DEPRECATE
#define _SECURE_SCL 0
#pragma comment(linker, "/STACK:200000000")

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <fstream>
#include <iostream>
#include <map>
#include <memory.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <sstream>
#include <vector>
#include <utility>
#include <cmath>
using namespace std;

#define pb push_back
#define mp make_pair
#define sz(a) (int)(a).size()
#define all(a) (a).begin(), (a).end()

#define forn(i,n) for (int i=0; i<int(n); ++i)
#define fornd(i,n) for (int i=int(n)-1; i>=0; --i)
#define forab(i,a,b) for (int i=int(a); i<=int(b); ++i)

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

const int INF = (int) 1e9;
const long long INF64 = (long long) 1e18;
const long double eps = 1e-9;
const long double pi = 3.14159265358979323846;

ll d[3][3];

int main(){
#ifndef ONLINE_JUDGE
    freopen("input.txt","rt",stdin);
    freopen("output.txt","wt",stdout);
#endif
    int n;
    int nn;
    cin >> n;
    nn = n*n;
    vector <ll> a(nn);
    forn(i,nn)
        cin >> a[i];
    sort(all(a));
    do{
        forn(i,nn)
            d[i/n][i%n] = a[i];
        set <ll> tsum;
        ll sum3 = 0;
        ll sum4 = 0;
        forn(i,n){
            ll sum1 = 0;
            ll sum2 = 0;
            forn(j,n){
                sum1 += d[i][j];
                sum2 += d[j][i];
            }
            sum3 += d[i][i];
            sum4 += d[i][n-i-1];
            tsum.insert(sum1);
            tsum.insert(sum2);
        }
        tsum.insert(sum3);
        tsum.insert(sum4);
        if (sz(tsum) == 1){
            cout << *(tsum.begin());
            forn(i,n){
                cout << endl;
                forn(j,n)
                    cout << d[i][j] << ' ';
            }
            return 0;
        }
    }while (next_permutation(all(a)));
    return 0;
}

#include<bits/stdc++.h>
#define pb push_back
#define ll long long
#define max 1000000000
using namespace std;

map <ll, ll> mapa;
vector <ll> vec;
ll resp[1010][1010];
ll found;

bool isOK(ll val, ll l, ll c, ll goal, ll n){
	if(mapa[val]==0) return false;
	if(found) return false;
	ll counter = 0;
	//checando horizontal
	for(ll i=0;i<n;i++){
		counter += resp[l][i];
	}
	if(counter+val!=goal && c==n-1) return false;
	//checando vertical
	counter = 0;
	for(ll i=0;i<n;i++){
		counter += resp[i][c];
	}
	if(counter+val!=goal && l==n-1) return false;
	//diagonal principal
	bool fazParte = false;
	counter = 0;
	for(ll i=0, j=0;i<n && j<n;i++,j++){
		if(i==l && j==c) fazParte=1;
		counter += resp[i][j];
	}
	if(fazParte){
		if(counter+val!=goal&&l==n-1&&c==n-1) return false;
	}
	//diagonal secundaria
	counter = 0;
	fazParte = false;
	for(ll i=0, j=n-1;i<n && j>=0;i++,j--){
		if(i==l && j==c) fazParte=1;
		counter += resp[i][j];
	}
	if(fazParte){
		if(counter+val!=goal&&l==n-1&&c==0) return false;
	}
	return true; //ok
}

void backTrack(ll l,ll c, ll goal , ll n){
	if(l==n){
		found = 1;
		for(ll i=0;i<n;i++){
			for(ll j=0; j<n;j++){
				if(j==0)printf("%lld", resp[i][j]);
				else printf(" %lld", resp[i][j]);
			}
			printf("\n");
		}
		//printf("\n");
	}
	else if(c==n){
		backTrack(l+1,0,goal, n);
	}
	else{
		for(ll j=0;j<vec.size();j++){
			ll val = vec[j];
			if(isOK(val, l, c, goal,n)){
				resp[l][c] = val;
				mapa[val]--;
				backTrack(l, c+1, goal, n);
				resp[l][c] = 0;
				mapa[val]++;
			}

		}
	}
}

int main(){
	ll aux, soma=0, n;
	scanf("%lld", &n);
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			resp[i][j] = 0;
		}
	}
	ll tam = pow(n, 2);
	for(int i=0;i<tam;i++){
		scanf("%lld", &aux);
		vec.pb(aux);
		mapa[aux]++;
		soma+=aux;
	}
	found = 0;
	printf("%lld\n", soma/n);
	backTrack(0, 0,soma/n, n);
	resp[0][0]=1;
	resp[0][1]=0;
	resp[0][2]=0;
	resp[1][0]=0;
	resp[1][1]=0;
	resp[1][2]=0;
	resp[2][0]=0;
	resp[2][1]=0;
	resp[2][2]=0;
	//printf("%d\n",(isOK(0, 0, 1, 0, 3))?11:10);

}

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <bitset>
#include <cstring>
#include <string>
#include <queue>
#pragma comment (linker, "/STACK:256000000")

using namespace std;

const int maxN = 5;

int a[maxN][maxN];
int n;
pair < int, int > order[] = {
	make_pair(0, 0), // 0
	make_pair(0, 1), // 1
	make_pair(0, 2), // 2
	make_pair(0, 3), // 3
	make_pair(1, 0), // 4
	make_pair(2, 0), // 5
	make_pair(3, 0), // 6 no need to search
	make_pair(2, 1), // 7
	make_pair(1, 2), // 8 no need to search
	make_pair(1, 1), // 9
	make_pair(3, 1), // 10 no need to search
	make_pair(1, 3), // 11 no need to search
	make_pair(2, 2), // 12
	make_pair(3, 2), // 13 no need to search
	make_pair(3, 3), // 14 no need to search
	make_pair(2, 3)  // 15 no need to search
};

multiset < int > S;
int sum = 0;

void rec(int have) {
	if (have == n * n) {
		printf("%d\n", sum);
		for (int i = 0; i < n; ++i, printf("\n"))
			for (int j = 0; j < n; ++j)
				printf("%d ", a[i][j]);
		exit(0);
		return ;
	}
	int x = order[have].first, y = order[have].second;
	if (have < 4) {
		vector < int > order;
		for (multiset < int > :: iterator it = S.begin(); it != S.end(); ++it) {
			order.push_back(* it);
		}
		for (int i = 0; i < order.size(); ++i)
			if (i == 0 || order[i] > order[i - 1]) {
				a[x][y] = order[i];
				S.erase(S.find(order[i]));
				rec(have + 1);
				S.insert(order[i]);
			}
		return ;
	}

	if (have == 4) {
		sum = a[0][0] + a[0][1] + a[0][2] + a[0][3];
	}

	if (have == 6) {
		int t = sum - a[0][0] - a[1][0] - a[2][0];
		if (S.find(t) != S.end()) {
			S.erase(S.find(t));
			a[x][y] = t;
			rec(have + 1);
			S.insert(t);
		}
		return ;
	}
	if (have == 8) {
		int t = sum - a[3][0] - a[2][1] - a[0][3];
		if (S.find(t) != S.end()) {
			S.erase(S.find(t));
			a[x][y] = t;
			rec(have + 1);
			S.insert(t);
		}
		return ;
	}

	if (have == 10) {
		int t = sum - a[0][1] - a[1][1] - a[2][1];
		if (S.find(t) != S.end()) {
			S.erase(S.find(t));
			a[x][y] = t;
			rec(have + 1);
			S.insert(t);
		}
		return ;
	}

	if (have == 11) {
		int t = sum - a[1][0] - a[1][1] - a[1][2];
		if (S.find(t) != S.end()) {
			S.erase(S.find(t));
			a[x][y] = t;
			rec(have + 1);
			S.insert(t);
		}
		return ;
	}

	if (have == 13) {
		int t = sum - a[0][2] - a[1][2] - a[2][2];
		if (S.find(t) != S.end()) {
			S.erase(S.find(t));
			a[x][y] = t;
			rec(have + 1);
			S.insert(t);
		}
		return ;
	}
	if (have == 14) {
		int t = sum - a[3][0] - a[3][1] - a[3][2];
		int t2 = sum - a[0][0] - a[1][1] - a[2][2];
		if (t == t2 && S.find(t) != S.end()) {
			S.erase(S.find(t));
			a[x][y] = t;
			rec(have + 1);
			S.insert(t);
		}
		return ;
	}
	if (have == 15) {
		int t = sum - a[2][0] - a[2][1] - a[2][2];
		int t2 = sum - a[0][3] - a[1][3] - a[3][3];
		if (t == t2 && S.find(t) != S.end()) {
			S.erase(S.find(t));
			a[x][y] = t;
			rec(have + 1);
			S.insert(t);
		}
		return ;
	}

	vector < int > order;
	for (multiset < int > :: iterator it = S.begin(); it != S.end(); ++it) {
		order.push_back(* it);
	}
	for (int i = 0; i < order.size(); ++i)
		if (i == 0 || order[i] > order[i - 1]) {
			a[x][y] = order[i];
			S.erase(S.find(order[i]));
			rec(have + 1);
			S.insert(order[i]);
		}
	return ;
}

int b[maxN * maxN];

void solve_trivial() {
	sort(b, b + (n * n));
	do {
		for (int i = 0; i < n * n; ++i)
			a[i / n][i % n] = b[i];
		set < int > S;
		for (int i = 0; i < n; ++i) {
			int sum = 0;
			for (int j = 0; j < n; ++j)
				sum += a[i][j];
			S.insert(sum);
			sum = 0;
			for (int j = 0; j < n; ++j)
				sum += a[j][i];
			S.insert(sum);
		}
		int s1 = 0, s2 = 0;
		for (int i = 0; i < n; ++i)
			s1 += a[i][i], s2 += a[i][n - 1 - i];
		S.insert(s1);
		S.insert(s2);
		if (S.size() == 1) {
			printf("%d\n", * S.begin());
			for (int i = 0; i < n; ++i, printf("\n"))
				for (int j = 0; j < n; ++j)
					printf("%d ", a[i][j]);
			return ;
		}
	} while (next_permutation(b, b + n * n));
}

void solve_clever() {
	for (int i = 0; i < n * n; ++i)
		S.insert(b[i]);
	rec(0);
}

int main() {
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);

	scanf("%d", &n);
	for (int i = 0; i < n * n; ++i)
		scanf("%d", &b[i]);

	if (n <= 3) {
		solve_trivial();
	} else {
		solve_clever();
	}

	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define ll long long
#define maxn 4

ll n, N, sum, p[maxn * maxn];
bool ok(vector<ll> v) {
	sort(v.begin(), v.end());
	for (ll i = 0; i < N; i++)
		if (p[i] != v[i]) return false;
	return true;
}

ll cnt[maxn * maxn], ans[maxn][maxn], lef[maxn * maxn - maxn];
vector<pair<pair<ll, ll>, pair<ll, ll> > > v, vec;
set<pair<pair<ll, ll>, pair<ll, ll> > > st;
bool ok(ll a, ll b, ll c, ll d, bool flag) {
	vector<ll> v;
	v.resize(4);
	v[0] = a;
	v[1] = b;
	v[2] = c;
	v[3] = d;
	sort(v.begin(), v.end());
	pair<pair<ll, ll>, pair<ll, ll> > pa = mp(mp(v[0], v[1]), mp(v[2], v[3]));
	if (st.find(pa) == st.end()) {
		if (flag) {
			st.insert(pa);
		}
		return true;
	}
	return false;
}
bool ok(pair<pair<ll, ll>, pair<ll, ll> > tup) {
	if (cnt[tup.first.first] < 3) return false;
	if (cnt[tup.first.second] < 3) return false;
	if (cnt[tup.second.first] < 3) return false;
	if (cnt[tup.second.second] < 3) return false;
	return ok(p[tup.first.first], p[tup.first.second], p[tup.second.first], p[tup.second.second], true);
}
bool ok() {
	ll s = 0;
	for (ll r = 0; r < n; r++)
		s += ans[r][n - 1];
	if (s != sum) return false;
	s = 0;
	for (ll c = 0; c < n; c++)
		s += ans[n - 1][c];
	if (s != sum) return false;
	vector<ll> v;
	for (ll r = 0; r < n; r++)
		for (ll c = 0; c < n; c++)
			v.push_back(ans[r][c]);
	return ok(v);
}
bool cal(vector<ll> id) {
	multiset<ll> se = multiset<ll>(p, p + N);
	for (ll i = 0; i < n; i++) {
		ans[i][i] = p[id[i]];
		se.erase(se.find(ans[i][i]));
	}
	ll N = 0;
	for (multiset<ll>::iterator it = se.begin(); it != se.end(); it++)
		lef[N++] = *it;
	for (ll i = 0; i < N; i++) {
		for (ll k = 0; k < N; k++) {
			if (k == i) continue;
			for (ll h = 0; h < N; h++) {
				if (h == k || h == i) continue;
				for (ll t = 0; t < N; t++) {
					if (t == h || t == k || t == i) continue;
					ans[0][3] = lef[i];
					ans[1][2] = lef[k];
					ans[2][1] = sum - (ans[1][1] + ans[1][2] + ans[2][2]);
					ans[3][0] = sum - (ans[0][3] + ans[1][2] + ans[2][1]);
					if (ok(ans[0][3], ans[1][2], ans[2][1], ans[3][0], false)) continue;
					ans[0][1] = lef[h];
					ans[0][2] = sum - (ans[0][0] + ans[0][1] + ans[0][3]);
					ans[3][1] = sum - (ans[0][1] + ans[1][1] + ans[2][1]);
					ans[3][2] = sum - (ans[0][2] + ans[1][2] + ans[2][2]);
					ans[1][0] = lef[t];
					ans[2][0] = sum - (ans[0][0] + ans[1][0] + ans[3][0]);
					ans[1][3] = sum - (ans[1][0] + ans[1][1] + ans[1][2]);
					ans[2][3] = sum - (ans[2][0] + ans[2][1] + ans[2][2]);
					if (ok()) {
						printf("%lld", sum);
						for (ll r = 0; r < n; r++) {
							printf("\n");
							for (ll c = 0; c < n; c++) {
								if (c) printf(" ");
								printf("%lld", ans[r][c]);
							}
						}
						return true;
					}

				}
			}
		}
	}
	return false;
}

int main() {
	ll i, k, h, t, r, c;
	vector<vector<ll> > a;
	vector<ll> b, ret, id;
	scanf("%lld", &n);
	if (n <= 2) {
		scanf("%lld", &i);
		if (n == 1) printf("%lld\n%lld", i, i);
		else printf("%lld\n%lld %lld\n%lld %lld", (i << 1), i, i, i, i);
		return 0;
	}
	N = n * n;
	for (i = 0; i < N; i++) {
		scanf("%lld", p + i);
		sum += p[i];
	}
	sort(p, p + N);
	sum /= n;
	if (n == 3) {
		for (i = 0; i < N; i++) {
			for (k = 0; k < N; k++) {
				if (k == i) continue;
				for (h = 0; h < N; h++) {
					if (h == k || h == i) continue;
					ans[0][0] = p[i];
					ans[1][1] = p[k];
					ans[0][2] = p[h];
					ans[2][2] = sum - (ans[0][0] + ans[1][1]);
					ans[2][0] = sum - (ans[0][2] + ans[1][1]);
					ans[0][1] = sum - (ans[0][0] + ans[0][2]);
					ans[1][0] = sum - (ans[0][0] + ans[2][0]);
					ans[1][2] = sum - (ans[1][0] + ans[1][1]);
					ans[2][1] = sum - (ans[0][1] + ans[1][1]);
					if (ok()) {
						printf("%lld", sum);
						for (ll r = 0; r < n; r++) {
							printf("\n");
							for (ll c = 0; c < n; c++) {
								if (c) printf(" ");
								printf("%lld", ans[r][c]);
							}
						}
						return 0;
					}
				}
			}
		}
	}
	for (i = 0; i < N; i++)
		for (k = i + 1; k < N; k++)
			for (h = k + 1; h < N; h++)
				for (t = h + 1; t < N; t++) {
					if (p[i] + p[k] + p[h] + p[t] != sum) continue;
					cnt[i]++;
					cnt[k]++;
					cnt[h]++;
					cnt[t]++;
					v.push_back(mp(mp(i, k), mp(h, t)));
				}
	id.resize(n);
	for (i = 0; i < v.size(); i++) {
		if (!ok(v[i])) continue;
		vec.push_back(v[i]);
	}
	for (i = 0; i < vec.size(); i++) {
		id[0] = vec[i].first.first;
		id[1] = vec[i].first.second;
		id[2] = vec[i].second.first;
		id[3] = vec[i].second.second;
		if (cal(id)) break;
		swap(id[1], id[3]);
		if (cal(id)) break;
		swap(id[2], id[3]);
		if (cal(id)) break;
	}
	return 0;
}

#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <memory.h>
#include <map>
#include <queue>
#include <algorithm>
#include <set>

using namespace std;

int a[25],b[25],aa[25],c[25];
int cur[6][6];
int perm[25];
int sum;
bool flag;
set <int> my;

void calc()
{
    for (int i = 1; i<=9; i++)
        b[perm[i-1]]=a[i];
    if (b[1]+b[2]+b[3]!=sum) return;
    if (b[4]+b[5]+b[6]!=sum) return;
    if (b[7]+b[8]+b[9]!=sum) return;
    if (b[1]+b[4]+b[7]!=sum) return;
    if (b[2]+b[5]+b[8]!=sum) return;
    if (b[3]+b[6]+b[9]!=sum) return;
    if (b[1]+b[5]+b[9]!=sum) return;
    if (b[3]+b[5]+b[7]!=sum) return;
    cout << sum << endl;
    cout << b[1] << " " << b[2] << " " << b[3] << endl;
    cout << b[4] << " " << b[5] << " " << b[6] << endl;
    cout << b[7] << " " << b[8] << " " << b[9] << endl;
    flag=true;

}

void solve3()
    {

      for (int i = 0; i<9; i++) perm[i]=i+1;
      for (int iter = 1; iter <= 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9; iter++)
      {
          calc();
          if (flag) return;
          next_permutation(perm,perm+9);
     }
    }

bool good()
    {
        //cout << 1 << endl;
       // return false;
        for (int i = 1; i<=4; i++)
        {
            if (cur[i][1]+cur[i][2]+cur[i][3]+cur[i][4]!=sum) return false;
            if (cur[1][i]+cur[2][i]+cur[3][i]+cur[4][i]!=sum) return false;
        }
      if (cur[1][1]+cur[2][2]+cur[3][3]+cur[4][4]!=sum) return false;
      if (cur[1][4]+cur[2][3]+cur[3][2]+cur[4][1]!=sum) return false;
      int cc = 0;
      for (int i = 1; i<=4; i++)
        for (int j = 1; j<=4; j++)
        {
            cc++;
            c[cc-1]=cur[i][j];
        }
       sort(c,c+cc);
       for (int i = 1; i<=16; i++)
        if (aa[i]!=c[i-1]) return false;
       cout << sum << endl;
       for (int i = 1; i<=4; i++)
       {
           for (int j = 1; j<=4; j++)
             cout << cur[i][j] << " ";
            cout << endl;
       }
      return true;
    }

int main()
            {
                //freopen("input.txt","r",stdin);
                //freopen("output.txt","w",stdout);
                int n;
                cin >> n;
                for (int i = 1; i<=n*n; i++) cin >> a[i];
                for (int i = 1; i<=n*n; i++) swap(a[i],a[i/2+1]);
                for (int i = 1; i<=n*n; i++) aa[i]=a[i];
                for (int i = 1; i<=n*n; i++)
                     for (int j = 1; j<=n*n-1; j++)
                      if (aa[j]>aa[j+1]) swap(aa[j],aa[j+1]);
                my.clear();
                for (int i = 1; i<=n*n; i++) my.insert(a[i]);
                if (n==1)
                {
                    cout << a[1] << endl;
                    cout << a[1] << endl;
                    return 0;
                }
                bool used = true;
                for (int i = 2; i<=n*n; i++)
                {
                    if (a[i]!=a[1]) used=false;
                }
                if (used)
                {
                    cout << n * a[1] << endl;
                    for (int i = 1; i<=n; i++)
                    {
                        for (int j = 1; j<=n; j++)
                            cout << a[1] << " ";
                        cout << endl;

                    }
                    return 0;
                }
               sum = 0;
               for (int i = 1; i<=n*n; i++) sum+=a[i];
               sum/=n;
               flag=false;
               if (n==3)
               {
                  solve3();
                  return 0;
               }
               for (int i11 = 1; i11<=16; i11++)
                for (int i12 = 1; i12<=16; i12++)
                {
                  if (i11==i12) continue;
                  for (int i13 = 1; i13<=16; i13++)
                  {
                      if (i13==i11 || i13==i12) continue;
                      if (my.find(sum-a[i11]-a[i12]-a[13])==my.end()) continue;
                      for (int i21 = 1; i21<=16; i21++)
                      {
                          if (i21==i11 || i21==i12 || i21==i13) continue;
                          for (int i31 = 1; i31<=16; i31++)
                          {
                              if (i31==i21 || i31==i11 || i31==i12 || i31==i13) continue;
                              if (my.find(sum-a[i11]-a[i21]-a[i31])==my.end()) continue;
                               for (int i22 = 1; i22<=16; i22++)
                               {
                                   if (i22==i11 || i22==i12|| i22==i13 || i22==i21 || i22==i31) continue;
                                   for (int i33 = 1; i33<=16; i33++)
                                   {
                                       if (i33==i11 || i33==i22 || i33==i12 || i33==i13 || i33 == i21 || i33 == i31) continue;
                                       if (my.find(sum-a[i11]-a[i22]-a[i33])==my.end()) continue;
                                       for (int i23 = 1; i23<=16; i23++)
                                       {
                                           if (i23==i11 || i23==i21 || i23==i31 || i23==i12 || i23==i22 || i23 == i13 || i23 == i33) continue;
                                           if (my.find(sum-a[i21]-a[i22]-a[i23])==my.end()) continue;
                                           if (my.find(sum-a[i13]-a[i23]-a[i33])==my.end()) continue;
                                           for (int i34 = 1; i34<=16; i34++)
                                           {
                                               if (i34==i11 || i34==i12 || i34==i13 || i34 == i21 || i34 == i22 || i34==i23 || i34==i31 || i34== i33) continue;
                                               if (my.find(sum-a[i31]-a[i33]-a[i34])==my.end()) continue;
                                               int column1 = sum - a[i11]-a[i21]-a[i31];
                                               if (my.find(column1)==my.end()) continue;
                                               int column3 = sum - a[i13]-a[i23]-a[i33];
                                               if (my.find(column3)==my.end()) continue;
                                               int row1 = sum - a[i11] - a[i12] - a[i13];
                                               if (my.find(row1)==my.end()) continue;
                                               int row2 = sum - a[i21] - a[i22] - a[i23];
                                               if (my.find(row2)==my.end()) continue;
                                               int row3 = sum - a[i31] - a[i33] - a[i34];
                                               if (my.find(row3)==my.end()) continue;
                                               int column2 = sum - a[i12]-a[i22]-row3;
                                               if (my.find(column2)==my.end()) continue;
                                               int row4 = sum - column1 - column2 - column3;
                                               if (my.find(row4)==my.end()) continue;
                                               //cout << 1 << endl; return 0;
                                               cur[1][1]=a[i11];
                                               cur[1][2]=a[i12];
                                               cur[1][3]=a[i13];
                                               cur[1][4]=sum-cur[1][1]-cur[1][2]-cur[1][3];
                                               cur[2][1]=a[i21];
                                               cur[2][2]=a[i22];
                                               cur[2][3]=a[i23];
                                               cur[2][4]=sum-cur[2][1]-cur[2][2]-cur[2][3];
                                               cur[3][1]=a[i31];
                                               cur[3][3]=a[i33];
                                               cur[3][4]=a[i34];
                                               cur[3][2]=sum-cur[3][1]-cur[3][3]-cur[3][4];
                                               cur[4][1]=sum-cur[1][1]-cur[2][1]-cur[3][1];
                                               cur[4][2]=sum-cur[1][2]-cur[2][2]-cur[3][2];
                                               cur[4][3]=sum-cur[1][3]-cur[2][3]-cur[3][3];
                                               cur[4][4]=sum-cur[1][4]-cur[2][4]-cur[3][4];
                                               flag = false;
                                               if (good()) flag=true;
                                               if (flag) return 0;
                                           }
                                       }
                                   }
                               }
                          }
                      }
                 }
                }
               return 0;
            }

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cmath>
#include <memory.h>
#include <string>
#include <set>
#include <map>
#include <queue>
using namespace std;
#define forn(i,n) for(int i=0;i<(n);++i)
#define forv(i,v) forn(i,(int)(v).size())
#define iinf 1000000000
#define linf 1000000000000000000LL
#define dinf 1e200
#define all(v) (v).begin(),(v).end()
#define pb push_back
#define mp make_pair
#define lng long long
#define eps 1e-11
#define EQ(a,b) (fabs((a)-(b))<eps)
#define SQ(a) ((a)*(a))
#define PI 3.14159265359
#define index asdindex
#define FI first
#define SE second
#define prev asdprev
#define ends asdends
#define PII pair<int,int>
#define X first
#define Y second
#define mset(a,b) memset(a,b,sizeof(a))

int n;
int src[16];
int s;

int f(){
    int res=0;
    forn(i, n){
        int sum = 0;
        for(int j=i*n;j<(i+1)*n;++j){
            sum += src[j];
        }
        res+=abs(sum-s);
    }
    forn(i, n){
        int sum = 0;
        for(int j=i;j<n*n;j+=n){
            sum += src[j];
        }
        res+=abs(sum-s);
    }
    {
        int sum = 0;
        for(int i=0;i<n*n;i+=n+1)sum += src[i];
        res+=abs(sum-s);
    }
    int sum = 0;
    int j=0;
    for(int i=n-1;i<n*n;i+=n-1,++j){
        if (j==n)break;
        sum += src[i];
    }
    res+=abs(sum-s);
    return res;
}

void print(){
    cout << s << endl;
    forn(i,n*n){
        cout<<src[i]<<" ";
        if (i%n==n-1)cout<<endl;
    }
    exit(0);
}

int main(){
#ifdef __ASD__
    freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#endif
    cin >> n;
    s=0;
    forn(i,n*n){
        cin>>src[i];
        s+=src[i];
    }
    s=s/n;
    for(;;){
        random_shuffle(src, src+n*n);
        forn(i, 2){
            int q = f();
            if (q==0)
                print();
            forn(i1, n*n){
                for(int i2=i1+1;i2<n*n;++i2){
                    swap(src[i1], src[i2]);
                    int new_q = f();
                    if (new_q>=q){
                        swap(src[i1], src[i2]);
                    }else{
                        q = new_q;
                    }
                }
            }
l1:;
        }
    }

    return 0;
}

#include <bits/stdc++.h>

using namespace std;

mt19937 mt_rand(chrono::system_clock::now().time_since_epoch().count());

const int N = 22;

int n, tot;
int target;
int id[N];
int a[N];
int b[N][N];

inline int eval() {
  for (int i = 0; i < tot; i++) {
    b[i / n][i % n] = a[id[i]];
  }
  int ret = 0;
  for (int i = 0; i < n; i++) {
    int sum = 0;
    for (int j = 0; j < n; j++) {
      sum += b[i][j];
    }
    ret = max(ret, abs(sum - target));
    sum = 0;
    for (int j = 0; j < n; j++) {
      sum += b[j][i];
    }
    ret = max(ret, abs(sum - target));
  }
  int sum = 0;
  for (int i = 0; i < n; i++) {
    sum += b[i][i];
  }
  ret = max(ret, abs(sum - target));
  sum = 0;
  for (int i = 0; i < n; i++) {
    sum += b[i][n - i - 1];
  }
  ret = max(ret, abs(sum - target));
  return ret;
}

int main() {
  //freopen("input.txt", "r", stdin);
  //freopen("output.txt", "w", stdout);
  scanf("%d", &n);
  tot = n * n;
  for (int i = 0; i < tot; i++) {
    scanf("%d", a + i);
    id[i] = i;
    target += a[i];
  }
  target /= n;
  while (clock() < 1.9 * CLOCKS_PER_SEC) {
    random_shuffle(id, id + tot);
    for (int iter = 0; iter < 1111; iter++) {
      int x = mt_rand() % tot, y = mt_rand() % tot;
      int h = eval();
      swap(id[x], id[y]);
      int hh = eval();
      if (hh > h) {
        swap(id[x], id[y]);
      } else {
        h = hh;
      }
      if (h == 0) {
        goto stop;
      }
    }
  }
  stop:
  for (int i = 0; i < tot; i++) {
    b[i / n][i % n] = a[id[i]];
  }
  printf("%d\n", target);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      printf("%d ", b[i][j]);
    }
    printf("\n");
  }
  return 0;
}

#include <iostream>
#include <fstream>
#include <algorithm>
#include <locale>
#include <complex>
using namespace std;
const double maxTemp = 400000000, minTemp = 0.000001, dropRate = 0.000008;
int n, a[19], s, sumRow[9], sumCol[9], sumDia[2];

int Energy(int a[19])
{
    fill(sumRow, sumRow + n, 0);
    fill(sumCol, sumCol + n, 0);
    fill(sumDia, sumDia + 2, 0);
    for(int i = 0; i < n * n; i++)
    {
        sumRow[i / n] += a[i];
        sumCol[i % n] += a[i];
        if(i / n == i % n)
        {
            sumDia[0] += a[i];
        }
        if(i / n + i % n == n - 1)
        {
            sumDia[1] += a[i];
        }
    }
    int res = abs(sumDia[0] - s) + abs(sumDia[1] - s);
    for(int i = 0; i < n; i++)
    {
        res += abs(sumRow[i] - s) + abs(sumCol[i] - s);
    }
    return res;
}

int Delta(int a[19], int x, int y)
{
    int e = Energy(a);
    swap(a[x], a[y]);
    int ee = Energy(a);
    swap(a[x], a[y]);
    return ee - e;
}

double Prob(int delta, double temp)
{
    if(delta < 0)
    {
        return 1;
    }
    return exp(-delta / temp);
}

int main()
{
    //ifstream cin("d1.inp");
    //ofstream cout("d1.out");
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cin >> n;
    for(int i = 0; i < n * n; i++)
    {
        cin >> a[i];
    }
    s = 0;
    for(int i = 0; i < n * n; i++)
    {
        s += a[i];
    }
    s /= n;
    srand(time(NULL));
    random_shuffle(a, a + n * n);
    for(double temp = maxTemp; temp >= minTemp; temp = temp * (1 - dropRate))
    {
        if(Energy(a) == 0)
        {
            break;
        }
        int x = rand() % (n * n), y = rand() % (n * n);
        if((double)rand() / RAND_MAX <= Prob(Delta(a, x, y), temp))
        {
            swap(a[x], a[y]);
        }
    }
    cout << s << "\n";
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n; j++)
        {
            cout << a[i * n + j] << " ";
        }
        cout << "\n";
    }
    return 0;
}

#include<iostream>
#include<algorithm>
#include<memory.h>
using namespace std;
int n, sum, a[4][4], in[16];
int t[8], C, r[4], s[8], f[8], m[5];
bool check(){
	for (int i = 0;i < n;++i){
		int p = 0, q = 0;
		for (int j = 0;j < n;++j){
			p += a[i][j];
			q += a[j][i];
		}
		if (p != sum || q != sum) return false;
	}
	int p = 0, q = 0;
	for (int i = 0;i < n;++i){
		p += a[i][i];
		q += a[i][n - i - 1];
	}
	if (p != sum || q != sum) return false;
	return true;
}
void print(){
	if (check()){
		cout << sum << endl;
		for (int i = 0;i < n;++i){
			for (int j = 0;j < n;++j)
				cout << a[i][j] << (j != n - 1 ? " " : "");
			cout << endl;
		}
	}
	else cout << -1 << endl;
}
bool zero(int mask, int plc){
	return ((mask >> plc) & 1) == 0;
}
void find_ans(int key, int &mask){
	for (int i = 0;i < 16;++i)
		if (zero(mask, i) && in[i] == key){
			mask |= (1 << i);
			return;
		}
	mask = (1 << 17) - 1;
}
int main()
{
	cin >> n;
	for (int i = 0;i < n * n;++i){
		cin >> in[i];
		sum += in[i];
	}
	if (n == 1) cout << in[0] << endl << in[0] << endl;
	else {
		if (sum % n != 0) cout << -1 << endl;
		else if (n != 4){
			sum /= n;
			int id[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
			do{
				for (int i = 0;i < n;++i) for (int j = 0;j < n;++j) a[i][j] = in[id[i * n + j]];
				if (check()) break;
			}
			while(next_permutation(id, id + n * n));
			print();
		}
		else {
			sum /= n;
			bool E = false;
			for(int i=0;i<16&&!E;++i)for(int j=i+1;j<16&&!E;++j)for(int k=0;k<16&&!E;++k)for(int l=k+1;l<16&&!E;++l)if(k!=i&&k!=j&&l!=i&&l!=j){
				s[0] = in[i], s[1] = in[j], s[2] = in[k], s[3] = in[l];
				m[0] = (1 << i) + (1 << j) + (1 << k) + (1 << l);
				bool ans = false;
				for (int i0 = 0;i0 < 16 && !ans;++i0) if (zero(m[0], i0)){
					s[4] = in[i0];
					m[1] = m[0] + (1 << i0);
					find_ans(f[0] = sum - s[0] - s[4] - s[1], m[1]);
					for (int i1 = 0;i1 < 16 && !ans;++i1) if (zero(m[1], i1)){
						s[5] = in[i1];
						m[2] = m[1] + (1 << i1);
						find_ans(f[1] = sum - s[5] - s[2] - s[3], m[2]);
						for (int i2 = 0;i2 < 16 && !ans;++i2) if (zero(m[2], i2)){
							s[6] = in[i2];
							m[3] = m[2] + (1 << i2);
							find_ans(f[2] = sum - s[4] - s[6] - f[1], m[3]);
							find_ans(f[3] = sum - s[1] - s[3] - f[2], m[3]);
							find_ans(f[4] = sum - s[5] - f[0] - f[3], m[3]);
							if (f[4] != sum - s[0] - s[6] - s[2]) continue;
							for (int i3 = 0;i3 < 16 && !ans;++i3) if (zero(m[3], i3)){
								s[7] = in[i3];
								m[4] = m[3] + (1 << i3);
								find_ans(f[5] = sum - s[4] - s[5] - s[7], m[4]);
								find_ans(f[6] = sum - s[1] - s[2] - f[5], m[4]);
								find_ans(f[7] = sum - f[0] - f[1] - f[6], m[4]);

								if (f[7] != sum - s[0] - s[3] - s[7]) continue;

								if (m[4] == (1 << 17) - 1) continue;
								ans = true;
								//cout << "here " << mask << endl;
								a[0][0] = s[0], a[0][1] = s[6], a[0][2] = f[4], a[0][3] = s[2];
								a[1][0] = s[7], a[1][1] = s[4], a[1][2] = s[5], a[1][3] = f[5];
								a[2][0] = f[7], a[2][1] = f[1], a[2][2] = f[0], a[2][3] = f[6];
								a[3][0] = s[3], a[3][1] = f[2], a[3][2] = f[3], a[3][3] = s[1];

							}
						}
					}
				}
				if (ans == true) E = true;
			}
			print();
		}
	}
	return 0;
}

/*
	4
	7  12 1  14
	2  13 8  11
	16 3  10 5
	9  6  15 4
*/

#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <string.h>
#include <math.h>
#include <fstream>
using namespace std;
#define fon(i,n) for(i=0;i<n;++i)
#define forr(i,n) for(i=n-1;i>=0;--i)
#define re return
#define ll long long
const double EPS = 1E-9;
const int INF = 1000000000;
const ll INF64 = (ll)1E18;
const double PI = 3.1415926535897932384626433832795;

ll n,m,h;

typedef map<ll,ll>tmap;
typedef set<ll>tset;
set<ll>::iterator it,itt,itlow,itup;
set<ll>::reverse_iterator irev;

tmap ids;

int main()
{
	#ifdef _DINARISIO
	ifstream cin("input.txt");
	ofstream cout("output.txt");
	#endif

	ll i,n,n2,k,j,a[20];
	ll s=0,s2,s1;

	cin>>n;n2=n*n;
	fon(i,n2){cin>>a[i];s+=a[i];}
	s/=n;
	sort(a,a+n2);
	do{
		/*if(a[0]==1&&a[1]==0&&a[2]==-1&&
			a[3]==-2&&a[4]==0&&a[5]==2&&
			a[6]==1&&a[7]==0&&a[8]==-1)
		{
			s1=9;
		}*/
		fon(j,n){
			s2=0;
			s1=0;
			fon(k,n)s2+=a[j*n+k];
			fon(k,n)s1+=a[k*n+j];
			if(s2!=s)j=n+10;
			if(s1!=s)j=n+10;
		}if(s2==s&&s1==s){
			s2=0;fon(j,n)
				s2+=a[j*n+j];
			s1=0;fon(j,n)
				s1+=a[(n-j-1)*n+j];
			if(s2==s&&s1==s2)
			{
				cout<<s<<endl;
				fon(j,n){
					fon(k,n)cout<<a[j*n+k]<<" ";
					cout<<endl;
				}
				re 0;
			}
		}
	}while ( next_permutation (a,a+n2) );
	re 0;
}

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;

long long S;
int n;
long long a[32];

int mx;

int us[32];

int pr = 0;

long long t[8][8];

bool cmag()
{
	int i, j, k, s;
			s = 0;
			for ( i = 0; i < n; i++ )
				s = s + t[0][i];

			for ( i = 0; i < n; i++ )
			{
				k = 0;
				for ( j = 0; j < n; j++ )
					k = k + t[i][j];

				if ( k != s ) return 0;

				k = 0;
				for ( j = 0; j < n; j++ )
				{
					k = k + t[j][i];
				}
				if ( k != s ) return 0;
			}

	k = 0;
	for ( i = 0; i < n; i++ )
	{
		k = k + t[i][i];
	}
	if ( k != s ) return 0;

	 k = 0;
	for ( i = 0; i < n; i++ )
	{
		k = k + t[i][n-i-1];
	}
	if ( k != s ) return 0;

	return 1;
}

void pf()
{
	cout << "FR: ";
	for ( int i = 0; i < n*n; i++ )
		if ( !us[i] )
			cout << a[i] << " ";
	cout << endl;
}

inline int mark( long long val )
{
	for ( int i = 0; i < n*n; i++ )
	{
		if ( ( a[i] == val ) && ( us[i] == 0 ) )
		{
			us[i] = 1;
			return i;
		}
	}
	return -1;
}

inline void unmark( long long val )
{
	int i;
	for ( i = 0; i < n*n; i++ )
	{
		if ( ( a[i] == val ) && ( us[i] == 1 ) )
		{
			us[i] = 0;
			break;
		}
	}
//	if ( i >= n*n ) while ( 1 ); // ne vliza tuk.
}

bool make4( int st )
{
	if ( st > mx ) mx = st;
	int x;
	if ( st > 7 )
	{
		return cmag();
	}
	if ( st <= 1 )
	{
		for ( int i = 0; i < n*n; i++ )
		{
			if ( us[i] == 0 )
			{
				us[i] = 1;
				t[0][st] = a[i];
					if ( make4( st+1 ) )
						return 1;
				us[i] = 0;
			}
		}
		return 0;
	}
	else if ( st == 2 )
	{
		for ( int i = 0; i < n*n; i++ )
		{
			if ( us[i] == 0 )
			{
				us[i] = 1;
				t[0][2] = a[i];
					t[0][3] = S - t[0][0] - t[0][1] - t[0][2];
					x = mark( t[0][3] );
					if ( x != -1 )
					{
						if ( make4( st+1 ) ) return 1;
						us[x] = 0;
					}
				us[i] = 0;
			}
		}
		return 0;
	}
	else if ( st == 3 )
	{
		for ( int i = 0; i < n*n; i++ )
		{
			if ( us[i] == 0 )
			{
				us[i] = 1;
				t[1][0] = a[i];
				if ( make4( st+1 ) ) return 1;
				us[i] = 0;
			}
		}
		return 0;
	}
	else if ( st == 4 )
	{
		for ( int i = 0; i < n*n; i++ )
		{
			if ( us[i] == 0 )
			{
				us[i] = 1;
				t[2][0] = a[i];
				t[3][0] = S - t[0][0] - t[1][0] - t[2][0];
				x = mark( t[3][0] );
				if ( x != -1 )
				{
					if ( make4( st+1 ) ) return 1;
					us[x] = 0;
				}
				us[i] = 0;
			}
		}
		return 0;
	}
	else if ( st == 5 )
	{
		for ( int i = 0; i < n*n; i++ )
		{
			if ( us[i] == 0 )
			{
				us[i] = 1;
				t[1][2] = a[i];
				t[2][1] = S - t[3][0] - t[1][2] - t[0][3];
				x = mark( t[2][1] );
				if ( x != -1 )
				{
					if ( make4( st+1 ) )
						return 1;
					us[x] = 0;
				}
				us[i] = 0;
			}
		}
		return 0;
	}
	else if ( st == 6 )
	{
		for ( int i = 0; i < n*n; i++ )
		{
			if ( us[i] == 0 )
			{
				us[i] = 1;
				t[2][2] = a[i];
				t[2][3] = S - t[2][0] - t[2][1] - t[2][2];
				t[3][2] = S - t[0][2] - t[1][2] - t[2][2];
				int y;
				x = mark( t[3][2] );
				if ( x != -1 )
				{
					y = mark( t[2][3] );
					if ( y != -1 )
					{
						if ( make4( st+1 ) )
							return 1;
						us[y] = 0;
					}
					us[x] = 0;
				}
				us[i] = 0;
			}
		}
		return 0;
	}
	else if ( st == 7 )
	{
		for ( int i = 0; i < n*n; i++ )
		{
			if ( us[i] == 0 )
			{
				us[i] = 1;
				t[1][1] = a[i];
				t[1][3] = S - t[1][0] - t[1][1] - t[1][2];
				t[3][1] = S - t[0][1] - t[1][1] - t[2][1];
				t[3][3] = S - t[0][0] - t[1][1] - t[2][2];

				x = mark( t[3][1] );
				if ( x != -1 )
				{
					int y = mark( t[1][3] );
					if ( y != -1 )
					{
						int z = mark( t[3][3] );
						if ( z != -1 )
						{
							if ( make4( st+1 ) )
								return 1;
							us[z] = 0;
						}
						us[y] = 0;
					}
					us[x] = 0;
				}
				us[i] = 0;
			}
		}
		return 0;
	}
	return 0;
}

int main()
{
	memset( t, 0, sizeof( t ) );
	int i, j;
	long long s;

	cin >> n;
	S = 0;
	for ( i = 0; i < n*n; i++ )
	{
		cin >> a[i];
		S = S + a[i];
	}

	S = S / n;

	memset( us, 0, sizeof( us ) );

	mx = -1;
	if ( n <= 3 )
	{
		sort( a, a + n*n );
		do
		{
			for ( i = 0; i < n; i++ )
			{
				for ( j = 0; j < n; j++ )
				{
					t[i][j] = a[ n*i + j ];
				}
			}

			if ( cmag() ) break;
		}
		while ( next_permutation( a, a + n*n ) );
	}
	else
	{
		make4( 0 );
	}

			s = 0;
			for ( i = 0; i < n; i++ )
				s = s + t[i][3-i];

/*				if ( t[1][2] + t[2][1] + t[3][0] + t[0][3] != S && n == 4 )
				{
					if ( mx == 6 )
						while ( 1 );
				}*/

	/*if ( ( S != s ) && ( n == 4 ) )
	{
		while ( 1 ); // vliza tuka -> ne e qko!
	}*/

	s = S;
	cout << s << endl;
	for ( i = 0; i < n; i++ )
	{
		cout << t[i][0];
		for ( j = 1; j < n; j++ )
			cout << " " << t[i][j];
		printf( "\n" );
	}
	return 0;
}

#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <ctime>
#include <cmath>
#include <complex>
#include <numeric>
#include <vector>
#include <string>
#include <queue>
#include <map>
#include <set>

using namespace std;

#define all(a) (a).begin(), (a).end()
#define sz(a) int((a).size())
#define FOR(i, a, b) for(int i(a), _b(b); i < _b; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORD(i, a, b) for(int i(a), _b(b); i >= _b; --i)
#define CL(a, v) memset(a, v, sizeof a)
#define pb push_back
#define X first
#define Y second

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;

const int INF = 1000000000;
const ll INF_LL = 1000000000000000000LL;

const int h = 4;

int n, m, s;
int a[h*h];

bool ok(int i0, int i1) {
  return a[i0] + a[i1] == s;
}

bool ok(int i0, int i1, int i2) {
  return a[i0] + a[i1] + a[i2] == s;
}

int r[h][h];

void go7(int u) {
  int l = -INF;
  int b[3], f[3];
  REP(i, m) if(a[i]!=l && (u&1<<i)) {
    r[2][2] = a[i];
    int v = u - (1<<i);
    f[0] = r[2][3] = s - r[2][0] - r[2][1] - r[2][2];
    f[1] = r[3][2] = s - r[0][2] - r[1][2] - r[2][2];
    f[2] = r[3][3] = s - r[0][0] - r[1][1] - r[2][2];
    int k = 0;
    REP(j, m) if(v&1<<j) b[k++] = a[j];
    sort(b, b+3);
    sort(f, f+3);
    if(b[0]==f[0] && b[1]==f[1] && b[2]==f[2]) {
      REP(ii, 4) {
        REP(jj, 4) printf("%d ", r[ii][jj]);
        printf("\n");
      }
      exit(0);
    }
    l = a[i];
  }
}

void go6(int u) {
  int l = -INF;
  REP(i, m) if(a[i]!=l && (u&1<<i)) {
    r[1][1] = a[i];
    int v = u - (1<<i);
    int f = r[1][3] = s - r[1][0] - r[1][1] - r[1][2];
    int j = 0;
    while(j<m && a[j]<f) ++j;
    while(j<m && (~v&1<<j)) ++j;
    if(j<m && a[j]==f) {
      v -= 1<<j;
      f = r[3][1] = s - r[0][1] - r[1][1] - r[2][1];
      j = 0;
      while(j<m && a[j]<f) ++j;
      while(j<m && (~v&1<<j)) ++j;
      if(j<m && a[j]==f) go7(v - (1<<j));
    }
    l = a[i];
  }
}

void go5(int u) {
  int l = -INF;
  REP(i, m) if(a[i]!=l && (u&1<<i)) {
    r[1][2] = a[i];
    int v = u - (1<<i);
    int f = r[2][1] = s - r[0][3] - r[1][2] - r[3][0];
    int j = 0;
    while(j<m && a[j]<f) ++j;
    while(j<m && (~v&1<<j)) ++j;
    if(j<m && a[j]==f) go6(v - (1<<j));
    l = a[i];
  }
}

void go4(int u) {
  int l = -INF;
  REP(i, m) if(a[i]!=l && (u&1<<i)) {
    r[2][0] = a[i];
    int v = u - (1<<i);
    int f = r[3][0] = s - r[0][0] - r[1][0] - r[2][0];
    int j = 0;
    while(j<m && a[j]<f) ++j;
    while(j<m && (~v&1<<j)) ++j;
    if(j<m && a[j]==f) go5(v - (1<<j));
    l = a[i];
  }
}

void go3(int u) {
  int l = -INF;
  REP(i, m) if(a[i]!=l && (u&1<<i)) {
    r[1][0] = a[i];
    go4(u - (1<<i));
    l = a[i];
  }
}

void go2(int u) {
  int l = -INF;
  REP(i, m) if(a[i]!=l && (u&1<<i)) {
    r[0][2] = a[i];
    int v = u - (1<<i);
    int f = r[0][3] = s - r[0][0] - r[0][1] - r[0][2];
    int j = 0;
    while(j<m && a[j]<f) ++j;
    while(j<m && (~v&1<<j)) ++j;
    if(j<m && a[j]==f) go3(v - (1<<j));
    l = a[i];
  }
}

void go1(int u) {
  int l = -INF;
  REP(i, m) if(a[i]!=l && (u&1<<i)) {
    r[0][1] = a[i];
    go2(u - (1<<i));
    l = a[i];
  }
}

void go0(int u) {
  int l = -INF;
  REP(i, m) if(a[i]!=l && (u&1<<i)) {
    r[0][0] = a[i];
    go1(u - (1<<i));
    l = a[i];
  }
}

int main() {
#ifdef LocalHost
  freopen("input.txt", "r", stdin);
  //freopen("output.txt", "w", stdout);
#endif

  //printf("%I64d\n\n", ll(16)*15*14 * 12*11 * 9 * 7 * 4);

  scanf("%d", &n);
  m = n*n;
  REP(i, m) {
    scanf("%d", a+i);
    s += a[i];
  }
  s /= n;
  printf("%d\n", s);

  sort(a, a+m);

  if(n==1) {
    printf("%d\n", a[0]);
  }
  if(n==2) {
    do {
      if(ok(0, 1) && ok(0, 2) && ok(0, 3) && ok(1, 2)) {
        REP(i, 2) {
          REP(j, 2) printf("%d ", a[i*2+j]);
          printf("\n");
        }
        break;
      }
    } while(next_permutation(a, a+m));
  }
  if(n==3) {
    do {
      if(ok(0, 1, 2) && ok(3, 4, 5) && ok(0, 3, 6) && ok(1, 4, 7) &&
          ok(0, 4, 8) && ok(2, 4, 6)) {
        REP(i, 3) {
          REP(j, 3) printf("%d ", a[i*3+j]);
          printf("\n");
        }
        break;
      }
    } while(next_permutation(a, a+m));
  }
  if(n==4) {
    go0((1<<m)-1);
  }

#ifdef LocalHost
  cout << endl << endl << "TIME: " << clock() << endl;
#endif
  return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#pragma comment (linker, "/STACK:16777216")
#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <list>
#include <stack>
#include <string>
#include <fstream>
#include <math.h>
#include <limits>
#include <set>
#include <map>
#include <sstream>
#include <stdio.h>
#include <time.h>
#include <memory.h>
#include <cassert>
#include <complex>
using namespace std;

///////////////// macros and typedefs ///////////////////
#define rep(i, n) for (int i = 0, _n = (n); i < _n; ++i)
#define repd(i, n) for (int i = (n)-1; i >= 0; --i)
#define _fill(a, x) memset((a), (x), sizeof((a)))
#define DEB(k) cerr<<"debug: "#k<<"="<<k<<endl;
#define all(c) (c).begin(), (c).end()
#define mp(a, b) make_pair(a, b)
#define l(c) (int)((c).size())
#define sqr(a) ((a)*(a))
#define inf 0x7f7f7f7f
#define pb push_back
#define ppb pop_back
#define x first
#define y second
typedef long long ll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef pair<int,int> pi;

int n, m;
int a[16];
int d[4][4];
int a1[4], a2[4], a3[8];
int c1, c2, c3;
bool used[8];

vector<pi> Q;

int Find(int val) {
   rep(i, 8) if (!used[i] && a3[i] == val) return i;
   return -1;
}

bool solve(int sum) {
   rep(i, 4) d[i][i] = a1[i];
   rep(i, 4) d[4-i-1][i] = a2[i];
   for (int i = 0; i < 8; i++) {
      for (int j = 0; j < 8; j++) if (i != j) {
         _fill(used, 0);
         used[i] = used[j] = true;
         d[0][1] = a3[i];
         d[1][0] = a3[j];
         int row = d[0][0] + d[0][1] + d[0][3];
         int col = d[0][0] + d[1][0] + d[3][0];

         int idx = Find(sum-row);
         if (idx == -1) return false;
         used[idx] = true;
         d[0][2] = a3[idx];

         idx = Find(sum-col);
         if (idx == -1) return false;
         used[idx] = true;
         d[2][0] = a3[idx];

         idx = Find(sum - d[0][1] - d[1][1] - d[2][1]);
         if (idx == -1) return false;
         used[idx] = true;
         d[3][1] = a3[idx];

         idx = Find(sum - d[0][2] - d[1][2] - d[2][2]);
         if (idx == -1) return false;
         used[idx] = true;
         d[3][2] = a3[idx];

         idx = Find(sum - d[1][0] - d[1][1] - d[1][2]);
         if (idx == -1) return false;
         used[idx] = true;
         d[1][3] = a3[idx];

         idx = Find(sum - d[2][0] - d[2][1] - d[1][2]);
         if (idx == -1) return false;
         used[idx] = true;
         d[2][3] = a3[idx];

         if (d[3][0] + d[3][1] + d[3][2] + d[3][3] != sum) return false;
         if (d[0][3] + d[1][3] + d[2][3] + d[3][3] != sum) return false;

         cout << sum << endl;
         rep(i, n) {
            rep(j, n)
               cout << d[i][j] << " ";
            cout << endl;
         }
         return true;
      }
   }
   return false;
}

bool solved(pi A, pi B) {
   c1 = c2 = c3 = 0;
   rep(i, m) {
      if ((A.y>>i)&1) a1[c1++] = a[i];
      else if ((B.y>>i)&1) a2[c2++] = a[i];
      else { a3[c3++] = a[i]; }
   }
   sort(a1, a1+4);
   sort(a2, a2+4);
   do {
      do {
         if (solve(A.x))
            return true;
      } while (next_permutation(a2, a2+4));
   } while (next_permutation(a1, a1+4));
   return false;
}

bool solve4() {
   for (int i1 = 0; i1 < m; i1++)
   for (int i2 = i1+1; i2 < m; i2++)
   for (int i3 = i2+1; i3 < m; i3++)
   for (int i4 = i3+1; i4 < m; i4++) {
      int mask = (1<<i1) | (1<<i2) | (1<<i3) | (1<<i4);
      int sum = a[i1]+a[i2]+a[i3]+a[i4];
      Q.pb(pi(sum, mask));
   }
   sort(all(Q));
   for (int i = 0; i < l(Q); i++) {
      int j = i;
      while (j+1 < l(Q) && Q[j+1].x == Q[i].x)
         j++;
      for (int i1 = i; i1 < j; i1++)
         for (int i2 = i1; i2 < j; i2++)
            if ((Q[i1].y & Q[i2].y) == 0 && solved(Q[i1], Q[i2]))
               return true;
      i = j;
   }
   return false;
}

void solution()
{
   cin >> n;
   rep(i, n*n)
      cin >> a[i];
   m = n*n;
   sort(a, a+m);
   if (n == 1) {
      cout << a[0] << endl;
      cout << a[0] << endl;
   }
   else if (n == 2) {
      do {
         rep(i, m)
            d[i/n][i%n] = a[i];
         int s = d[0][0]+d[0][1];
         int s2 = d[1][0]+d[1][1];
         int s3 = d[0][0]+d[1][0];
         int s4 = d[0][1]+d[1][1];
         int s5 = d[0][0]+d[1][1];
         int s6 = d[0][1]+d[1][0];
         if (s != s2 || s != s3 || s != s4 || s != s5 || s != s6)
            continue;
         cout << s << endl;
         rep(i, n) {
            rep(j, n)
               cout << d[i][j] << " ";
            cout << endl;
         }
         break;
      } while (next_permutation(a, a+m));
   }
   else if (n == 3) {
      do {
         rep(i, m)
            d[i/n][i%n] = a[i];
         int s = d[0][0]+d[0][1]+d[0][2];
         int s2 = d[1][0]+d[1][1]+d[1][2];
         int s3 = d[2][0]+d[2][1]+d[2][2];
         int s4 = d[0][0]+d[1][0]+d[2][0];
         int s5 = d[0][1]+d[1][1]+d[2][1];
         int s6 = d[0][2]+d[1][2]+d[2][2];
         int s7 = d[0][0]+d[1][1]+d[2][2];
         int s8 = d[0][2]+d[1][1]+d[2][0];
         if (s != s2 || s != s3 || s != s4 || s != s5 || s != s6 || s != s7 || s != s8)
            continue;
         cout << s << endl;
         rep(i, n) {
            rep(j, n)
               cout << d[i][j] << " ";
            cout << endl;
         }
         break;
      } while (next_permutation(a, a+m));
   }
   else if (n == 4) {
      if (!solve4())
         throw 1;
   }
}

int main()
{
#ifdef MY_JUDGE
   freopen("my.in", "rt", stdin);
   //freopen("my.out", "wt", stdout);
#endif
   solution();
#ifdef MY_JUDGE
   double time = clock()*1./CLOCKS_PER_SEC;
   fprintf(stderr, "Time: %.2lf sec\n", time);
#endif
   return 0;
}

#include <iomanip>
#include <numeric>
#include <functional>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <cctype>
#include <sstream>
#include <map>
#include <set>
#include <cstdio>
#include <queue>
#define f(i,x,y) for(int i=x;i<y;i++)
#define fd(i,y,x) for(int i=y;i>=x;i--)
#define FOR(it,A) for( typeof A.begin() it = A.begin(); it!=A.end(); it++)
#define impr(A) for( typeof A.begin() chen = A.begin(); chen !=A.end(); chen++ ) cout<<*chen<<" "; cout<<endl
#define ll long long
#define vint vector<int>
#define clr(A,x) memset(A,x,sizeof(A))
#define CLR(v) f(i,0,n) v[i].clear()
#define oo (1<<30)
#define ones(x) __builtin_popcount(x)
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define pb push_back
#define eps (1e-9)
#define cua(x) (x)*(x)
using namespace std;

int n;
map <int,int> rep;
int a[4][4];
int suma;

bool force (int x, int y){
//	f(i,0,n){ f(j,0,n)cout << a[i][j] << " "; cout << endl; }
	if (x==n){
		int d1 = 0; f(i,0,n) d1 += a[i][i];
		int d2 = 0; f(i,0,n) d2 += a[i][n-1-i];
		bool ok = d1==suma && d2==suma;
		if (ok==0) return 0;
		cout << suma << endl;
		f(i,0,n){
			f(j,0,n) printf ("%d%c", a[i][j], j==n-1? 10:32);
		}
		return 1;
	}

	if (y==n-1){
		int num = suma;
		f(i,0,n-1) num -= a[x][i];
		if (rep.count(num)==0 || rep[num]==0) return 0;
		a[x][y] = num;
		rep[num]--;
		if (force (x+1, 0)) return 1;
		rep[num]++;
		return 0;
	}
	if (x==n-1){
		int num = suma;
		f(i,0,n-1) num -= a[i][y];
		if (rep.count(num)==0 || rep[num]==0) return 0;
		a[x][y] = num;
		rep[num]--;
		if (force (x, y+1)) return 1;
		rep[num]++;
		return 0;
	}

	FOR(it, rep)if (it->second){
		it->second--;
		a[x][y] = it->first;
		if (force (x,y+1)) return 1;
		it->second++;
	}
	return 0;
}
bool fila (int,int);
bool columna (int,int);

bool fila (int x, int y){
	if (y==n-1){
		int num = suma;
		f(i,0,n-1) num -= a[x][i];
		if (rep.count(num)==0 || rep[num]==0) return 0;
		if (x==n-1){
			a[x][y] = num;
			int d1 = 0, d2 = 0;
			f(i,0,n) d1 += a[i][i];
			f(i,0,n) d2 += a[i][n-1-i];
			bool ok =  d1==suma && d2==suma;
			if (ok==0) return 0;
			cout << suma << endl;
			f(i,0,n){
				f(j,0,n) printf ("%d%c", a[i][j], j==n-1? 10:32);
			}
			return 1;
		}

		rep[num]--;
		a[x][y] = num;
		if (columna (x+1,x)) return 1;
		rep[num]++;
		return 0;
	}
	FOR(it, rep)if (it->second){
		it->second--;
		a[x][y] = it->first;
		if (fila (x, y+1)) return 1;
		it->second++;
	}
	return 0;
}

bool columna(int x, int y){

	if (x==n-1){
		int num = suma;
		f(i,0,n-1) num -= a[i][y];
		if (rep.count(num)==0 || rep[num]==0) return 0;
		rep[num]--;
		a[x][y] = num;
//		f(i,0,4){ f(j,0,4) cout <<a[i][j]<<"\t"; cout << endl; }
		if (fila (y+1, y+1)) return 1;
		rep[num]++;
		return 0;
	}
	FOR(it, rep)if (it->second){
		it->second--;
		a[x][y] = it->first;
		if (columna (x+1, y)) return 1;
		it->second++;
	}
	return 0;
}

int main()
{
	cin >> n;
	int x;
	f(i,0,n*n) cin >> x, rep[x]++, suma += x;
	suma /= n;

	if (n<4) force (0,0);
	else fila (0,0);

}

#include <vector>
#include <cstring>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <queue>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <stdlib.h>
#include <ctime>
#include <string>
#include <complex>
#ifdef __ASD__
//#include <windows.h>
#endif
using namespace std;
#define all(a) a.begin(),a.end()
#define forn(i,n) for(int i=0;i<(n);++i)
#define fornn(i,n) for(i=0;i<(n);++i)
#define lng long long
#define SQ(a) ((a)*(a))
#define forv(i,v) for(int i=0;i<(int)v.size();++i)
#define mp make_pair
#define pb push_back
#define ABS(a) ((a)<0?-(a):(a))
#define iinf 1000000000
#define linf 1000000000000000000LL
#define dinf 1e200
#define left asdleft
#define prev asdprev
#define eps 1e-12
#define y1 asdy1
#define y2 asdy2
#define EQ(a,b) abs((a)-(b))<eps
#define PI 3.1415926535897932384626433832795
#define DEG2RAD PI/180
#define ulng unsigned lng
#define next asdnext
#define prev asdprev
#define PII pair<int,int>
#define X first
#define Y second
void mytimer(string task){
#ifdef __ASD__
        //static LARGE_INTEGER prev;      LARGE_INTEGER cur,freq; QueryPerformanceCounter(&cur);  QueryPerformanceFrequency(&freq);       if(task!="")            cout<<task<<" took "<<(cur.QuadPart-prev.QuadPart)*1.0/freq.QuadPart<<endl;     prev=cur;
#endif
}
void ext(int c){
#ifdef __ASD__
        exit(c);
#endif
}

int val[20];
int cnt[20];
int n;
int m;
int S;
int res[4][4];
bool done;

struct action{
	enum{
		ANY,
		ROW,
		COL,
	} type;
	int a,b;
};

action acts[]={{action::ANY,0,0},{action::ANY,0,1},{action::ANY,0,2},{action::ROW,0,3},{action::ANY,1,0},{action::ANY,2,0},{action::COL,3,0},{action::ANY,1,1},{action::ANY,1,2},{action::ROW,1,3},{action::ANY,2,1},{action::COL,3,1},{action::ANY,2,2},{action::ROW,2,3},{action::COL,3,2},{action::ROW,3,3}};
const int acnt=sizeof(acts)/sizeof(acts[0]);

bool doit(int ac){
	if(ac==acnt){
		if(res[0][0]+res[1][1]+res[2][2]+res[3][3]==S && res[0][3]+res[1][2]+res[2][1]+res[3][0]==S)
			return true;
		return false;
	}
	action act=acts[ac];
	if(act.type==action::ANY){
		forn(v,m){
			if(!cnt[v])
				continue;
			--cnt[v];
			res[act.a][act.b]=val[v];
			if(doit(ac+1))
				return true;
			res[act.a][act.b]=0;
			++cnt[v];
		}
	}else if(act.type==action::ROW){
		int s=0;
		forn(i,4)
			s+=res[act.a][i];
		int v=lower_bound(val,val+m,S-s)-val;
		if(v==m||val[v]!=S-s||!cnt[v])
			return false;

		res[act.a][act.b]=S-s;
		--cnt[v];
		if(doit(ac+1))
			return true;
		res[act.a][act.b]=0;
		++cnt[v];
	}else if(act.type==action::COL){
		int s=0;
		forn(i,4)
			s+=res[i][act.b];
		int v=lower_bound(val,val+m,S-s)-val;
		if(v==m||val[v]!=S-s||!cnt[v])
			return false;

		res[act.a][act.b]=S-s;
		--cnt[v];
		if(doit(ac+1))
			return true;
		res[act.a][act.b]=0;
		++cnt[v];
	}else
		exit(321);

	return false;
}

int main(){
#ifdef __ASD__
    freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#endif

	ios_base::sync_with_stdio(false);

	cin>>n;
	vector<int> src(n*n);
	forn(i,n*n){
		cin>>src[i];
		S+=src[i];
		val[i]=src[i];
	}
	S/=n;
	sort(all(src));

	if(n<=3){
		do{
			int ss1=0,ss2=0;
			bool ok=true;
			forn(i,n){
				int s1=0,s2=0;
				forn(j,n){
					s1+=src[i*n+j];
					s2+=src[j*n+i];
				}
				ss1+=src[i*n+i];
				ss2+=src[i*n+(n-1-i)];
				if(s1!=S||s2!=S){
					ok=false;
					break;
				}
			}
			if(ss1!=S||ss2!=S)
				ok=false;
			if(ok){
				forn(i,n)
					forn(j,n)
						res[i][j]=src[i*n+j];
				break;
			}
		}while(next_permutation(all(src)));
	}else{
		sort(val,val+n*n);
		m=unique(val,val+n*n)-val;
		forn(i,m)
			cnt[i]=upper_bound(all(src),val[i])-lower_bound(all(src),val[i]);

		if(!doit(0))
			exit(99);
	}

	cout<<S<<endl;
	forn(i,n){
		forn(j,n)
			cout<<res[i][j]<<' ';
		cout<<endl;
	}

	return 0;
}

#include<cstdio>
#include<algorithm>
#define MAX 16

using namespace std;

int n, N;
int arr[MAX];
int s;

int row[4], col[4], dia[2];
bool vis[MAX];
int grid[MAX][MAX];
bool flag;

void ct(int r, int c, int v)
{
    if(r == c)
        dia[0] += v;

    if(r + c == n-1)
        dia[1] += v;

    row[r] += v;
    col[c] += v;
}

void dfs(int now)
{
    if(flag)
        return ;

    if(now == N)
    {
        printf("%d\n", s);
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(j)
                    printf(" ");
                printf("%d", grid[i][j]);
            }
            printf("\n");
        }
        flag = 1;
        return ;
    }
    int r = now / n;
    int c = now % n;
    for(int i=0;i<N;i++)
    {
        if(i>0 && !vis[i-1] && arr[i-1] == arr[i])
            continue;

        if(vis[i])
            continue;

        if(c == 0 && r == n-1)
            if(dia[1] + arr[i] != s)
                continue;

        if(c == n-1 && r == n-1)
            if(dia[0] + arr[i] != s)
                continue;

        if(c == n-1)
            if(row[r] + arr[i] != s)
                continue;

        if(r == n-1)
            if(col[c] + arr[i] != s)
                continue;

        grid[r][c] = arr[i];

        vis[i] = 1;
        ct(r, c, arr[i]);

        dfs(now + 1);

        ct(r, c, -arr[i]);
        vis[i] = 0;
    }
}

int main()
{
    scanf("%d", &n);

    N = n*n;

    for(int i=0;i<N;i++)
    {
        scanf("%d", &arr[i]);
        s += arr[i];
    }
    s /= n;
    sort(arr, arr+N);

    dfs(0);
}

#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <list>
#include <stack>
#include <string>
#include <fstream>
#include <math.h>
#include <limits>
#include <set>
#include <map>
#include <sstream>
#include <stdio.h>
#include <time.h>
#include <memory.h>
#include <cassert>
#include <complex>
using namespace std;

int mas[16];
int perm[16];
int matr[4][4];
int main()
{
    //freopen("in.in","rt",stdin);
    int n;
    cin>>n;
    for (int i=0;i<n*n;i++) {
        cin>>mas[i];
        perm[i]=i;
    }
    if (n==1) {
        cout<<mas[0]<<endl;
        cout<<mas[0]<<endl;
        return 0;
    }
    while(next_permutation(perm,perm+n*n)) {
        for (int i=0;i<n;i++) {
            for (int j=0;j<n;j++) {
                matr[i][j]=mas[perm[i*n+j]];
            }
        }
        int a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0;
        for (int i=0;i<n;i++) {
            a+=matr[0][i];
            b+=matr[min(1,n-1)][i];
            c+=matr[min(2,n-1)][i];
            d+=matr[i][0];
            e+=matr[i][min(1,n-1)];
            f+=matr[i][min(2,n-1)];
            g+=matr[i][i];
            h+=matr[i][n-1-i];
        }
        if (a==b&&a==c&&a==d&&a==e&&a==f&&a==g&&a==h) {
            cout<<a<<endl;
            for (int i=0;i<n;i++) {
                for (int j=0;j<n;j++) {
                    cout<<matr[i][j]<<" ";
                }
                cout<<endl;
            }
            return 0;
        }
    }
    return 0;
}

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <set>
#include <ctime>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <cassert>
#include <climits>
#include <fstream>

using namespace std;

#define Filename ""
#define sqr(a) (a)*(a)

typedef long long lng;
typedef long double ldb;

const double EPS = 1e-8;

int n, m, a[20], b[15][15], sumc[20], sumr[20], was[20], s;

void Load()
{
    cin >> n;
    m = n;
    n *= n;
    for (int i = 0;i < n;i++)
        cin >> a[i];
}

bool check () {
    int cur = 0, i = 0, j = 0;

    for (i = 0;i < m;i++, j++) cur += b[i][j];
    if (cur != s) return false;

    cur = 0;
    for (i = 0, j = m - 1;i < m;i++, j--) cur += b[i][j];
    if (cur != s) return false;
    return true;
}

void go (int i, int j) {
    if (j == m) {
        if (sumr[i] != s) return;
        i++, j = 0;
    }
    if (i == m - 1 && j > 0) {
        if (sumc[j - 1] != s) return;
    }

    if (i == m) {
        if (check()) {
            for (int i = 0;i < m;i++) {
                for (int j = 0;j < m;j++)
                    cout << b[i][j] << " ";
                cout << endl;
            }
            exit (0);
        }
        return;
    }

    for (int k = 0;k < n;k++) {
        if (was[k]) continue;
        was[k] = 1;
        b[i][j] = a[k];
        sumc[j] += a[k];
        sumr[i] += a[k];
        go (i, j + 1);
        sumc[j] -= a[k];
        sumr[i] -= a[k];
        was[k] = 0;
    }
}

void Solve()
{
    if (n == 1) {
        printf ("%d\n%d", a[0], a[0]);
        return;
    }
    random_shuffle (a, a + n);
    s = 0;
    for (int i = 0;i < n;i++)
        s += a[i];
    s /= m;

    printf ("%d\n", s);
    go (0, 0);
}

int main()
{
    //freopen(Filename"a.in", "r", stdin);
    //freopen(Filename".out", "w", stdout);
    Load();

    Solve();

    return 0;
}

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;

int n, a[16]={}, sum = 0;
int u[16]={};
int b[16][16]={}, r[16]={}, c[16]={}, d1, d2;
int N, P;
bool go(int x, int y) {
	if(x==0 && y==3 && b[0][0] > b[0][1]) return false;
	/*if(x>=2) {
		fprintf(stderr, "(%d, %d)\n", x,  y);
		for(int i=0;i<n;i++)
				for(int j=0;j<n;j++)
					fprintf(stderr, "%d ", b[i][j]);
			fprintf(stderr, "\n");
	}*/
	if(x==n) {
		if(d1==0 && d2==0 && c[n-1]==0) {
			printf("%d\n", sum);
			for(int i=0;i<n;i++,puts(""))
				for(int j=0;j<n;j++)
					printf("%d ", b[i][j]);
			return true;
		}
		return false;
	}
	if(y==n-1) {
		b[x][y] = r[x];
		int i=0;
		for(i=0;i<N;i++) if(!u[i] && r[x]==a[i]) break;
		if(i==N) return false;
		u[i] = 1;
		c[n-1] -= a[i];
		if(x==y) d1 -= a[i];
		if(x+y==n-1) d2 -= a[i];
		if(go(x+1, 0)) return true;
		c[n-1] += a[i];
		if(x==y) d1 += a[i];
		if(x+y==n-1) d2 += a[i];
		u[i]=0;
		b[x][y] = 0;
		return false;
	} else if(x==n-1) {
		b[x][y] = c[y];
		int i=0;
		for(i=0;i<N;i++) if(!u[i] && c[y]==a[i]) break;
		if(i==N) return false;
		u[i] = 1;
		r[n-1] -= a[i];
		if(x==y) d1 -= a[i];
		if(x+y==n-1) d2 -= a[i];
		if(go(x, y+1)) return true;
		r[n-1] += a[i];
		if(x==y) d1 += a[i];
		if(x+y==n-1) d2 += a[i];
		u[i]=0;
		b[x][y] = 0;
		return false;
	}
	for(int i=0;i<N;i++) {
		if(!u[i]) {
			/*int flg=0;
			for(int j=0;j<i;j++)
				if(!u[j] && a[j]==a[i]) flg=1;
			if(flg) continue;*/
			b[x][y] = a[i];
			u[i] = 1;
			r[x] -= a[i];
			c[y] -= a[i];
			if(x==y) d1 -= a[i];
			if(x+y==n-1) d2 -= a[i];
			if(go(x, y+1)) return true;
			r[x] += a[i];
			c[y] += a[i];
			if(x==y) d1 += a[i];
			if(x+y==n-1) d2 += a[i];
			u[i] = 0;
			b[x][y] = 0;
		}
	}
	b[x][y] = 0;
	return false;
}

int main(void) {
	scanf("%d", &n);
	for(int i=0;i<n*n;i++) scanf("%d", &a[i]);
	for(int i=0;i<n*n;i++) sum += a[i];
	sum /= n;
	sort(a, a+n*n);
	random_shuffle(a, a+n*n);
	N = n*n, P = (n-1)*(n-1);
	for(int i=0;i<n;i++) r[i]=c[i]=sum;
	d1=d2=sum;
	go(0, 0);
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

const int maxn = 20;

int a[maxn], b[maxn], ans[maxn][maxn], mark[maxn];

void bt(int k,int n,int s) {
	if (k == n * n) {

		int sum1 = 0, sum2 = 0;
		for (int i = 0; i < n; i++)
			sum1 += a[b[i * n + i]], sum2 += a[b[i * n + (n - 1 - i)]];
		if (sum1 != s || sum2 != s)
			return;

		for (int i = 0; i < n * n; i++)
			ans[i / n][i % n] = a[b[i]];

		cout << s << '\n';
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++)
				cout << ans[i][j] << ' ';
			cout << '\n';
		}
		exit(0);
	}
	if (k > 0 && k % n == 0) {
		int sum = 0;
		for (int i = 0; i < n; i++)
			sum += a[b[k - 1 - i]];
		if (sum != s)
			return;
	}
	if (k > n * (n - 1)) {
		int sum = 0, c = (k - 1) % n;
		for (int i = 0; i < n; i++)
			sum += a[b[i * n + c]];
		if (sum != s)
			return;
	}
	for (int i = 0; i < n * n; i++)
		if (!mark[i]) {
			mark[i] = true;
			b[k] = i;
			bt(k + 1, n, s);
			mark[i] = false;
		}
}

int main() {
	srand(701);
	int n, s = 0;
	cin >> n;
	for (int i = 0; i < n * n; i++)
		cin >> a[i], s += a[i];
	random_shuffle(a, a + n * n);
	s /= n;
	bt(0, n, s);
	return 0;
}

/*
Author : Hocky Yudhiono
8/5/2020 5:00:37 PM

1. You can sort the query if offline!
2. Don't bring the dp remaining state when dfsing on DP on Tree.
3. Try to reverse (Think from the back) if you stuck.
4. Be careful when submitting Div. 2 D-F, dont waste it on stupid WAs.
5. Try to reduce a problem, think of it when you're making subtasks
   like when problemsetting.
*/

#include <algorithm>
#include <iostream>
#include <numeric>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <iomanip>
#include <cstdio>
#include <limits>
#include <string>
#include <vector>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;

typedef long long LL;
typedef long long ll;
typedef long double LD;
typedef vector<int> vi;
typedef pair<LL,LL> PLL;
typedef pair<LL,int> PLI;
typedef pair<int,int> PII;
typedef pair<int,int> pii;
typedef vector<vector<LL>> VVL;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define popf pop_front
#define pf push_front
#define popb pop_back
#define mp make_pair
#define pb push_back
#define remove erase
#define fi first
#define se second

// If the time limit is strict, try not to use long double

// Remember to undefine if the problem is interactive
#define endl '\n'
#define DEBUG(X) cout << ">>> DEBUG(" << __LINE__ << ") " << #X << " = " << (X) << endl

const double EPS = 1e-9;
const int INFMEM = 63;
const int INF = 1061109567;
const LL LINF = 4557430888798830399LL;
const double DINF = numeric_limits<double>::infinity();
const LL MOD = 1000000007;
const int dx[8] = {0,0,1,-1,1,-1,1,-1};
const int dy[8] = {1,-1,0,0,1,-1,-1,1};
// Do dir^1 to get reverse direction
const char dch[4] = {'R','L','D','U'};
// const string ds[8] = {"E","W","S","N","SE","NW","SW","NE"};
const double PI = 3.141592653589793;

inline void open(string a){
    freopen((a+".in").c_str(),"r",stdin);
    freopen((a+".out").c_str(),"w",stdout);
}

inline void fasterios(){
    // Do not use if interactive
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    // cout << fixed << setprecision(10);
}

LL n;

LL getNum(LL x, LL y, LL c, bool oneIndex = 1){
    return (x-oneIndex)*c+y;
}

PII getKoor(LL num, LL c, bool oneIndex = 1){
    PII ret; ret.se = num%c;
    if(!ret.se && oneIndex) ret.se = c;
    ret.fi = (num-ret.se)/c; ret.fi += oneIndex;
    return ret;
}

int main(){
    cin >> n;
    vector <LL> isi(n*n);
    trav(cur, isi) cin >> cur;
    LL mini = LINF;
    trav(cur, isi) mini = min(mini, cur);
    sort(isi.begin(), isi.end());
    do{
        int x = 0; int y = 0;
        int sum[2][4];
        int sumd[2];
        memset(sum,0,sizeof(sum));
        memset(sumd,0,sizeof(sumd));
        trav(cur, isi){
            sum[0][x] += cur;
            sum[1][y] += cur;
            y++; if(y == n) y = 0, x++;
        }
        rep(i,0,n){
            sumd[0] += isi[i*n+i];
            sumd[1] += isi[(n-1)*(i+1)];
        }
        bool can = 1;
        rep(i,0,2){
            rep(j,0,n){
                if(sum[i][j] != sum[0][0]){
                    can = 0;
                    break;
                }
            }
            if(!can) break;
        }
        if(can && sum[0][0] == sumd[0] && sumd[0] == sumd[1]){
            cout << sum[0][0] << endl;
            rep(i,0,n*n) cout << isi[i] << " \n"[i%n == (n-1)];
            return 0;
        }
    }while(next_permutation(isi.begin(), isi.end()));

    return 0;
}

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cmath>
#include <memory.h>
#include <string>
#include <set>
#include <map>
#include <queue>
using namespace std;
#define forn(i,n) for(int i=0;i<(n);++i)
#define forv(i,v) forn(i,(int)(v).size())
#define iinf 1000000000
#define linf 1000000000000000000LL
#define dinf 1e200
#define all(v) (v).begin(),(v).end()
#define pb push_back
#define mp make_pair
#define lng long long
#define eps 1e-11
#define EQ(a,b) (fabs((a)-(b))<eps)
#define SQ(a) ((a)*(a))
#define PI 3.14159265359
#define index asdindex
#define FI first
#define SE second
#define prev asdprev
#define ends asdends
#define PII pair<int,int>
#define X first
#define Y second
#define mset(a,b) memset(a,b,sizeof(a))

int n;
int src[16];
int s;

int f(){
    int res=0;
    forn(i, n){
        int sum = 0;
        for(int j=i*n;j<(i+1)*n;++j){
            sum += src[j];
        }
        res+=abs(sum-s);
    }
    forn(i, n){
        int sum = 0;
        for(int j=i;j<n*n;j+=n){
            sum += src[j];
        }
        res+=abs(sum-s);
    }
    {
        int sum = 0;
        for(int i=0;i<n*n;i+=n+1)sum += src[i];
        res+=abs(sum-s);
    }
    int sum = 0;
    int j=0;
    for(int i=n-1;i<n*n;i+=n-1,++j){
        if (j==n)break;
        sum += src[i];
    }
    res+=abs(sum-s);
    return res;
}

void print(){
    cout << s << endl;
    forn(i,n*n){
        cout<<src[i]<<" ";
        if (i%n==n-1)cout<<endl;
    }
    exit(0);
}

int main(){
#ifdef __ASD__
    freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#endif
    cin >> n;
    s=0;
    forn(i,n*n){
        cin>>src[i];
        s+=src[i];
    }
    s=s/n;
    for(;;){
        random_shuffle(src, src+n*n);
        forn(i, 3){
            int q = f();
            if (q==0)
                print();
            //break;
            forn(i1, n*n){
                for(int i2=i1+1;i2<n*n;++i2){
                    swap(src[i1], src[i2]);
                    int new_q = f();
                    if (new_q>=q){
                        swap(src[i1], src[i2]);
                    }else{
                        q = new_q;
                    }
                }
            }
l1:;
        }
    }

    return 0;
}

#include <iostream>
#include <algorithm>

using namespace std;

template<int N>
void gao() {
    union {
        struct {
            long long a[N][N];
        };
        struct {
            long long p[N * N];
        };
    } u;
    long long s = 0;

    for (int i = 0; i < N * N; ++i) {
        cin >> u.p[i];
        s += u.p[i];
    }
    sort(u.p, u.p + N * N);
    s /= N;

    do {
        long long d1 = 0, d2 = 0;
        for (int i = 0; i < N; ++i) {
            d1 += u.a[i][i];
            d2 += u.a[i][N - 1 - i];
        }
        bool flag = d1 == s && d2 == s;
        for (int i = 0; flag && i < N; ++i) {
            long long r = 0, c = 0;
            for (int j = 0; j < N; ++j) {
                r += u.a[i][j];
                c += u.a[j][i];
            }
            flag &= r == s;
            flag &= c == s;
        }
        if (flag) {
            break;
        }
    } while (next_permutation(u.p, u.p + N * N));

    cout << s << endl;
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) {
            cout << u.a[i][j] << (j == N - 1 ? "\n" : " ");
        }
    }
}

const long long UNDEF = 0x123456789ABCDEFLL;

long long s;
long long x[16];
long long y[4][4];

int find(long long z) {
    return find(x, x + 16, z) - x;
}

void show() {
    cout << s << endl;
    for (int r = 0; r < 4; ++r) {
        for (int c = 0; c < 4; ++c) {
            cout << y[r][c] << (c == 3 ? "\n" : " ");
        }
    }
}

bool check() {
    return y[2][0] + y[2][1] + y[2][2] + y[2][3] == s
        && y[3][0] + y[3][1] + y[3][2] + y[3][3] == s;
}

void dfs(int i, int j) {
    if ((i == 0 || i == 1) && j == 3) {
        int p = find(s - y[i][0] - y[i][1] - y[i][2]);
        if (p < 16) {
            y[i][3] = x[p];
            x[p] = UNDEF;
            if (i == 0) {
                dfs(1, 0);
            } else {
                dfs(2, 1);
            }
            x[p] = y[i][3];
        }
    } else if (i == 2 && j == 0) {
        int p = find(s - y[0][3] - y[1][2] - y[2][1]);
        if (p < 16) {
            y[3][0] = x[p];
            x[p] = UNDEF;
            int q = find(s - y[0][0] - y[1][0] - y[3][0]);
            if (q < 16) {
                y[2][0] = x[q];
                x[q] = UNDEF;
                dfs(2, 2);
                x[q] = y[2][0];
            }
            x[p] = y[3][0];
        }
    } else if (i == 2 && j == 3) {
        int p = find(s - y[0][0] - y[1][1] - y[2][2]);
        if (p < 16) {
            y[3][3] = x[p];
            x[p] = UNDEF;
            int q = find(s - y[0][3] - y[1][3] - y[3][3]);
            if (q < 16) {
                y[2][3] = x[q];
                x[q] = UNDEF;
                dfs(3, 1);
                x[q] = y[2][3];
            }
            x[p] = y[3][3];
        }
        // show();
    } else if (i == 3 && j == 3) {
        if (check()) {
            show();
            throw 1;
        }
    } else if (i == 3) {
        int p = find(s - y[0][j] - y[1][j] - y[2][j]);
        if (p < 16) {
            y[3][j] = x[p];
            x[p] = UNDEF;
            dfs(i, j + 1);
            x[p] = y[3][j];
        }
    } else {
        long long last = UNDEF;
        for (int k = 0; k < 16; ++k) {
            if (x[k] != UNDEF && x[k] != last) {
                last = x[k];
                y[i][j] = x[k];
                x[k] = UNDEF;
                if (i < 2) {
                    dfs(i, j + 1);
                } else {
                    dfs(i, j ^ 1);
                }
                x[k] = y[i][j];
            }
        }
    }
}

template<>
void gao<4>() {
    s = 0;
    for (int i = 0; i < 16; ++i) {
        cin >> x[i];
        s += x[i];
    }
    sort(x, x + 16);
    s /= 4;

    try {
        dfs(0, 0);
    } catch (int) {
        return;
    }
    throw -1;
}

int main() {
    int n;

    cin >> n;
    fill(y[0], y[4], -1LL);
    switch (n) {
    case 1: gao<1>(); break;
    case 2: gao<2>(); break;
    case 3: gao<3>(); break;
    case 4: gao<4>(); break;
    }

    return 0;
}

#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int n;

bool isMagical(const vector<int>& a) {
    auto it = a.begin();
    int b[4][4];
    for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) {
        b[i][j] = *it;
        ++it;
    }

    set<int> sums;
    int d1 = 0, d2 = 0;
    for (int i = 0; i < n; ++i) {
        d1 += b[i][i];
        d2 += b[i][n-i-1];
        int h = 0, v = 0;
        for (int j = 0; j < n; ++j) {
            h += b[i][j];
            v += b[j][i];
        }
        sums.insert(h);
        sums.insert(v);
    }

    sums.insert(d1);
    sums.insert(d2);

    return sums.size() == 1;
}

int main() {
    cin >> n;
    vector<int> a(n*n);
    for (int i = 0; i < n*n; ++i)
        cin >> a[i];
    sort(a.begin(), a.end());
    do {
        if (isMagical(a)) {
            int s = accumulate(a.begin(), a.begin() + n, 0);
            cout << s << endl;
            for (int i = 0; i < n*n; ++i) {
                cout << a[i];
                if (i%n == n-1)
                    cout << endl;
                else
                    cout << " ";
            }
            return 0;
        }
    } while (next_permutation(a.begin(), a.end()));
    return 0;
}

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
#define mp make_pair
#define s second
#define f first
#define pb push_back
typedef pair <int, int> pii;
typedef long long ll;
void gen(int);
void insert(int, int, int);

int b[20], v[20][20], size, s, n, cnt, ans[5][5];
pii a[20];
bool check()
{
  for (int i = 0; i < n; i++)
  {
    int r1 = 0, r2 = 0;
    for (int j = 0; j < n; j++)
    {
      r1 += ans[i][j];
      r2 += ans[j][i];
    }
    if (r1 != s || r2 != s) return false;
  }
  int d1 = 0, d2 = 0;
  for (int i = 0; i < n; i++)
  {
    d1 += ans[i][i];
    d2 += ans[i][n - i - 1];
  }
  return d1 == s && d2 == s;
}
bool use[20];
void insert(int ind, int k, int type)
{
  if (use[ind])
  {
    insert(ind + 1, k, type);
    return;
  }
  if (!k)
  {
    for (int i = 0; i < a[type].s; i++)
    {
      int pi = v[type][i] / n;
      int pj = v[type][i] % n;
      ans[pi][pj] = a[type].f;
    }
    gen(type + 1);
    return;
  }
  if (size - ind >= k)
  {
    v[type][k - 1] = ind;
    use[ind] = true;
    insert(ind + 1, k - 1, type);
    use[ind] = false;
    insert(ind + 1, k, type);
  }
}
void gen(int type)
{
  if (type == cnt)
  {
    if (check())
    {
      cout << s << endl;
      for (int i = 0; i < n; i++, cout << endl)
        for (int j = 0; j < n; j++)
          cout << ans[i][j] << " ";
      exit(0);
    }
    return;
  }
  insert(0, a[type].s, type);
}
int main()
{
  cin >> n;
  for (int i = 0; i < n * n; i++)
  {
    cin >> b[i];
    s += b[i];
  }
  s /= n;
  size = n * n;
  sort(b, b + size);
  for (int i = 0; i < size; i++)
  {
    int j = i;
    while (i < size && b[i] == b[j]) ++i;
    a[cnt++] = mp(b[j], i - j);
    i--;
  }
  gen(0);
  /*
  4
1 1 1 1 2 2 2 2 3 4 5 6 7 8 9 10

  */
  return 0;
}

#include<iostream>
#include<vector>
#include<string>
#include<stack>
#include<algorithm>
#include<cmath>
#include<set>
#include<queue>
#include<sstream>
#include<utility>

using std::pair;
using std::stringstream;
using std::next_permutation;
using std::sqrt;
using std::priority_queue;
using std::sort;
using std::stack;
using std::string;
using std::vector;
using std::cin;
using std::cout;
using std::endl;
using std::min;
using std::max;
using std::set;
using std::swap;
using std::random_shuffle;
using std::queue;
using std::sin;
using std::cos;
using std::make_pair;
using std::cos;
using std::cerr;

typedef long long ll;
typedef pair<ll, ll> pll;
const long double PI = 3.14159265358979323846;

vector<int> color;
vector<vector<pair<int, int> > > gr;
vector<int> p;
vector<int> cond;

bool dfs (int v) {
	color[v] = 1;
	for (size_t i=0; i<gr[v].size(); ++i) {
		if (cond[i] != i)
			continue;
		int to = gr[v][i].first;
		if (to == p[v])
			continue;
		if (color[to] == 0) {
			p[to] = v;
			if (dfs (to))  return true;
		}
		else if (color[to] == 1) {
			int cycle_end = v;

			vector<int> p1;
			p1.push_back(v);
			while(cycle_end != 0) {
				cycle_end = p[cycle_end];
				p1.push_back(cycle_end);
			}

			int cycle_st = to;

			int mini = to;
			/*while (cycle_end != cycle_st) {
				mini = min(mini, cycle_end);
				cycle.push_back(cycle_end);
				cycle_end = p[cycle_end];
			}
			cycle.push_back(cycle_st);*/
			vector<int> p2;
			p2.push_back(to);
			while(cycle_st != 0) {
				cycle_st = p[cycle_st];
				p2.push_back(cycle_st);
			}
			int j = 0;
			while ((j < p1.size()) && (j < p2.size()) && (p1[p1.size() - j - 1] == p2[p2.size() - 1 - j])) {
				++j;
			}
			vector<int> cycle;

			/*if ((j < p1.size()) && (j < p2.size())) {
				cycle.push_back(p1[p1.size() - j - 1]);
				cycle.push_back(p2[p2.size() - j - 1]);
			}*/
			cycle.push_back(p1[p1.size() - j]);

			for (int i = j; i < p1.size(); ++i) {
				cycle.push_back(p1[p1.size() - i - 1]);
				mini = min(mini, p1[p1.size() - i - 1]);
			}
			for (int i = j; i < p2.size(); ++i) {
				cycle.push_back(p2[p2.size() - i - 1]);
				mini = min(mini, p2[p2.size() - i - 1]);
			}
			for(int i = 0; i < cycle.size(); ++i)
				cond[cycle[i]] = mini;

			return true;
		}
	}
	color[v] = 2;
	return false;
}
vector<int> d;
bool ddfs (int v) {
	color[v] = 1;
	for (size_t i=0; i<gr[v].size(); ++i) {
		if (cond[i] != i)
			continue;
		int to = gr[v][i].first;
		if (to == i)
			continue;
		if (to == p[v])
			continue;
		if (color[to] == 0) {

			p[to] = v;
			if (gr[v][i].second == 1)
				d[to] = d[v] + 1;
			else
				d[to] = d[v];
		ddfs(to);
		}
	}
	color[v] = 2;
	return false;
}
int n, s;

bool magic(const vector<int>& data) {
	for (int i = 0; i < n; ++i) {
		int sum = 0;
		for (int j = 0; j < n; ++j)
			sum += data[i * n + j];
		if (sum != s)
			return false;
	}
	for (int i = 0; i < n; ++i) {
		int sum = 0;
		for (int j = 0; j < n; ++j)
			sum += data[j * n + i];
		if (sum != s)
			return false;
	}
	int sum = 0;
	for (int i = 0; i < n; ++i)
		sum += data[i * n + i];
	if (sum != s)
		return false;
	sum = 0;
	for (int i = 0; i < n; ++i)
		sum += data[i * n - i + n - 1];
	if (sum != s)
		return false;
	sum = 0;
	return true;
}
int main() {
	//freopen("input.txt", "r", stdin);
	cin >> n;
	vector<int> data(n * n);
	s = 0;
	for (int i = 0; i < n * n; ++i) {
		cin >> data[i];
		s += data[i];
	}
	s /= n;
	sort(data.begin(), data.end());
	do {

		if (magic(data)) {

			cout << s << endl;
			for (int i = 0; i < n; ++i) {
				for (int j = 0; j < n; ++j)
					cout << data[i * n + j] << " ";
				cout << endl;
			}
			return 0;
		}
		for (int j = 0; j < n - 1; ++j) {
		int sum = 0;
		for (int i = 0; i < n; ++i)
			sum += data[i + j * n];
		if (sum != s) {
			sort(data.begin() + n * (j + 1), data.end());
			reverse(data.begin() + n * (j + 1), data.end());
			break;
		}
		}
	} while (next_permutation (data.begin(), data.end()));
	return 0;
}

#include <stdio.h>
#include <string.h>
#define MAXN 16

int matrix[MAXN];
bool hassolution;
int DP[1<<MAXN][25];
int n;

bool find ( int vet[MAXN])
{
    int sum = 0;
    for (int i = 0; i < n*n; i += (n+1))
        sum += vet[i];
    int newsum = 0;
    for (int i = 0; i < n*n; i++)
    {
        newsum += vet[i];
        if ( (i+1)%n == 0)
        {
            if ( newsum != sum )
                return false;
            newsum = 0;
        }
    }
    for (int i = 0; i < n; i++)
    {
        int news = 0;
        int j = i;
        while ( j < n*n )
        {
            news += vet[j];
            j += n;
        }
        if (news != sum)
            return false;
    }
    int ksum = 0;
    for(int i = n-1; i < (n-1)*n + 1; i += (n-1))
        ksum += vet[i];
    if( ksum != sum )
        return false;
    printf("%d\n", sum);
    for (int i = 0; i < n*n; i++)
    {
        printf("%d ", vet[i]);
        if ( (i+1)%n == 0 )
            printf("\n");
    }
    return true;
}
int ct = 0;

void solve(int mask, int mask2, int vet[MAXN], int pos)
{
    int sum = 0;
    if ( mask == ((1<<(n*n))-1) && mask == mask2)
    {
        //printf("---%d\n", ct++);
        if ( find(vet) )
        {
            hassolution = true;
            //print(vet);
            return;
        }
    }
    for (int j = 0; j < n*n && !hassolution; j++)
    {
        if (!(mask2 & (1<<j)))
        {
            vet[pos] = matrix[j];
            //printf("I: %d Mj: %d\n", i, matrix[j]);
            solve(mask|(1<<pos), mask2|(1<<j),vet,pos+1);
        }
    }

}

int main()
{
    scanf("%d", &n);
    for (int i = 0; i < n*n; i++)
        scanf("%d", &matrix[i]);
    if( n == 1 )
    {
        printf("%d\n%d\n", matrix[0], matrix[0]);
        return 0;
    }
    int vet[MAXN];
    memset(DP,-1,sizeof(DP));
    solve(0,0,vet,0);
    return 0;
}

#include<cstdio>
#define MAX 16

int n, N;
int arr[MAX];
int s;

int row[4], col[4], dia[2];
bool vis[MAX];
int grid[MAX][MAX];
bool find;

void ct(int r, int c, int v)
{
    if(r == c)
        dia[0] += v;

    if(r + c == n-1)
        dia[1] += v;

    row[r] += v;
    col[c] += v;
}

void dfs(int now)
{
    if(find)
        return ;
    if(now == N)
    {
        /*
        for(int i=0;i<n;i++)
            printf("row[%d]=%d, ", i, row[i]);
        for(int i=0;i<n;i++)
            printf("col[%d]=%d, ", i, col[i]);
        printf("%d %d", dia[0], dia[1]);
        printf("\n");
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(j)
                    printf(" ");
                printf("%d", grid[i][j]);
            }
            printf("\n");
        }
        printf("\n");

        find = 1;
        //return ;
        */
        for(int i=0;i<n;i++)
            if(row[i] != s || col[i] != s)
                return ;

        if(dia[0] != s || dia[1] != s)
            return ;

        printf("%d\n", s);
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(j)
                    printf(" ");
                printf("%d", grid[i][j]);
            }
            printf("\n");
        }
        find = 1;
        return ;
    }
    int r = now / n;
    int c = now % n;
    for(int i=0;i<N;i++)
    {
        if(vis[i])
            continue;

        if(c == 0 && r == n-1)
            if(dia[1] + arr[i] != s)
                continue;

        if(c == n-1)
            if(row[r] + arr[i] != s)
                continue;

        if(r == n-1)
            if(col[c] + arr[i] != s)
                continue;

        grid[r][c] = arr[i];

        vis[i] = 1;
        ct(r, c, arr[i]);

        dfs(now + 1);

        ct(r, c, -arr[i]);
        vis[i] = 0;
    }
}

int main()
{
    scanf("%d", &n);

    N = n*n;

    for(int i=0;i<N;i++)
    {
        scanf("%d", &arr[i]);
        s += arr[i];
    }
    s /= n;

    //printf("%d\n", s);
    dfs(0);
}

#include <cstdio>
#include <map>
#include <algorithm>
using namespace std;

int sum = 0;
int total;
int ps[32];
int N;

int num[32];
int rs[32];
int board[8][8];
int p1[32];
int p2[32];

int xs[32];
int ys[32];

inline
int pick(int x)
{
    if(!rs[x])
	return 0;
    if(--rs[x] == 0)
    {
	p2[p1[x]] = p2[x];
	p1[p2[x]] = p1[x];
    }
    return x;
}

inline
void put(int x)
{
    if(rs[x]++ == 0)
    {
	p2[p1[x]] = x;
	p1[p2[x]] = x;
    }
}

inline
int find(int v)
{
    int l = 0, r = total;
    while(l + 1 < r)
    {
	int m = (l + r) / 2;
	if(num[m] == v)
	    return pick(m);
	else if(num[m] < v)
	    l = m;
	else
	    r = m;
    }
    return 0;
}

bool go(int x, int y, int g)
{
    if(x == N)
	return true;
    if(x == N - 1)
    {
	int v = 0;
	for(int i = 0; i < N - 1; i++)
	    v += board[i][y];
	int p = g - v;

	int z = find(p);
	if(z)
	    put(z);
	if(!z)
	    return false;

	if(y == 0)
	{
	    v = 0;
	    for(int i = 0; i < N - 1; i++)
		v += board[i][N - 1 - i];
	    if(g - v != p)
		return false;
	}
	if(y == N - 1)
	{
	    v = 0;
	    for(int i = 0; i < N - 1; i++)
		v += board[i][i];
	    if(g - v != p)
		return false;

	    v = 0;
	    for(int i = 0; i < N - 1; i++)
		v += board[N - 1][i];
	    if(g - v != p)
		return false;
	}

	pick(z);
	board[x][y] = p;
	if(go(x + (y == N - 1), (y == N - 1)? 0: (y + 1), g))
	    return true;
	put(z);
    }
    else if(y == N - 1 && (x != 0))
    {
        int v = 0;
	for(int i = 0; i < N - 1; i++)
	    v += board[x][i];
	int p = g - v;
	int z = find(p);
	if(z)
	    put(z);
	if(!z)
	    return false;
	pick(z);
	board[x][y] = p;
	if(go(x + 1, 0, g))
	    return true;
	put(z);
    }
    else
    {
	for(int z = p2[0]; z; z = p2[z])
	{
	    board[x][y] = num[z];
	    pick(z);
	    if(y == N - 1)
	    {
		g = 0;
		for(int i = 0; i < N; i++)
		    g += board[0][i];
		if(g == sum / N && go(x + 1, 0, g))
		    return true;
	    }
	    else
		if(go(x, y + 1, g))
		    return true;
	    put(z);
	}
    }
    return false;
}

int main()
{
    scanf("%d", &N);
    for(int i = 0; i < N * N; i++)
	scanf("%d", ps + i), sum += ps[i];

    if(N == 1)
    {
	printf("%d\n%d\n", ps[0], ps[0]);
	return 0;
    }

    /*
    int now = 0;
    for(int i = 0; i < N; i++)
    {
	xs[now] = 0;
	ys[now] = i;
	now++;
    }
    for(int i = 1; i < N; i++)
    {
	xs[now] = i;
	ys[now] = 0;
	now++;
    }
    for(int i = 1
    */

    sort(ps, ps + N * N);

    total = 1;
    for(int i = 0; i < N * N; i++)
	if(!i || ps[i] != num[total - 1])
	{
	    num[total] = ps[i];
	    rs[total] = 1;
	    total++;
	}
	else
	    rs[total - 1]++;

    p2[0] = 1;
    for(int i = 1; i < total; i++)
    {
	if(i)
	    p1[i] = i - 1;
	p2[i] = i + 1;
    }
    p2[total - 1] = 0;

    go(0, 0, 0);
    int v = 0;
    for(int i = 0; i < N; i++)
	v += board[0][i];
	printf("%d\n", v);
	for(int i = 0; i < N; i++, printf("\n"))
	    for(int j = 0; j < N; j++)
	    {
		if(j)
		    printf(" ");
		printf("%d", board[i][j]);
	    }

    return 0;
}

#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <string.h>
#include <math.h>
#include <fstream>
using namespace std;
#define fon(i,n) for(i=0;i<n;++i)
#define forr(i,n) for(i=n-1;i>=0;--i)
#define re return
#define ll long long
const double EPS = 1E-9;
const int INF = 1000000000;
const ll INF64 = (ll)1E18;
const double PI = 3.1415926535897932384626433832795;

ll n,m,h;

typedef map<ll,ll>tmap;
typedef set<ll>tset;
set<ll>::iterator it,itt,itlow,itup;
set<ll>::reverse_iterator irev;

tmap ids;

int main()
{
	#ifdef _DINARISIO
	ifstream cin("input.txt");
	ofstream cout("output.txt");
	#endif

	ll i,n,n2,k,j,a[20];
	ll s=0,s2,s1;

	cin>>n;n2=n*n;
	fon(i,n2){cin>>a[i];s+=a[i];}
	s/=n;
	sort(a,a+n2);
	do{
		/*if(a[0]==1&&a[1]==0&&a[2]==-1&&
			a[3]==-2&&a[4]==0&&a[5]==2&&
			a[6]==1&&a[7]==0&&a[8]==-1)
		{
			s1=9;
		}*/
		fon(j,n){
			s2=0;
			s1=0;
			fon(k,n)s2+=a[j*n+k];
			fon(k,n)s1+=a[k*n+j];
			if(s2!=s)j=n+10;
			if(s1!=s)j=n+10;
		}if(s2==s&&s1==s){
			s2=0;fon(j,n)
				s2+=a[j*n+j];
			s1=0;fon(j,n)
				s1+=a[(n-j-1)*n+j];
			if(s2==s&&s1==s2)
			{
				cout<<s<<endl;
				fon(j,n){
					fon(k,n)cout<<a[j*n+k]<<" ";
					cout<<endl;
				}
				re 0;
			}
		}
	}while ( next_permutation (a,a+n2) );
	re 0;
}

#include <iostream>
#include <iomanip>
#include <fstream>

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#include <sstream>
#include <string>

#include <bitset>
#include <deque>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>

#include <algorithm>

#include <utility>

using namespace std;

const int inf = 2000000000;
const long long linf = 9000000000000000000LL;
const double finf = 1.0e18;
const double eps = 1.0e-8;

int n, N,a[20], s, A[4][4];
bool b[20];

bool dfs(int x, int y) {
    if (y==n-1) {
        y=0;
        x++;
    }
    if (x==n-1) {
        int tmp = 0;
        for (int i=0; i<n; i++)
            tmp += a[A[i][n-1-i]];
        return tmp==s;
    }
    for (int i=0; i<N; i++) {
        if (b[i]) {
            b[i] = false;
            A[x][y] = i;
            bool flag = true;
            if (y==n-2) {
                int tmp = 0;
                for (int k=0; k<=y; k++) {
                    tmp += a[A[x][k]];
                }
                tmp = s-tmp;
                A[x][n-1] = -1;
                for (int k=0; k<N; k++) {
                    if ((a[k]==tmp)&&(b[k])) {
                        b[k] = false;
                        A[x][n-1] = k;
                        break;
                    }
                }
                if (A[x][n-1]==-1)
                    flag = false;
            }
            if (x==n-2) {
                int tmp = 0;
                for (int k=0; k<=x; k++) {
                    tmp += a[A[k][y]];
                }
                tmp = s-tmp;
                A[n-1][y] = -1;
                for (int k=0; k<N; k++) {
                    if ((a[k]==tmp)&&(b[k])) {
                        b[k] = false;
                        A[n-1][y] = k;
                        break;
                    }
                }
                if (A[n-1][y]==-1)
                    flag = false;
            }
            if ((x==n-2)&&(y==n-2)) {
                int tmp = 0;
                for (int k=0; k<=x; k++) {
                    tmp += a[A[k][k]];
                }
                tmp = s-tmp;
                A[n-1][n-1] = -1;
                for (int k=0; k<N; k++) {
                    if ((a[k]==tmp)&&(b[k])) {
                        b[k] = false;
                        A[n-1][n-1] = k;
                        break;
                    }
                }
                if (A[n-1][n-1]==-1)
                    flag = false;
            }
            if (flag)
                if (dfs(x, y+1))
                    return true;
            b[i] = true;
            if ((y==n-2)&&(A[x][n-1]!=-1)) {
                b[A[x][n-1]] = true;
            }
            if ((x==n-2)&&(A[n-1][y]!=-1)) {
                b[A[n-1][y]] = true;
            }
            if ((x==n-2)&&(y==n-2)&&(A[n-1][n-1]!=-1)) {
                b[A[n-1][n-1]] = true;
            }
        }
    }
    return false;
}

int main() {
    scanf("%d",&n);
    N = n*n;
    s = 0;
    for (int i=0; i<N; i++) {
        scanf("%d",&a[i]);
        s += a[i];
    }
    s /= n;
if (n==1) {
    printf("%d\n", s);
    printf("%d\n", a[0]);
    return 0;
}
    memset(b, true,sizeof(b));
    dfs(0, 0);
    printf("%d\n",s);
    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
            if (j) printf(" ");
            printf("%d",a[A[i][j]]);
        }
        printf("\n");
    }

    return 0;
}

#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <set>

#define mp make_pair
#define x first
#define y second

using namespace std ;

typedef long long LL ;

const int INF = 1000000000 ;

int n, s ;
int q[20], path[4][4], pow2[1 << 20] ;
set<int>hash ;

int lowbit (int x)
{
    return x & (-x) ;
}

bool check (int x, int y)
{
    if (y == n - 1)
    {
        int ss = 0 ;
        for (int i = 0; i < n; i ++ ) ss += path[x][i] ;
        if (ss != s) return 0 ;
    }
    if (x == n - 1)
    {
        int ss = 0 ;
        for (int i = 0; i < n; i ++ ) ss += path[i][y] ;
        if (ss != s) return 0 ;
    }
    if (x == n - 1 && y == 0)
    {
        int ss = 0 ;
        for (int i = 0; i < n; i ++ ) ss += path[i][n - i - 1] ;
        if (ss != s) return 0 ;
    }
    if (x == n - 1 && y == n - 1)
    {
        int ss = 0 ;
        for (int i = 0; i < n; i ++ ) ss += path[i][i] ;
        if (ss != s) return 0 ;
    }
    if (x == n - 2)
    {
        int ss = 0 ;
        for (int i = 0; i <= x; i ++ ) ss += path[i][y] ;
        if (!hash.count (ss)) return 0 ;
    }
    return 1 ;
}

bool dfs (int x, int y, int state)
{
    if (y == n) x ++, y = 0 ;
    if (x == n) return 1 ;
    for (int i = state, t, r = INF; i; i -= lowbit (i))
    {
        t = lowbit (i) ;
        if (r == q[pow2[t]]) continue ;
        r = q[pow2[t]] ;
        path[x][y] = q[pow2[t]] ;
        if (check (x, y) && dfs (x, y + 1, state - t)) return 1 ;
    }
    return 0 ;
}

int main ()
{
    for (int i = 0; i < 18; i ++ ) pow2[1 << i] = i ;
    scanf ("%d", &n) ;
    for (int i = 0; i < n * n; i ++ ) scanf ("%d", &q[i]), s += q[i] ;
    sort (q, q + n * n) ;
    s /= n ;
    for (int i = 0; i < n * n; i ++ ) hash.insert (s - q[i]) ;
    dfs (0, 0, (1 << n * n) - 1) ;
    printf ("%d\n", s) ;
    for (int i = 0; i < n; i ++ )
    {
        for (int j = 0; j < n; j ++ )
            printf ("%d ", path[i][j]) ;
        puts ("") ;
    }
    return 0 ;
}

#include <stdio.h>
#include <sstream>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <memory.h>
#include <queue>
#include <string>
#include <string.h>
#include <cmath>
#include <utility>
#include <bitset>
#include <time.h>
#include <climits>

#define PI 3.1415926535897932384626433832795
#define sqr(x) ((x)*(x))
#define OUT_RT cerr << (float(clock()) / CLOCKS_PER_SEC) << endl

typedef int LL;
typedef unsigned long long ULL;

using namespace std;

LL a[22], s1[3], s2[3], s3, s4, b[22], ss = 0;
int n, nn, c[16], sc[16];

int main() {
//	freopen("input.txt","r",stdin);
//	freopen("output.txt","w",stdout);

	cin >> n;
	nn = n * n;
	for (int i = 0; i < nn; i++) {
		cin >> a[i];
		ss += a[i];
	}
	LL s = ss / 4;

	if (n <= 3) {
		sort(a, a + nn);
		do {
			memset(s1, 0, sizeof(s1));
			memset(s2, 0, sizeof(s2));
			s3 = s4 = 0;

			for (int i = 0; i < nn; i++) {
				s1[i / n] += a[i];
				s2[i % n] += a[i];
			}
			for (int i = 0; i < nn; i += n + 1)
				s3 += a[i];
			for (int i = nn - n, j = 0; j < n; i -= n - 1, j++)
				s4 += a[i];

			bool good = s3 == s4;
			for (int i = 1; i < n; i++) if (s1[i] != s1[0]) good = false;
			for (int i = 1; i < n; i++) if (s2[i] != s2[0]) good = false;
			good &= s3 == s1[0] && s3 == s2[0];

			if (good) {
				cout << s3 << endl;
				for (int i = 0; i  < nn; i++) {
					if (i % n == n - 1) {
						cout << a[i] << endl;
					}else
						cout << a[i] << " ";
				}
				return 0;
			}
		} while(next_permutation(a, a + nn));
	} else {
		int cn = 0;
		sort(a, a + nn);
		for (int i = 0; i < nn; i++)
			if(i == 0 || a[i] != a[i-1]) {
				b[cn] = a[i];
				c[cn++] = 1;
			} else
				c[cn - 1]++;

		for (int a11 = 0; a11 < cn; a11++) if (c[a11]) {
		c[a11]--;
		for (int a12 = 0; a12 < cn; a12++) if (c[a12]) {
		c[a12]--;
		for (int a13 = 0; a13 < cn; a13++) if (c[a13]) {
		c[a13]--;

		LL b14 = s - b[a11] - b[a12] - b[a13];
		int j0 = 0;
		for(;j0 < cn; j0++) if (b[j0] == b14) break;
		if (j0 == cn || c[j0] == 0) {
			c[a13]++;
			continue;
		}
		c[j0]--;

		for (int a21 = 0; a21 < cn; a21++) if (c[a21]) {
		c[a21]--;
		for (int a22 = 0; a22 < cn; a22++) if (c[a22]) {
		c[a22]--;
		for (int a23 = 0; a23 < cn; a23++) if (c[a23]) {
		c[a23]--;

		LL b24 = s - b[a21] - b[a23] - b[a22];
		int j1 = 0;
		for (; j1 < cn; j1++) if (b[j1] == b24) break;
		if (j1 == cn || c[j1] == 0){
			c[a23]++;
			continue;
		}
		c[j1]--;

		for (int a32 = 0; a32 < cn; a32++) if (c[a32]) {
			c[a32]--;

			LL b42 = s - b[a12] - b[a22] - b[a32];
			int j2 = 0;
			for (; j2 < cn; j2++) if (b[j2] == b42) break;
			if (j2 == cn || c[j2] == 0){
				c[a32]++;
				continue;
			}
			c[j2]--;

			for (int a33 = 0; a33 < cn; a33++) if (c[a33]) {
				c[a33]--;

				LL b43 = s - b[a13] - b[a23] - b[a33];
				LL b44 = s - b[a11] - b[a22] - b[a33];
				LL b41 = s - b42 - b43 - b44;
				if (b41 != s - b14 - b[a23] - b[a32]) {
					c[a33]++;
					continue;
				}
				LL b31 = s - b[a11] - b[a21] - b41;
				LL b34 = s - b[a32] - b31 - b[a33];

				memcpy(sc, c, sizeof(c));

				int j3;
				for (j3 = 0; j3 < cn; j3++) if (b[j3] == b43 && c[j3]) {
					c[j3]--;
					break;
				}
				if (j3 < cn) {
				for (j3 = 0; j3 < cn; j3++) if (b[j3] == b44 && c[j3]) {
					c[j3]--;
					break;
				}
				if (j3 < cn) {
				for (j3 = 0; j3 < cn; j3++) if (b[j3] == b41 && c[j3]) {
					c[j3]--;
					break;
				}
				if (j3 < cn) {
				for (j3 = 0; j3 < cn; j3++) if (b[j3] == b31 && c[j3]) {
					c[j3]--;
					break;
				}
				if (j3 < cn) {
				for (j3 = 0; j3 < cn; j3++) if (b[j3] == b34 && c[j3]) {
					c[j3]--;
					break;
				}
				if (j3 < cn) {
					cout << s << endl;
					cout << b[a11] << " " << b[a12] << " " << b[a13] << " " << b14 << endl;
					cout << b[a21] << " " << b[a22] << " " << b[a23] << " " << b24 << endl;
					cout << b31 << " " << b[a32] << " " << b[a33] << " " << b34 << endl;
					cout << b41 << " " << b42 << " " << b43 << " " << b44 << endl;
					return 0;
				}
				}
				}
				}
				}

				memcpy(c, sc, sizeof(c));
				c[a33]++;
			}

			c[j2]++;
			c[a32]++;
		}

		c[j1]++;
		c[a23]++;
		}

		c[a22]++;
		}

		c[a21]++;
		}

		c[j0]++;
		c[a13]++;
		}

		c[a12]++;
		}
		c[a11]++;
		}
	}

	return 0;
}

//Kaori Miyazono, did I reach you ?
//"I can't die because... I'm the one who will protect you." -Asuna Yuuki

#include <bits/stdc++.h>
using namespace std;

int ma[5][5];

int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	int n;
	cin >> n;
	vector<int> nums(n * n);
	vector<int> norm(n * n);
	for(int i = 0; i < n * n; i++) {
		cin >> nums[i];
		norm[i] = i;
	}
	int k = n * n;
	int mn = 10;
	do {
		vector<int> cur(n * n);
		for(int i = 0; i < n * n; i++) {
			cur[i] = nums[norm[i]];
		}
		int on = 0;
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < n; j++) {
				ma[i][j] = cur[on++];
			}
		}
		set<long long int> all;
		for(int i = 0; i < n; i++) {
			long long int sum = 0;
			for(int j = 0; j < n; j++) sum += ma[i][j];
			all.insert(sum);
			sum = 0;
			for(int j = 0; j < n; j++) sum += ma[j][i];
			all.insert(sum);
		}
		long long int d1 = 0, d2 = 0;
		for(int i = 0; i < n; i++) {
			d1 += ma[i][i];
			d2 += ma[i][n - i - 1];
		}
		all.insert(d1);
		all.insert(d2);
		mn = min(mn, (int) all.size());
		if ((int) all.size() == 1) {
			cout << *(all.begin()) << '\n';
			for(int i = 0; i < n; i++) {
				for(int j = 0; j < n; j++) {
					cout << ma[i][j] << ' ';
				}
				cout << '\n';
			}
			return 0;
		}
		/*for(int i = 0; i < n * n; i++) cout << cur[i] << ' ';
		cout << '\n';*/
	} while(next_permutation(norm.begin(), norm.end()));
	//cout << mn << '\n';
	return 0;
}

#include<iostream>
//#include <bits/stdc++.h>
using namespace std;
#include<algorithm>
#include<map>
#include<vector>
#include <iomanip>
#include <set>
#include<cmath>
#define F first
#define S second
#define pii pair<int,int>
#define pb push_back
#define int long long
#define mp make_pair
//setprecision
int inf=1000*1000*1000+10;
int const MAX=1000*100+10;
int const MAX2=1000*1000+10;
int const MAX3=5000+10;
map<int,int>t;
vector<int>in;
int lim;
int c=1,r=1;
int sum[10][10];
int sq[10][10];
map<int,int>t2;
map<int,int>t3;
int a3[MAX];
bool mark3[MAX];
int s3;
int ind3[10][10];
vector<int>ans3;
int sum13[10];
int sum23[10];
int sumd3;
int sumd23;
int r3;
void output()
{
    cout<<sum13[1]<<endl;
    for(int i=1;i<=s3;i++)
    {
        for(int j=1;j<=s3;j++)
        {
            cout<<ind3[i][j]<<" ";
        }
        cout<<endl;
    }
    exit(0);
}
void check2(vector<int>a)
{
    int x=1;
    int y=1;
    bool tmp=true;
    for(int i=0;i<ans3.size();i++)
    {
        ind3[x][y]=ans3[i];
        sum13[x]+=ind3[x][y];
        sum23[y]+=ind3[x][y];
        if(x==y)
            sumd3+=ind3[x][y];
        if(x+y==s3+1)
            sumd23+=ind3[x][y];
        y++;
        if(y>r3)
            y=1,x++;
    }
    int n=s3;
    for(int i=1;i<=n;i++)
    {
        if(sum13[i]!=sum13[1] || sum23[i]!=sum13[1])
            tmp=false;
    }
    if(sum13[1]!=sumd3 || sum13[1]!=sumd23)
        tmp=false;
    if(tmp==true)
    {
        output();
    }
    for(int i=1;i<=s3;i++)
        sum13[i]=0,sum23[i]=0;
    sumd3=0;
    sumd23=0;
}
void jay(int n)
{
    //cerr<<n<<" ";3
    if(n==0)
    {
        s3=sqrt(s3);
        check2(ans3);
        s3*=s3;
    }
    for(int i=1;i<=s3;i++)
    {
        int t4=1;
        if(!mark3[i])
            mark3[i]=1,ans3.pb(a3[i]),t4=0,jay(n-1);
        if(t4==0)
            mark3[i]=0,ans3.pop_back();
    }
}
void check()
{
    for(int i=1;i<=3;i++)
    {
        sq[i][4]=lim-sum[1][i];
        sq[4][i]=lim-sum[2][i];
        if(t[sq[i][4]]==0 || t[sq[4][i]]==0)
            return;
    }
    sq[4][4]=lim-sq[4][1]-sq[4][2]-sq[4][3];
    if(t[sq[4][4]]==0)
        return;
    for(int i=1;i<=4;i++)
    {
        for(int j=1;j<=4;j++)
        {
            t2[sq[i][j]]++;
            if(t2[sq[i][j]]>t3[sq[i][j]])
                return;
        }
    }
    int a=sq[1][4]+sq[2][4]+sq[3][4]+sq[4][4];
    int d=sq[4][1]+sq[4][2]+sq[4][3]+sq[4][4];
    int b=0;
    for(int i=1;i<=4;i++)
        b+=sq[i][i];
    c=sq[1][4]+sq[2][3]+sq[3][2]+sq[4][1];
    if(c!=lim || b!=lim || a!=lim || d!=lim)
        return;
    cout<<lim<<endl;
    for(int i=1;i<=4;i++)
    {
        for(int j=1;j<=4;j++)
        {
            cout<<sq[i][j]<<" ";
        }
        cout<<endl;
    }
    exit(0);
}
void pick(int lvl)
{
    if(lvl==10)
    {
        check();
        for(int i=0;i<in.size();i++)
            t2[in[i]]=0;
        return;
    }
    for(int i=0;i<in.size();i++)
    {
        int tmp=in[i];
        if(t[tmp]!=0){
            sum[1][r]+=tmp;
            sum[2][c]+=tmp;
            t[tmp]--,sq[r][c]=tmp;
            //change(&x,&y);
            int lr=r;
            int lc=c;
            r++;
            if(r==4)
                c++,r=1;
            pick(lvl+1);
            c=lc,r=lr;
            t[tmp]++;
            sum[1][r]-=tmp;
            sum[2][c]-=tmp;
        }
    }
}
main(){
	ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
	int n;
	cin>>n;
    lim=0;
    if(n!=4)
    {
        for(int i=1;i<=n*n;i++)
        {
            cin>>a3[i];
        }
        r3=n;
        s3=n;
        s3*=n;
        jay(s3);
        return 0;
    }
	for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            int x;
            cin>>x;
            lim+=x;
            if(t[x]==0)
                in.pb(x);
            t[x]++;
            t3[x]++;
        }
    }
    random_shuffle(in.begin(),in.end());
    random_shuffle(in.begin(),in.end());
    lim/=n;
    pick(1);
    cout<<-1;
}

#include <cstdio>
#include <cstdlib>
#include <map>

using namespace std;

const int N = 20;

int i, j, n, m, sum;
int a[N], b[N][N], sv[N], st[N], ans[4][4];
int diag1, diag2, Mn, Mx;
bool l[N];
map<int, int> M;
int dif, f[N][2];

void check(){
    if (diag1 != sum) return;
    if (sv[n - 1] != st[n - 1] || sv[n - 1] != sum)
        return;
    for (i = 0; i < n; i++){
        printf("%d", ans[i][0]);
        for (j = 1; j < n; j++)
            printf(" %Id", ans[i][j]);
        printf("\n");
    }
    exit(0);
}

void find(int v, int u){
    if (u >= n){
        find(v + 1, 0);
        return;
    }
    if (v == n){
        check();
        return;
    }

    if (sv[v] + Mx * (n - u) < sum || sv[v] + (n - u)  * Mn > sum)
        return;

    if (st[u] + Mx * (n - v)  < sum || st[u] + Mn * (n - v)  > sum)
        return;

    if (v > 0 && sv[v - 1] != sum)
        return;
    if (v == n - 1 && u > 0){
        if (diag2 != sum)
            return;
        if (st[u - 1] != sum)
            return;
    }

    for (int w = 0; w < dif; w++){
        if (!f[w][1]) continue;
        ans[v][u] = f[w][0];
        sv[v] += f[w][0];
        st[u] += f[w][0];
        diag1 += (v == u) * f[w][0];
        diag2 += (v + u == n - 1) * f[w][0];
        f[w][1] --;
        if (v < n * n ) find(v, u  + 1);
        sv[v] -= f[w][0];
        st[u] -= f[w][0];
        diag1 -= (v == u) * f[w][0];
        diag2 -= (v + u == n - 1) * f[w][0];
        f[w][1] ++;
    }
}

int main(){
    scanf("%d",&n);
    Mn = 1000000000;
    Mx = -1000000000;
    for (i = 0; i < n * n; i++){
        scanf("%d",&a[i]);
        M[a[i]] ++;
        sum += a[i];
        Mn = min(Mn, a[i]);
        Mx = max(Mx, a[i]);

    }
    sum /= n;
    dif = M.size();
    i = 0;
    for (map<int,int>::iterator it = M.begin(); it != M.end(); it++){
        f[i][0] = it->first;
        f[i][1] = it->second;
        i++;
    }
    printf("%d\n", sum);
    find(0, 0);
    return 0;
}

/**                 Be name Khoda                   **/
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <algorithm>
#include <bitset>
#include <limits>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <memory.h>
#include <ctime>
#include <cassert>
using namespace std;

#define ll long long
#define un unsigned
#define IT iterator
#define VAL(x) #x << " = " << x << "   "
#define SQR(a) ((a) * (a))
#define SZ(x) ((int) x.size())
#define ALL(x) x.begin(), x.end()
#define CLR(x, a) memset(x, a, sizeof x)
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define SAL cout << "Salam!\n"
#define MAXN 1000
//#define cout fout
//#define cin fin

ll n, avg, a[10];

vector<vector<ll> > ans;

bool check()
{
    ll sum;
    for (int i = 0; i < n; i ++)
    {
        sum = 0;
        for (int j = 0; j < n; j ++)
            sum += ans[j][i];
        if (sum != avg)
            return false;
    }
    sum = 0;
    for (int i = 0; i < n; i ++)
        sum += ans[i][i];
    if (sum != avg)
        return false;
    sum = 0;
    for (int i = 0; i < n; i ++)
        sum += ans[i][(n - 1) - i];
    if (sum != avg)
        return false;
    else
        return true;
}

bool b = false;

void solve(int x)
{
    if (b)
        return ;
    if (x == n)
    {
        if (check())
        {
            b = true;
            for (int i = 0; i < n; i ++, cout << endl)
                for (int j = 0; j < n; j ++)
                    cout << ans[i][j] << " ";
        }
        return ;
    }
    for (int i = 0; i < n; i ++)
        for (int j = 0; j < n; j ++)
        {
            solve(x + 1);
            swap(ans[x][i], ans[x][j]);
        }
}

int main ()
{
    cin >> n;
    ll sum = 0;
    for (int i = 0; i < SQR(n); i ++)
    {
        cin >> a[i];
        sum += a[i];
    }
    avg = sum / n;
    //cout << sum << endl;
    cout << avg << endl;
    bool mark[10];
    CLR(mark, false);
    if (n == 1)
    {
        //cout << a[0] << endl;
        cout << a[0] << endl;
        return 0;
    }
    if (n == 2)
    {
        for (int i = 0; i < SQR(n) - 2; i ++)
            for (int j = 0; j < SQR(n) - 1; j ++)
                if (a[i] + a[j] == avg && !mark[i] && !mark[j])
                {
                    vector<ll> cur;
                    cur.pb(a[i]), cur.pb(a[j]);
                    ans.pb(cur);
                    mark[i] = mark[j] = true;
                }
    }
    else
    {
        for (int i = 0; i < SQR(n) - 2; i ++)
            for (int j = i + 1; j < SQR(n) - 1; j ++)
                for (int k = j + 1; k < SQR(n); k ++)
                    if (a[i] + a[j] + a[k] == avg && !mark[i] && !mark[j] && !mark[k])
                    {
                        vector<ll> cur;
                        cur.pb(a[i]), cur.pb(a[j]), cur.pb(a[k]);
                        ans.pb(cur);
                        mark[i] = mark[j] = mark[k] = true;
                    }
    }
    /*for (int i = 0; i < n; i ++, cout << endl)
        for (int j = 0; j < n; j ++)
            cout << ans[i][j] << " ";*/
    solve(0);
    if (!b)
    {
        swap(ans[0], ans[1]);
        solve(0);
    }
    if (!b && n == 3)
    {
        swap(ans[0], ans[1]);
        swap(ans[1], ans[2]);
        solve(0);
    }
    if (!b && n == 3)
    {
        swap(ans[1], ans[2]);
        swap(ans[0], ans[2]);
        solve(0);
    }
    return 0;
}

#include <cstdio>
#include <cstring>

int n;
int a[10];
bool mk[10];
int d[3][3];

bool check() {
	int i, j, s=0, t1=0, t2;
	for (i=0; i < n; i++) {
		s += d[i][i];
	}
	for (i=0; i < n; i++) {
		t1 += d[n-1-i][i];
	}
	if ( s != t1 )
		return false;
	for (i=0; i < n; i++) {
		t1 = t2 = 0;
		for (j=0; j < n; j++) {
			t1 += d[i][j];
			t2 += d[j][i];
		}
		if ( t1 != s || t2 != s )
			return false;
	}
	return true;
}
bool dfs(int i, int j) {
	if (i == n) {
		return check();
	}
	if (j == n) {
		return dfs(i+1, 0);
	}
	int k;
	for (k=0; k < n*n; k++) if (!mk[k]) {
		mk[k] = true;
		d[i][j] = a[k];
		if ( dfs(i, j+1) )
			return true;
		mk[k] = false;
	}
	return false;
}
int main() {
	int i, j;
	scanf("%d",&n);
	for (i=0; i < n*n; i++) {
		scanf("%d",&a[i]);
	}
	dfs(0, 0);
	int s = 0;
	for (i=0; i < n; i++) {
		s += d[0][i];
	}
	printf("%d\n",s);
	for (i=0; i < n; i++) {
		for (j=0; j < n; j++) {
			printf("%d",d[i][j]);
			printf(j<n-1 ? " " : "\n");
		}
	}
	return 0;
}

#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
#include <random>
#include <cassert>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MEM(a,b) memset((a),(b),sizeof(a))
const LL INF = 1e9 + 7;
const int N = 5e2 + 10;
int a[4][4];
void check(int n, int key)
{
	int sum1[4] = { 0,0,0,0 };
	int sum2[4] = { 0,0,0,0 };
	LL tot = 0;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			sum1[i] += a[i][j];
			sum2[j] += a[i][j];
		}
	}
	for (int i = 0; i < n; i++)
	{
		if (sum1[i] != key) return;
		if (sum2[i] != key) return;
	}
	int x = 0, y = 0;
	for (int i = 0; i < n; i++) x += a[i][i], y += a[i][n - i - 1];
	if (x != key) return;
	if (y != key) return;
	cout << key << endl;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			printf("%d%c", a[i][j], " \n"[j == n - 1]);
		}
	}
	exit(0);
}
void change(vector<int>& v, int n)
{
	int o = 0;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			a[i][j] = v[o++];
		}
	}
}
void solve3(int n)
{
	vector<int> v(n * n);
	LL tot = 0;
	for (auto& x : v) cin >> x, tot += x;
	tot /= n;
	sort(v.begin(), v.end());
	do
	{
		change(v, n);
		check(n, tot);
	} while (next_permutation(v.begin(), v.end()));
}
inline void del(map<int, int>& mc, int x)
{
	if (mc[x]-- == 1) mc.erase(x);
}
inline void add(map<int, int>& mc, int x)
{
	mc[x]++;
}
vector<pair<int, int>> vp = { {0,2},{1,2},{2,2},{2,1} };
vector<vector<int>> qr[10];
/*
1 1 1 1
1 1 1 1
0 0 1 0
0 0 0 0
*/
void init()
{
	qr[0].emplace_back(vector<int>{0, 3, 0, 0, 0, 1, 0, 2});
	qr[1].emplace_back(vector<int>{1, 3, 1, 0, 1, 1, 1, 2});
	qr[2].emplace_back(vector<int>{3, 2, 0, 2, 1, 2, 2, 2});
	qr[2].emplace_back(vector<int>{3, 3, 0, 0, 1, 1, 2, 2});
	qr[2].emplace_back(vector<int>{2, 3, 0, 3, 1, 3, 3, 3});
	qr[3].emplace_back(vector<int>{2, 0, 2, 1, 2, 2, 2, 3});
	qr[3].emplace_back(vector<int>{3, 0, 0, 0, 1, 0, 2, 0});
	qr[3].emplace_back(vector<int>{3, 1, 0, 1, 1, 1, 2, 1});
}
bool check(vector<pair<int, int>>& vp, int cur, int key)
{
	map<int, int> mc;
	for (auto& p : vp)
		if (p.second)
			mc.insert(p);
	for (auto& v : qr[cur])
	{
		int rest = key;
		for (int i = 2; i < v.size(); i += 2)
		{
			rest -= a[v[i]][v[i + 1]];
		}
		if (!mc.count(rest)) return false;
		del(mc, rest);
		a[v[0]][v[1]] = rest;
	}
	vp.clear();
	for (auto& p : mc)
		vp.push_back(p);
	return true;
}
void dfs(int cur, vector<pair<int, int>>& vp, int key)
{
	if (cur == 4)
	{
		check(4, key);
		return;
	}
	int x, y;
	tie(x, y) = ::vp[cur];
	for (int i = 0; i < vp.size(); i++)
	{
		if (vp[i].second == 0) continue;
		auto vv = vp;
		vv[i].second--;
		a[x][y] = vv[i].first;
		if (!check(vv, cur, key)) continue;
		dfs(cur + 1, vv, key);
	}
}
void solve4(vector<int> v, map<int, int> mc, int tot)
{
	for (auto& x : v)
		del(mc, x);
	vector<pair<int, int>> vp;
	for (auto& p : mc)
		vp.push_back(p);
	sort(v.begin(), v.end());
	do
	{
		change(v, 2);
		dfs(0, vp, tot);
	} while (next_permutation(v.begin(), v.end()));
}
int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int n;
	cin >> n;
	if (n <= 3) solve3(n);
	init();
	vector<int> v(n * n);
	LL tot = 0;
	map<int, int> mc;
	for (auto& x : v) cin >> x, tot += x, mc[x]++;
	sort(v.begin(), v.end());
	for (int i = 1; i < n * n; i++)
	{
		for (int j = i + 1; j < n * n; j++)
		{
			for (int k = j + 1; k < n * n; k++)
			{
				vector<int> tmp = { v[0],v[i],v[j],v[k] };
				//cout << i << ' ' << j << ' ' << k << endl;
				solve4(tmp, mc, tot / 4);
			}
		}
	}
	return 0;
}

//God & me
#include <bits/stdc++.h>
#define pb push_back
#define X first
#define Y second
//#define int long long
using namespace std;
template<class T,class L>bool smax(T&x,L y){return x<y?(x=y,1):0;}template<class T,class L>bool smin(T&x,L y){return x>y?(x=y,1):0;}
typedef pair<int,int> pii;

const int maxn=16;
int n,a[maxn],b[maxn][maxn],ex,x[maxn];
bool mark[maxn];
vector<vector<int> >all;
vector<int>todo;
int fond(int x){
  for(int i=0;i<n*n;i++)
    if(!mark[i] && a[i]==x)
      return i;
  return -1;
}
void back(int s=0){
  if(s==n){
    for(int i=0,sum=0;i<n;i++,sum=0){
      for(int j=0;j<n;j++)
	sum+=b[j][i];
      if(sum!=ex)return;
    }
    {
      int sum=0;
      for(int i=0;i<n;i++)
	sum+=b[i][i];
      if(sum!=ex)return;
    }
    {
      int sum=0;
      for(int i=0;i<n;i++)
	sum+=b[i][n-i-1];
      if(sum!=ex)return;
    }
    for(int i=0;i<n;i++,cout<<'\n')
      for(int j=0;j<n;j++)
	cout<<b[i][j]<<' ';
    exit(0);
  }
  if(s==n-1){
    fill(b[n-1],b[n-1]+n,ex);
    for(int i=0;i<n-1;i++)
      for(int j=0;j<n;j++)
	b[n-1][j]-=b[i][j];
    int i=0;
    for(int tmp;i<n;i++)
      if(~(tmp=fond(b[n-1][i])))
	mark[tmp]=1,todo.pb(tmp);
      else
	break;
    if(i==n)
      back(n);
    for(auto x:todo)mark[x]=0;
    todo.clear();
  }
  for(auto v:all){
    bool reval=1;
    for(auto x:v)
      if(mark[x]){reval=0;break;}
    if(!reval)continue;
    do{
      for(int i=0;i<n;i++)
	b[s][i]=a[v[i]],mark[v[i]]=1;
      back(s+1);
      for(int i=0;i<n;i++)
	mark[v[i]]=0;
    }while(next_permutation(v.begin(),v.end()));
  }
}
void tof(int idx){
  if(idx==4 && x[0]+x[1]+x[2]+x[3]!=ex)return;
  if(idx==8 && x[4]+x[5]+x[6]+x[7]!=ex)return;
  if(idx==12 && x[8]+x[9]+x[10]+x[11]!=ex)return;
  if(idx==16 && x[12]+x[13]+x[14]+x[15]!=ex)return;
  if(idx==13 && x[0]+x[4]+x[8]+x[12]!=ex)return;
  if(idx==14 && x[1]+x[5]+x[9]+x[13]!=ex)return;
  if(idx==15 && x[2]+x[6]+x[10]+x[14]!=ex)return;
  if(idx==16 && x[3]+x[7]+x[11]+x[15]!=ex)return;
  if(idx==13 && x[3]+x[6]+x[9]+x[12]!=ex)return;
  if(idx==16 && x[0]+x[5]+x[10]+x[15]!=ex)return;
  if(idx==16){
    for(int i=0;i<n;i++,cout<<'\n')
      for(int j=0;j<n;j++)
	cout<<x[i*4+j]<<' ';
    exit(0);
  }
  for(int i=0;!(i>>4);++i)
    if(!mark[i]){
      x[idx]=a[i],mark[i]=1;
      tof(idx+1);
      mark[i]=0;
    }
}
main(){
  srand(time(0));
  ios::sync_with_stdio(0),cin.tie();
  cin>>n;
  for(int i=0;i<n*n;i++)
    cin>>a[i],ex+=a[i];
  random_shuffle(a,a+n*n);
  ex/=n;
  cout<<ex<<'\n';
  if(n<4){
    for(int i=0;i<1<<n*n;i++)
      if(__builtin_popcount(i)==n){
	int sum=0;
	for(int j=0;j<n*n;j++)
	  sum+=(i>>j&1)*a[j];
	if(sum==ex){
	  all.pb({});
	  for(int j=0;j<n*n;j++)
	    if(i>>j&1)
	      all.back().pb(j);
	  sort(all.back().begin(),all.back().end());
	}
      }
    return back(),0;
  }
  tof(0);
  return 0;
}

//sob, 2 cze 2018, 19:06:28 CEST
//Konrad Paluszek, University of Warsaw (former XIV LO Staszic)
# include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7, MN = 1e6 + 44;
const bool local =
#ifdef LOCAL
true;
#else
false;
#endif
#define f first
#define s second
#define FOR(i, b, e) for (int i = (b); i <= (int)(e); ++i)
#define FORD(i, b, e) for (int i = (b); i >= (int)(e); --i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define bvt(s, f) int s(int x){return f(x);}int s(UI x){return f(x);}int s(LL x){return f##ll(x);}int s(ULL x){return f##ll(x);}
#define FORBITS(i,m)	for(decltype(m)lP6N4Slr=(m),i=lP6N4Slr?ctz(lP6N4Slr):0;lP6N4Slr;lP6N4Slr^=((decltype(m))1<<i),i=ctz(lP6N4Slr))
#define FORDBITS(i,m) for(decltype(m)_6Im=(m),i=_6Im?8*sizeof(m)-clz(_6Im)-1:0;_6Im;_6Im^=((decltype(m))1<<i),i=8*sizeof(m)-clz(_6Im)-1)
#define SUBS(i, mask) for(decltype(mask)i = (mask),NFR9xNQCJZTrYQ=1;NFR9xNQCJZTrYQ;NFR9xNQCJZTrYQ=i,i=(i-1)&(mask))
#define PB push_back
#define MP make_pair
#define EB emplace_back
#define ymd(t) using P##t=pair<t,t>;using V##t=vector<t>;using S##t=set<t>;using T##t=pair<P##t,t>;using Q##t=pair<P##t,P##t>;
using LL=long long;using LD=long double;using VI=vector<int>;using PII=pair<int,int>;using SI=set<int>;using MII=map<int,int>;ymd(LD)
using TII=pair<PII,int>;using QII=pair<PII,PII>;using ULL=unsigned long long;ymd(ULL)ymd(VI)ymd(LL)ymd(PII)ymd(PLL)ymd(PLD)ymd(PULL)ymd(SI)
ymd(TII)ymd(TLL)ymd(TLD)ymd(QII)ymd(QLL)ymd(QLD)ymd(QULL)ymd(TULL)using UI=unsigned int;ymd(UI)ymd(PUI)ymd(VUI)ymd(TUI)ymd(QUI)ymd(VVI)
bvt(popc,__builtin_popcount)bvt(ctz,__builtin_ctz)bvt(clz,__builtin_clz)bvt(bit_parity,__builtin_parity)ymd(SLL)ymd(VLL)ymd(VULL)ymd(SULL)
#define siz(r) ((int)r.size())
#define ALL(r) r.begin(), r.end()
#define sim template <class T
#define ros return o
sim,class N>bool mini(T&o,N h){if(o>h)ros=h,1;return 0;}sim,class N>bool maxi(T&o,N h){if(o<h)ros=h,1;return 0;}
#define oor >ostream &operator<<(ostream &o,
#define urs(r) typename pta<typename remove_reference<decltype(r)>::type>::t
#define bnt ;}sim> struct
sim,class N oor pair<T,N>u){ros<<"<"<<u.f<<", "<<u.s<<">" bnt pta{using t=T;};sim,int N>struct pta<T[N]>{using t=T*;};
sim,typename=typename enable_if<!is_same<T,string>::value,urs(T().end())>::type oor
T u){int c=0;o<<"{";for(auto e:u)o<<", "+2*!c++<<e;ros<<"}";}
int mpow(int b, int e) {int o = 1; while (e) {if (e & 1)o = o * 1ll * b % MOD;b = b * 1ll * b % MOD;e /= 2;}ros;}
void dor(){cerr<<dec<<"\033[0m"<<endl;}sim,class...N>void dor(T x,N...y){cerr<<boolalpha<<x<<"; ";dor(y...);} const bool deb =
#define acv(N,O) sim> typename enable_if<is_arithmetic<T>::value, N>::type operator O(N o, T x) {ros O N(x);} \
	sim> typename enable_if<is_arithmetic<T>::value, N>::type operator O(T o, N x) {return N(o) O x;} \
	sim> typename enable_if<is_arithmetic<T>::value || is_same<T,N>::value, N&>::type operator O##=(N &o, T x) {ros = o O N(x);}
#ifdef DEB
true;
#define debug(c...)cerr<<"\033[1;"<<30+__LINE__%8<<"m["<<__FUNCTION__<<"#"<<__LINE__<<"] \033["<<30+(__LINE__/8+__LINE__*3)%8<<"m",::dor(c)
#define rcu m);}g(+) g(-) g(*) g(/) g(%) g(<<) g(>>) g(^) g(|) g(&) g(>=) g(<=) g(>) g(<) g(!=) g(==)
#define zub(h,t,n...) class N> auto operator h(n u)->nep<urs(t)> {return ngy(t,
sim>string bte(T u){stringstream o;o<<u;ros.str()bnt nep;sim>T yyo(nep<T>);sim>nep<T>ngy(T v,string c,int m=0);sim>T yyo(T o){ros bnt
nep{T v;string c;int m; nep<T>mask(){return ngy(v,c,1);}
#define g(o) template<zub(o, v o u, N) "(" + bte(*this) + ") " #o " (" + bte(u) + ")", m);}
template<zub([],v[yyo(u)],N)c+"["+bte(u)+"]",rcu};sim>nep<T>ngy(T v,string c,int m){return nep<T>{v,c,m};}
sim>T yyo(nep<T> o){ros.v;}sim>int uhn(T){return 0;}sim>int uhn(nep<T>o){ros.m;}
#define ccy(c, y) sim> class enable_if<c is_arithmetic<T>::value,string>::type ema(T u){return bte(y);}
ccy(,bitset<8*sizeof(T)>(u))ccy(!,u)sim,class N> string ema(pair<T,N> o) {return "<" + ema(o.f) + ", " + ema(o.s) + ">";}
#define g(o) sim, zub(o, yyo(p) o u.v, T p, nep<N>) "(" + bte(p) + ") " #o " (" + bte(u) + ")", uhn(p) + u.m);}
sim>string bte(nep<T>o){ros.c+" = "+(o.m?ema(o.v):bte(o.v));}sim oor nep<T>m){ros<<bte(rcu sim>nep<T>ngy(nep<T>o,string c="",int m=0){ros;}
#undef g
#define imie(a...) ngy(a, #a)
#define imask(a...) ngy(a, #a, 1)
#define range(u, g, c) ngy(list<urs(u[0])>(&u[yyo(g)], &u[yyo(c)] + 1), #u "[" + bte(g) + "..." + bte(c) + "]")
#define arr(a, i) imie(a)[imie(i)]
#define arr2(a, i, j) imie(a)[imie(i)][imie(j)]
#define arr3(a, i, j, k) imie(a)[imie(i)][imie(j)][imie(k)]
#define arr4(a, i, j, k, q) imie(a)[imie(i)][imie(j)][imie(k)][imie(q)]
vector <stringstream> lif;struct prux {prux() {lif.EB();}~prux() {lif.pop_back();}};
#define mark_stack(c...) erf(__LINE__,c)
#define erf(l,c...) prux _4IJ1MYUxZy2p_Q##l;lif.back()<<__FUNCTION__<<"#"<<l<<": ";siw(c)
void siw(){};sim, class...N> void siw(T x, N...y) {lif.back() << x << "; ";siw(y...);}
#define print_stack() cerr << "STACK_TRACE_FROM:" << __FUNCTION__ << "#" << __LINE__<<endl,nfl()
#define ckx(o) template<zub(o, o yyo(u), nep<N>) #o "(" + bte(u) + ")", u.m);}
sim> struct tqaz{T a;int u,d,l,r;};sim> tqaz<T> zaqt(T a,int u,int d,int l,int r){return tqaz<T>{a,u,d,l,r};}
#define matr(a,u,d,l,r) ngy(zaqt(a,yyo(u),yyo(d),yyo(l),yyo(r)), #a"["+bte(u)+"..."+bte(d)+"]["+bte(l)+"..."+bte(r)+"]")
#define avc(f) sim>string f(tqaz<T> x){vector<vector<string>> t;FOR(i,x.u,x.d){t.EB();FOR(j,x.l,x.r)t.back().PB(f(x.a[i][j]));}\
stringstream o;VI M;FOR(j,x.l,x.r){int m=0;FOR(i,x.u,x.d)maxi(m, siz(t[i][j]));M.PB(m);}FOR(i,x.u,x.d){o<<"\n\t["<<i<<"] ";\
FOR(j,x.l,x.r){REP(_, M[j-x.l]-siz(t[i][j]))o<<" ";o<<t[i][j]<<((j+1)%5?", ":"; ");}}ros.str() + "\n";}
void nfl() {for (auto &s : lif)cerr << "	" << s.str() << endl;} ckx(+) ckx(-) ckx(!) ckx(~) avc(bte) avc(ema)
struct zet_p {int o;LD v;zet_p(LL x):o(x%MOD),v(x){} zet_p(LL x, LD y):o(x%MOD),v(y){} int get(){ros>0?o:o+MOD;}
	zet_p operator+(zet_p a) {return zet_p(o+a.o,v+a.v);}
	zet_p operator-(zet_p a) {return zet_p(o-a.o,v-a.v);}
	zet_p operator*(zet_p a) {return zet_p(o*1ll*a.o,v*a.v);}
	zet_p operator/(zet_p a) {assert(a.o != 0); return zet_p(o*1ll*mpow(a.o, MOD-2),v/a.v);}
	bool operator==(zet_p a) {return (o-a.o)%MOD == 0;}};
ostream&operator<<(ostream &o, zet_p u) {ros<<"["<<u.o<<" @ "<<u.v<<"]";}using frac_mod = zet_p;
#else
false;
struct zet_p {int o;int get(){ros>0?o:o+MOD;}zet_p(LL x):o(x%MOD){}zet_p(int x):o(x%MOD){}
	zet_p operator+(zet_p a) {return zet_p(o+a.o);}
	zet_p operator-(zet_p a) {return zet_p(o-a.o);}
	zet_p operator*(zet_p a) {return zet_p(o*1ll*a.o);}
	zet_p operator/(zet_p a) {return zet_p(o*1ll*mpow(a.o, MOD - 2));}
	bool operator==(zet_p a) {return (o-a.o)%MOD == 0;}};
struct frac_mod {int o,m;int get(){return (o*1ll*mpow(m,MOD-2)%MOD+MOD)%MOD;}frac_mod(int x):o(x%MOD),m(1){}frac_mod(LL x):o(x%MOD),m(1){}
	frac_mod(LL a,LL b):o(a),m(b){}
	frac_mod operator+(frac_mod a){return frac_mod(o*1ll*a.m+m*1ll*a.o,m*a.m);}
	frac_mod operator-(frac_mod a){return frac_mod(o*1ll*a.m-m*1ll*a.o,m*a.m);}
	frac_mod operator*(frac_mod a){return frac_mod(o*1ll*a.o,m*1ll*a.m);}
	frac_mod operator/(frac_mod a){return frac_mod(o*1ll*a.m,m*1ll*a.o);}
	bool operator==(frac_mod a) {return (o * 1ll * a.m - m * 1ll * a.o) % MOD == 0;}};
acv(frac_mod, +) acv(frac_mod, -) acv(frac_mod, *) acv(frac_mod, /)
#define debug(...)
#define mark_stack(...)
#define print_stack(...)
#endif
acv(zet_p, +) acv(zet_p, -) acv(zet_p, *) acv(zet_p, /)
#define nok(o,c,r,l...) auto operator o(c p, r y)-> decltype(MP(l)) {return MP(l);}
#define pcg(o) sim, class S, class N, class H> nok(o, pair<S u T>, pair<H u N>, p.first o y.first, p.second o y.second)\
sim,class S,class N,class=typename enable_if<!is_base_of<ios_base,typename remove_reference<T>::type>::value>::type>nok\
(o,T,pair<S u N>, p o y.first, p o y.second) sim, class S, class N> nok(o, pair<S u N>, T, p.first o y, p.second o y)
#define clp(r) pcg(r) sim, class S, class N, class H> \
	pair <T,S> & operator r##=(pair <T,S> &p, pair<N,H> y) {p.first r##= y.first; p.second r##= y.second; return p;}\
	sim, class S, class N> pair <T,S> &operator r##=(pair<T,S> &p, N y) {p.first r##= y; p.second r##= y; return p;}
#define u ,
#define syd(o) sim, class N> auto operator o(pair<T,N> e) -> decltype(MP(o e.f, o e.s)) {return MP(o e.f, o e.s);}
clp(+) clp(-) clp(*) clp(/) clp(%) clp(^) clp(|) clp(>>) clp(<<) clp(&) pcg(&&) pcg(||) syd(-) syd(+) syd(~) syd(!)
#undef u
sim, class N> using gyv = T;
namespace std {sim, class N> struct hash<pair<T,N> > {size_t operator()(const pair<T,N>&u) const {
			return hash<T>()(u.f) * 440624741ULL ^ hash<N>()(u.s);}};
	sim> struct hash<gyv<T, decltype(((T*)0)->end())>> {size_t operator()(const T& u) const {size_t o = 505347433;
for (auto e : u)o = hash<urs(e)>()(e) ^ o * 277953755ULL;ros;}};}
MII cou;
int sum = 0;
int n;
int board[4][4];
void go(int num, int i, int j);
void backtrack(int i, int j) {
	if (i == n && j == 0) {
		REP(i, n)
			REP(j, n)
				printf("%2d%c", board[i][j], j == n - 1 ? '\n' : ' ');
		exit(0);
	}
	if (j == 3) {
		int s = sum;
		REP(x, 3)
			s -= board[i][x];
    if (i == 0 && s < board[0][0])
      return;
		go(s, i, j);
		return;
	}
	if (i == 3) {
		int s = sum;
		REP(x, 3)
			s -= board[x][j];
		go(s, i, j);
		return;
	}
	if (i == 2 && j == 1) {
		int v = board[0][0] + board[1][0] + board[2][0] - board[1][2] - board[0][3];
    if (v >= board[0][0])
      go(v, i, j);
		return;
	}
	if (i == 2 && j == 2) {
		int o = board[0][3] + board[1][3] + sum - board[2][0] - board[2][1] - board[0][0] - board[1][1];
		if (o % 2 == 0 && o / 2 >= board[0][0])
  		go(o / 2, i, j);
		return;
	}
  int limit = -1e9;
  if ((i == 0 && j == 2) || ((i == 1 || i == 2) && (j == 0)))
    limit = board[0][1];
  else if ((i == j || i + j == 3) && (i || j))
    limit = board[0][0];
	for (auto x : cou) {
    if (x.f >= limit)
  		go(x.f, i, j);
  }
}
void go(int num, int i, int j) {
	auto it = cou.find(num);
	if (it == cou.end() || it -> s == 0) return;
	it -> s--;
	board[i][j] = num;
	if (j == 3)
		backtrack(i + 1, 0);
	else
		backtrack(i, j + 1);
	it -> s++;
}
int main() {
	scanf("%d", &n);
	VI nums;
	REP(_, n * n) {
		int x;
		scanf("%d", &x);
		cou[x]++;
		sum += x;
		nums.PB(x);
	}
	assert(sum % n == 0);
	sum /= n;
	printf("%d\n", sum);
	if (n <= 3) {
		sort(ALL(nums));
		do {
			bool good = true;
			REP(i, n) {
				int s1 = 0, s2 = 0;
				REP(j, n) {
					s1 += nums[i * n + j];
					s2 += nums[j * n + i];
				}
				if (s1 != sum || s2 != sum)
					good = false;
			}
			int s1 = 0, s2 = 0;
			REP(i, n) {
				s1 += nums[i * n + i];
				s2 += nums[i * n + n - i - 1];
			}
			if (s1 != sum || s2 != sum)
				good = false;
			if (good) {
				REP(i, n * n)
					printf("%d%c", nums[i], i % n == n - 1 ? '\n' : ' ');
				return 0;
			}
		}
		while (next_permutation(ALL(nums)));
	}
	backtrack(0, 0);
}

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <iostream>
    #include <climits>
    #include <numeric>
    #define foreach(e,x) for(__typeof(x.begin()) e=x.begin();e!=x.end();++e)
    #define REP(i,n) for(int i=0;i<n;++i)
    using namespace std;

    const int MAX_N = 4;
    int n;
    int a[MAX_N * MAX_N];

    bool used[MAX_N * MAX_N];

    bool check(int x[4][4], int n) {
    	int sum = 0;
    	for (int i = 0; i < n; ++i) {
    		sum += x[0][i];
    	}

    	for (int r = 0; r < n; ++r) {
    		if (accumulate(x[r], x[r] + n, 0) != sum) {
    			return false;
    		}
    	}
    	for (int c = 0; c < n; ++c) {
    		int t = 0;
    		for (int r = 0; r < n; ++r) {
    			t += x[r][c];
    		}
    		if (t != sum)
    			return false;
    	}
    	int A = 0, B = 0;
    	for (int r = 0; r < n; ++r) {
    		A += x[r][r];
    		B += x[r][n - 1 - r];
    	}
    	if (A != sum || B != sum)
    		return false;
    	return true;
    }

    void work3() {

    	sort(a, a + n * n);
    	do {
    		int x[4][4];
    		for (int i = 0; i < n; ++i) {
    			for (int j = 0; j < n; ++j) {
    				x[i][j] = a[i * n + j];
    			}
    		}
    		int sum = 0;
    		for (int i = 0; i < n; ++i) {
    			sum += x[0][i];
    		}

    		bool ok = true;
    		for (int r = 0; r < n; ++r) {
    			if (accumulate(x[r], x[r] + n, 0) != sum) {
    				ok = false;
    				break;
    			}
    		}
    		for (int c = 0; c < n; ++c) {
    			int t = 0;
    			for (int r = 0; r < n; ++r) {
    				t += x[r][c];
    			}
    			if (t != sum)
    				ok = false;
    		}
    		int A = 0, B = 0;
    		for (int r = 0; r < n; ++r) {
    			A += x[r][r];
    			B += x[r][n - 1 - r];
    		}
    		if (A != sum || B != sum)
    			ok = false;
    		if (ok) {
    			cout << sum << endl;
    			for (int r = 0; r < n; ++r) {
    				for (int c = 0; c < n; ++c) {
    					cout << x[r][c] << " ";
    				}
    				cout << endl;
    			}
    			cout << endl;
    			return;
    		}
    	} while (next_permutation(a, a + n * n));
    }

    int main() {
    	cin >> n;
    	for (int i = 0; i < n * n; ++i) {
    		cin >> a[i];
    	}
    	if (n <= 3) {
    		work3();
    		return 0;
    	}

    	//then n=4

    	int sum = 0;
    	for (int i = 0; i < n * n; ++i) {
    		sum += a[i];
    	}
    	sum /= n;
    //	cout << sum << endl;

    	bool used[MAX_N * MAX_N] = { };
    	int tot = n * n;
    #define SEARCH(a) for(int a=0;a<tot;a++)if(!used[a])

    	SEARCH(a11) {
    		used[a11] = true;
    		SEARCH(a22) {
    			used[a22] = true;
    			SEARCH(a33) {
    				used[a33] = true;

    				int need44 = sum - (a[a11] + a[a22] + a[a33]);

    				//pick a44
    				SEARCH(a44)
    					if (a[a44] == need44) {
    						used[a44] = true;

    						SEARCH(a14) {
    							used[a14] = true;
    							SEARCH(a23) {
    								used[a23] = true;
    								SEARCH(a32) {
    									used[a32] = true;

    									int need41 = sum - (a[a14] + a[a23] + a[a32]);

    									//pick a41
    									SEARCH(a41)
    										if (a[a41] == need41) {
    											used[a41] = true;

    											SEARCH(a12) {
    												used[a12] = true;
    												//pick a42
    												int need42 = sum - (a[a12] + a[a22] + a[a32]);
    //												cout << need42 << endl;
    												SEARCH(a42)
    													if (a[a42] == need42) {
    														used[a42] = true;

    														//pick a13
    														int need13 = sum - (a[a11] + a[a12] + a[a14]);
    														SEARCH(a13)
    															if (a[a13] == need13) {
    																used[a13] = true;

    																//pick a43
    																int need43 = sum - (a[a41] + a[a42] + a[a44]);
    																SEARCH(a43)
    																	if (a[a43] == need43) {
    																		used[a43] = true;

    																		SEARCH(a21) {
    																			used[a21] = true;

    																			//pick a24
    																			int need24 = sum - (a[a21] + a[a22] + a[a23]);
    																			SEARCH(a24)
    																				if (a[a24] == need24) {
    																					used[a24] = true;

    																					//pick a31
    																					int need31 = sum - (a[a11] + a[a21] + a[a41]);

    																					SEARCH(a31)
    																						if (a[a31] == need31) {
    																							used[a31] = true;

    																							int need34 = sum - (a[a31] + a[a32] + a[a33]);
    																							SEARCH(a34)
    																								if (a[a34] == need34) {
    																									used[a34] = true;

    																									int y[4][4] = { { a11, a12, a13, a14 }, { a21, a22, a23, a24 }, { a31, a32, a33, a34 }, { a41, a42,
    																											a43, a44 } };

    																									int x[4][4];

    																									for (int r = 0; r < 4; ++r) {
    																										for (int c = 0; c < 4; ++c) {
    																											x[r][c] = a[y[r][c]];
    																										}
    																									}

    																									if (check(x, 4)) {
    																										cout << sum << endl;
    																										for (int r = 0; r < n; ++r) {
    																											for (int c = 0; c < n; ++c) {
    																												cout << x[r][c] << " ";
    																											}
    																											cout << endl;
    																										}
    																										cout << endl;
    																										return 0;
    																									}

    																									used[a34] = false;
    																									break;
    																								}
    																							used[a31] = false;
    																							break;
    																						}

    																					used[a24] = false;
    																					break;
    																				}

    																			used[a21] = false;
    																		}

    																		used[a43] = false;
    																		break;
    																	}

    																used[a13] = false;
    																break;
    															}

    														used[a42] = false;
    														break;
    													}
    												used[a12] = false;
    											}

    											used[a41] = false;
    											break;
    										}

    									used[a32] = false;
    								}
    								used[a23] = false;
    							}
    							used[a14] = false;
    						}

    						used[a44] = false;
    						break;
    					}

    				used[a33] = false;
    			}
    			used[a22] = false;
    		}
    		used[a11] = false;
    	}
    }

#define ReadFromConsole 1
#define inFile "input.txt"
#define outFile "output.txt"

#include <iostream>
#include <fstream>
#include <conio.h>
#include <vector>
#include <string>
#include <algorithm>
#include <math.h>
#include <ctype.h>
#include <map>
#include <set>

using namespace std;

#define SET(a,x) memset((a),(x),sizeof(a))
#define FOR(i,n) for( int i = 0; i <(n); i++)

typedef vector< int > vi;
typedef vector< vi > vii;
typedef vector< vii > viii;
typedef vector< int >:: iterator viit;

typedef long long int ll;
typedef unsigned int ui;

int a[20];
int b[20];
bool used[20];
int n;
int s;

bool f( int ind)
{
    if( ind == n*n )
    {
        int v[4] = {0,0,0,0};
        int h[4] = {0,0,0,0};
        int d[4] = {0,0};

        for( int i=0; i<n; i++)
        {
            for( int j=0; j<n; j++)
            {
                h[i] += b[i*n + j];
                v[j] += b[i*n + j];
            }
            d[0] += b[i*n + i];
            d[1] += b[i*n + (n-1-i)];
        }
        s = h[0];
        for( int i=0; i<n; i++)
            if( s != h[i] || s!= v[i] )
                return false;
        if( s!= d[0] || s!= d[1] )
            return false;
        return true;
    }
    else
        for( int i=0; i<n*n; i++)
            if( !used[i] )
            {
                used[i] = 1;
                b[ind] = a[i];
                if( f( ind+1) )
                    return true;
                used[i] = 0;
            }

    return false;
}

int main()
{
    if( fopen("debug.file.txt", "r") == NULL ) {
        if( !ReadFromConsole )
        {
            freopen(inFile,"r",stdin);
            freopen(outFile,"w",stdout);
        }
    }
    else {
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    }

    cin>>n;
    int sum = 0;
    FOR( i, n*n)
    {
        cin >> a[i];
        b[i] = a[i];
        sum += 2*b[i];
        used[i] = false;
    }
    sort( b, b+ n*n);

    int min, max;
    min = max = sum;

    min += b[0]*2;
    max += b[n*n-1]*2;
    for( int i=1; i<n*2-1; i++)
    {
        min += b[i];
        max += b[ n*n-1 - i];
    }

    f(0);

    cout << s << endl;
    FOR(i,n){
        FOR(j,n)
            cout << b[i*n+j] << " ";
        cout << endl;
    }

    return 0;
}

#ifdef ONLINE_JUDGE
#   define NDEBUG
#endif
#include <cstdint>
#include <cassert>
#include <iostream>
using namespace std;

namespace {

constexpr uint_fast8_t N = 4;
constexpr uint_fast32_t A = 100'000'000;

struct RC {
    uint_fast8_t r;
    uint_fast8_t c;
};

template<typename T, uint_fast8_t R, uint_fast8_t C>
struct Matrix {
    T e[R][C];

    constexpr       T &operator [](const RC rc)       noexcept { return e[rc.r][rc.c]; }
    constexpr const T &operator [](const RC rc) const noexcept { return e[rc.r][rc.c]; }
};

int_fast32_t a[N*N];
uint_fast8_t t[A+1+A] = {};
constexpr uint_fast8_t * const k = t+A;
Matrix<int_fast32_t, N, N> m;
#ifndef NDEBUG
Matrix<bool, N, N> v = {};
#endif

inline void set(const int_fast32_t u) noexcept {
    assert(k[u]);
    k[u]--;
}

inline void unset(const int_fast32_t u) noexcept {
    k[u]++;
    assert(k[u]);
}

inline void set(const RC rc, const int_fast32_t u) noexcept {
    assert(!v[rc]);
    set(u);
    m[rc] = u;
  #ifndef NDEBUG
    v[rc] = true;
  #endif
}

inline void unset(const RC rc, const int_fast32_t u) noexcept {
    assert(v[rc]);
    assert(u == m[rc]);
    unset(u);
  #ifndef NDEBUG
    v[rc] = false;
  #endif
}

static_assert(sizeof(a) + sizeof(t) + sizeof(m) <= 256'000'000);

uint_fast16_t n;
int_fast32_t s = 0;

struct Z {
    bool &g;
    bool f;
    int_fast32_t u;
  #ifndef NDEBUG
    RC rc;
  #endif

    constexpr explicit operator bool() const noexcept {
        return f;
    }

    Z(bool &g_, const RC rc2, const RC rc0, const RC rc1) noexcept
        : Z(g_, rc2, (assert(v[rc0]), m[rc0])+(assert(v[rc1]), m[rc1]))
    {
    }

    Z(bool &g_, const RC rc3, const RC rc0, const RC rc1, const RC rc2) noexcept
        : Z(g_, rc3, (assert(v[rc0]), m[rc0])+(assert(v[rc1]), m[rc1])+(assert(v[rc2]), m[rc2]))
    {
    }

    Z(bool &g_, const RC rc_, const int_fast32_t p) noexcept
        : g /*=*/ (g_)
    {
        assert(!g);
        assert(s >= -static_cast<int_fast32_t>(A)* n   );
        assert(s <= +static_cast<int_fast32_t>(A)* n   );
        assert(p >= -static_cast<int_fast32_t>(A)*(n-1));
        assert(p <= +static_cast<int_fast32_t>(A)*(n-1));
        if (s < p-static_cast<int_fast32_t>(A)) { // <=> s-p < -A
            f = false;
            return;
        }
        if (s > p+static_cast<int_fast32_t>(A)) { // <=> s-p > +A
            f = false;
            return;
        }
        const int_fast32_t u_ = s - p;
        if (!k[u_]) {
            f = false;
            return;
        }
        f = true;
        u = u_;
      #ifndef NDEBUG
        rc = rc_;
      #endif
        set(rc_, u_);
    }

    ~Z() noexcept {
        if (f && !g)
          #ifndef NDEBUG
            unset(rc, u);
          #else
            unset(u);
          #endif
    }
};

template<typename F>
inline bool iterate(const RC rc, F &f) noexcept {
    for (uint_fast8_t i = 0; i < n*n; i++) {
        if (!k[a[i]])
            continue;
        set(rc, a[i]);
        if (f())
            return true;
        unset(rc, a[i]);
    }
    return false;
}

namespace n_3 {

inline bool f0() noexcept {
    bool g = false;
    if (const Z z02 = Z(g, {0,2}, {0,0}, {0,1}))
        if (const Z z20 = Z(g, {2,0}, {0,2}, {1,1}))
            if (const Z z21 = Z(g, {2,1}, {0,1}, {1,1}))
                if (const Z z10 = Z(g, {1,0}, {0,0}, {2,0}))
                    if (const Z z12 = Z(g, {1,2}, {0,2}, {2,2}))
                        g = true;
    return g;
}

inline bool f1() noexcept {
    bool g = false;
    if (const Z z22 = Z(g, {2,2}, {0,0}, {1,1}))
        g = iterate({0,1}, f0);
    return g;
}

inline bool f2() noexcept {
    bool g = false;
    g = iterate({0,0}, f1);
    return g;
}

} // namespace n_3

namespace n_4 {

inline bool f0() noexcept {
    bool g = false;
    if (const Z z21 = Z(g, {2,1}, {0,1}, {1,1}, {3,1}))
        if (const Z z12 = Z(g, {1,2}, {0,3}, {2,1}, {3,0}))
            if (const Z z22 = Z(g, {2,2}, {0,0}, {1,1}, {3,3}))
                if (const Z z13 = Z(g, {1,3}, {1,0}, {1,1}, {1,2}))
                    if (const Z z23 = Z(g, {2,3}, {2,0}, {2,1}, {2,2}))
                        g = true;
    return g;
}

inline bool f1() noexcept {
    bool g = false;
    if (const Z z20 = Z(g, {2,0}, {0,0}, {1,0}, {3,0}))
        g = iterate({1,1}, f0);
    return g;
}

inline bool f2() noexcept {
    bool g = false;
    if (const Z z32 = Z(g, {3,2}, {0,1}, {0,2}, {3,1}))
        g = iterate({1,0}, f1);
    return g;
}

inline bool f3() noexcept {
    bool g = false;
    if (const Z z33 = Z(g, {3,3}, {0,0}, {0,3}, {3,0}))
        g = iterate({3,1}, f2);
    return g;
}

inline bool f4() noexcept {
    bool g = false;
    if (const Z z03 = Z(g, {0,3}, {0,0}, {0,1}, {0,2}))
        g = iterate({3,0}, f3);
    return g;
}

inline bool f5() noexcept {
    bool g = false;
    g = iterate({0,2}, f4);
    return g;
}

inline bool f6() noexcept {
    bool g = false;
    g = iterate({0,1}, f5);
    return g;
}

inline bool f7() noexcept {
    bool g = false;
    g = iterate({0,0}, f6);
    return g;
}

} // namespace n_4

} // namespace

int main() {
    cin >> n;
    for (uint_fast8_t i = 0; i < n*n; i++) {
        cin >> a[i];
        s += a[i];
        k[a[i]]++;
    }
    assert(s%n == 0);
    s /= n;
    switch (n) {
        case 1:
            set({0,0}, s);
            break;
        case 2:
            assert(s%2 == 0);
            assert(a[0] == s/2);
            assert(a[1] == s/2);
            assert(a[2] == s/2);
            assert(a[3] == s/2);
            set({0,0}, s/2);
            set({0,1}, s/2);
            set({1,0}, s/2);
            set({1,1}, s/2);
            break;
        case 3: {
            assert(s%3 == 0);
            set({1,1}, s/3);
            const bool f = n_3::f2();
            assert(f);
            break;
        }
        case 4: {
            const bool f = n_4::f7();
            assert(f);
            break;
        }
        default:
            assert(false);
            break;
    }

    cout << s << '\n';
    for (uint_fast8_t r = 0; r < n; r++) {
        for (uint_fast8_t c = 0; c < n; c++) {
            assert((v[{r,c}]));
            cout << m[{r,c}] << ' ';
        }
        cout << '\n';
    }
}

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int inf=-100000005;
int a[20];
bool vis[20];
int col[5];
int row[5];
int dia1,dia2;
int map[5][5];
int n,nn;
int sum;
bool dfs(int u)
{
    int i;
    if(u==nn)
        return true;
    int x=u/n;
    int y=u%n;
    int t=inf;
    for(i=0;i<nn;i++)
    {
        if(!vis[i]&&t<a[i])
        {
            t=a[i];
            map[x][y]=a[i];
            if(y==n-1&&row[x]+a[i]!=sum)
                continue;
            if(x==n-1&&col[y]+a[i]!=sum)
                continue;
            if(x==n-1&&y==0&&dia1+a[i]!=sum)
                continue;
            if(x==n-1&&y==n-1&&dia2+a[i]!=sum)
                continue;
            row[x]+=a[i];
            col[y]+=a[i];
            if(x+y==n-1)
                dia1+=a[i];
            if(x==y)
                dia2+=a[i];
            vis[i]=1;
            if(dfs(u+1))
                return true;
            row[x]-=a[i];
            col[y]-=a[i];
            if(x+y==n-1)
                dia1-=a[i];
            if(x==y)
                dia2-=a[i];
            vis[i]=0;
        }
    }
    return false;
}
int main()
{
    int i,j;
    scanf("%d",&n);
    nn=n*n;
    for(i=0;i<nn;i++)
        scanf("%d",&a[i]);
    sort(a,a+nn);
    sum=0;
    for(i=0;i<nn;i++)
        sum+=a[i];
    sum/=n;
    printf("%d\n",sum);
    dfs(0);
    for(i=0;i<n;i++)
    {
        printf("%d",map[i][0]);
        for(j=1;j<n;j++)
            printf(" %d",map[i][j]);
        printf("\n");
    }
    return 0;
}

#include<iostream>
#include<algorithm>

using namespace std;

int mat[3][3],a[10],s,n;

bool check(int x[][3])
{
  int b=0,c;
  for(int i=0;i<n;i++)
    b+=x[i][0];
  for(int i=0;i<n;i++)
  {
    c=0;
    for(int j=0;j<n;j++)
      c+=x[i][j];
    if(c!=b)
      return 0;
  }
  for(int i=1;i<n;i++)
  {
    c=0;
    for(int j=0;j<n;j++)
      c+=x[j][i];
    if(c!=b)
      return 0;
  }
  c=0;
  for(int i=0,j=0;i<n;i++,j++)
  {
    c+=x[i][j];
  }
  if(c!=b)
    return 0;
  c=0;
  for(int i=0,j=n-1;i<n;i++,j--)
  {
    c+=x[i][j];
  }
  if(c!=b)
    return 0;
  return 1;
}

int main()
{
  cin>>n;
  for(int i=0;i<n*n;i++)
    cin>>a[i];
  int x[3][3];
  sort(a,a+n*n);
  do{
    for(int i=0,k=0;i<n;i++)
    {
      for(int j=0;j<n;j++,k++)
	x[i][j]=a[k];
    }
    if(check(x))
    {
      for(int i=0;i<n;i++)
      {
	int c=0;
	for(int j=0;j<n;j++)
	{
	  c+=x[i][j];
	  mat[i][j]=x[i][j];
	}
	s=c;
      }
    }
  }while(next_permutation(a,a+n*n));
  cout<<s<<"\n";
  for(int i=0;i<n;i++)
  {
    for(int j=0;j<n;j++)
      cout<<mat[i][j]<<" ";
    cout<<"\n";
  }
  return 0;
}

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <queue>
#include <string>
#include <cstring>
#include <utility>

using namespace std;

int n,b[10][10],sum,a[100],q[100],st[100];
bool check,f[100];

void kiemtra()
{
    int i,j,tong;
    for (i=0;  i<=n-1;  i++)
        {
            tong=0;
            for (j=0;  j<=n-1;  j++) tong+=b[i][j];
            if (tong!=sum) return;
            tong=0;
            for (j=0;  j<=n-1;  j++) tong+=b[j][i];
            if (tong!=sum) return;
        }
    tong=0;
    for (i=0;  i<=n-1;  i++) tong+=b[i][i];  if (tong!=sum) return;
    tong=0;
    for (i=0;  i<=n-1;  i++) tong+=b[i][n-i-1];  if (tong!=sum) return;
    check=true;
}

void backtrack(int k)
{
    int i,j,t;
    for (i=1;  i<=n*n;  i++)
        if (f[i])
            {
                b[k/n][k%n]=a[i];  f[i]=false;
                if (k==n*n-1)
                    {
                        kiemtra();
                        if (check)
                            {
                                for (j=0;  j<=n-1;  j++)
                                    {
                                        for (t=0;  t<=n-1;  t++) cout <<b[j][t]<<" ";  cout <<endl;
                                    }
                            }
                    }
                else backtrack(k+1);
                if (check) return;
                f[i]=true;
            }
}

int rest(int k)
{
    int i;
    for (i=1;  i<=n*n;  i++)
        if ((a[i]==k)&&(f[i]))
            {
                f[i]=false;  return i;
            }
    return 0;
}

void update()
{
    b[0][0]=st[1];  b[0][1]=st[2];  b[0][3]=st[3];
    b[1][0]=st[4];  b[1][1]=st[5];  b[1][2]=st[6];
    b[2][1]=st[7];  b[2][2]=st[8];
    b[3][0]=sum-st[7]-st[6]-st[3];  q[7]=rest(b[3][0]);
    b[3][3]=sum-st[1]-st[5]-st[8];  q[8]=rest(b[3][3]);
    b[0][2]=sum-b[0][0]-b[0][1]-b[0][3];  q[1]=rest(b[0][2]);
    b[3][1]=sum-b[0][1]-b[1][1]-b[2][1];  q[2]=rest(b[3][1]);
    b[3][2]=sum-b[3][0]-b[3][1]-b[3][3];  q[3]=rest(b[3][2]);
    b[2][0]=sum-b[0][0]-b[1][0]-b[3][0];  q[4]=rest(b[2][0]);
    b[2][3]=sum-b[2][0]-b[2][1]-b[2][2];  q[5]=rest(b[2][3]);
    b[1][3]=sum-b[0][3]-b[2][3]-b[3][3];  q[6]=rest(b[1][3]);
    int i,j;
    for (i=1;  i<=8;  i++) if (q[i]==0)
        {
            for (j=1;  j<=8;  j++) f[q[j]]=true;  return;
        }
    check=true;
    for (i=0;  i<=3;  i++)
        {
            for (j=0;  j<=3;  j++) cout <<b[i][j]<<" ";  cout <<endl;
        }
}

void pick(int k)
{
    int i;
    for (i=1;  i<=10;  i++)
        if (f[i])
            {
                f[i]=false; st[k]=a[i];
                if (k==8) update();
                else pick(k+1);
                if (check) return;
                f[i]=true;
            }
}

int random(int x)
{
    return (((x+231645978)%21123)%(x));
}

int main()
{
 //   freopen("test.inp","r",stdin);
    int i,x,temp;
    cin >>n;  check=false;
    for (i=1;  i<=n*n;  i++) cin >>a[i];
    sum=0;  for (i=1;  i<=n*n;  i++) sum+=a[i];  sum/=n;
    memset(f,true,sizeof(f));
    cout <<sum<<endl;
    srand(time(NULL));
    if (n<=3) backtrack(0);
    else
        {
            while (!check)
                {
                    for (i=1;  i<=n*n;  i++)
                        {
                            x=rand()%i+1;  temp=a[x];  a[x]=a[i];  a[i]=temp;
                        }
                    pick(1);
                    if (check) break;
                }
        }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define ll long long
#define maxn 4

ll n, N, sum, p[maxn * maxn];
bool ok(vector<ll> v) {
	sort(v.begin(), v.end());
	for (ll i = 0; i < N; i++)
		if (p[i] != v[i]) return false;
	return true;
}

ll cnt[maxn * maxn], ans[maxn][maxn], lef[maxn * maxn - maxn];
vector<vector<ll> > v, vec;
set<pair<pair<ll, ll>, pair<ll, ll> > > st;
bool ok(vector<ll> v, bool flag) {
	sort(v.begin(), v.end());
	pair<pair<ll, ll>, pair<ll, ll> > pa = mp(mp(v[0], v[1]), mp(v[2], v[3]));
	if (st.find(pa) == st.end()) {
		if (flag) {
			st.insert(pa);
		}
		return true;
	}
	return false;
}
bool can(vector<ll> v) {
	for (ll i = 0; i < n; i++) {
		if (cnt[v[i]] < 3) return false;
		v[i] = p[v[i]];
	}
	return ok(v, true);
}
bool ok() {
	ll s = 0;
	for (ll r = 0; r < n; r++)
		s += ans[r][n - 1];
	if (s != sum) return false;
	s = 0;
	for (ll c = 0; c < n; c++)
		s += ans[n - 1][c];
	if (s != sum) return false;
	vector<ll> v;
	for (ll r = 0; r < n; r++)
		for (ll c = 0; c < n; c++)
			v.push_back(ans[r][c]);
	return ok(v);
}
void out() {
	printf("%lld", sum);
	for (ll r = 0; r < n; r++) {
		printf("\n");
		for (ll c = 0; c < n; c++) {
			if (c) printf(" ");
			printf("%lld", ans[r][c]);
		}
	}
}
bool cal(vector<ll> id) {
	multiset<ll> se = multiset<ll>(p, p + N);
	for (ll i = 0; i < n; i++) {
		ans[i][i] = p[id[i]];
		se.erase(se.find(ans[i][i]));
	}
	ll N = 0;
	for (multiset<ll>::iterator it = se.begin(); it != se.end(); it++)
		lef[N++] = *it;
	for (ll i = 0; i < N; i++) {
		for (ll k = 0; k < N; k++) {
			if (k == i) continue;
			for (ll h = 0; h < N; h++) {
				if (h == k || h == i) continue;
				for (ll t = 0; t < N; t++) {
					if (t == h || t == k || t == i) continue;
					ans[0][3] = lef[i];
					ans[1][2] = lef[k];
					ans[2][1] = sum - (ans[1][1] + ans[1][2] + ans[2][2]);
					ans[3][0] = sum - (ans[0][3] + ans[1][2] + ans[2][1]);
					vector<ll> v;
					v.resize(n);
					v[0] = ans[0][3];
					v[1] = ans[1][2];
					v[2] = ans[2][1];
					v[3] = ans[3][0];
					if (ok(v, false)) continue;
					ans[0][1] = lef[h];
					ans[0][2] = sum - (ans[0][0] + ans[0][1] + ans[0][3]);
					ans[3][1] = sum - (ans[0][1] + ans[1][1] + ans[2][1]);
					ans[3][2] = sum - (ans[0][2] + ans[1][2] + ans[2][2]);
					ans[1][0] = lef[t];
					ans[2][0] = sum - (ans[0][0] + ans[1][0] + ans[3][0]);
					ans[1][3] = sum - (ans[1][0] + ans[1][1] + ans[1][2]);
					ans[2][3] = sum - (ans[2][0] + ans[2][1] + ans[2][2]);
					if (ok()) {
						out();
						return true;
					}

				}
			}
		}
	}
	return false;
}

int main() {
	ll i, k, h, t, r, c;
	vector<vector<ll> > a;
	vector<ll> b, ret, id;
	scanf("%lld", &n);
	if (n <= 2) {
		scanf("%lld", &i);
		if (n == 1) printf("%lld\n%lld", i, i);
		else printf("%lld\n%lld %lld\n%lld %lld", (i << 1), i, i, i, i);
		return 0;
	}
	N = n * n;
	for (i = 0; i < N; i++) {
		scanf("%lld", p + i);
		sum += p[i];
	}
	sort(p, p + N);
	sum /= n;
	if (n == 3) {
		for (i = 0; i < N; i++) {
			for (k = 0; k < N; k++) {
				if (k == i) continue;
				for (h = 0; h < N; h++) {
					if (h == k || h == i) continue;
					ans[0][0] = p[i];
					ans[1][1] = p[k];
					ans[0][2] = p[h];
					ans[2][2] = sum - (ans[0][0] + ans[1][1]);
					ans[2][0] = sum - (ans[0][2] + ans[1][1]);
					ans[0][1] = sum - (ans[0][0] + ans[0][2]);
					ans[1][0] = sum - (ans[0][0] + ans[2][0]);
					ans[1][2] = sum - (ans[1][0] + ans[1][1]);
					ans[2][1] = sum - (ans[0][1] + ans[1][1]);
					if (ok()) {
						out();
						return 0;
					}
				}
			}
		}
	}
	id.resize(n);
	for (i = 0; i < N; i++)
		for (k = i + 1; k < N; k++)
			for (h = k + 1; h < N; h++)
				for (t = h + 1; t < N; t++) {
					if (p[i] + p[k] + p[h] + p[t] != sum) continue;
					cnt[i]++;
					cnt[k]++;
					cnt[h]++;
					cnt[t]++;
					id[0] = i;
					id[1] = k;
					id[2] = h;
					id[3] = t;
					v.push_back(id);
				}
	for (i = 0; i < v.size(); i++) {
		if (!can(v[i])) continue;
		vec.push_back(v[i]);
	}
	for (i = 0; i < vec.size(); i++) {
		if (cal(vec[i])) break;
		swap(vec[i][1], vec[i][3]);
		if (cal(vec[i])) break;
		swap(vec[i][2], vec[i][3]);
		if (cal(vec[i])) break;
	}
	return 0;
}

#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <cstring>

using namespace std;
typedef long long LL;
#define REP(i, n) for(int i=0; i<n; i++)
#define FOR(i, x, y) for(int i=x; i<=y; i++)

int n;
int a[5][5];
int s;

bool check()
{
    s = 0;
    REP(i, n)
        s += a[i][i];

    REP(i, n)
    {
        int cur = 0;
        REP(j, n)
            cur += a[i][j];
        if (s != cur) return false;
    }

    REP(i, n)
    {
        int cur = 0;
        REP(j, n)
            cur += a[j][i];
        if (s != cur) return false;
    }

    int cur = 0;
    REP(i, n)
        cur += a[i][n-1-i];
    if (cur != s)
        return false;
    return true;
}

int main()
{
    n;
    cin >> n;
    vector<int> nums(n*n);
    REP(i, n*n)
        cin >> nums[i];
    sort(nums.begin(), nums.end());

    do
    {
        REP(i, n*n)
            a[i / n][i % n] = nums[i];
        if (!check()) continue;
        cout << s << endl;
        REP(i, n)
        {
            REP(j, n)
            {
                if (j)
                    cout << " ";
                cout << a[i][j];
            }
            cout << endl;
        }
        return 0;
    } while(next_permutation(nums.begin(), nums.end()));
}

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <ctime>

#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <algorithm>

using namespace std;

#define sqr(x) ((x)*(x))
#define sz(v) ((v).size())
#define forn(i,n) for (i = 0; i < (int)(n); ++i)
#define pb push_back
#define mp make_pair
#define all(v) (v).begin(), (v).end()

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair <int, int> pii;
typedef vector <int> vi;

const int maxn = 4;
ll a[maxn * maxn + 5];
int n;

struct State {
	ll b[4][4];
	bool ok() {
		int i, j;
		ll sum, sum1 = 0, sum2 = 0;
		forn(i, n) {
			sum1 += b[i][i];
			sum2 += b[i][n - i - 1];
		}
		if (sum1 != sum2)
			return false;
		sum = sum1;
		forn(i, n) {
			sum1 = sum2 = 0;
			forn(j, n) {
				sum1 += b[i][j];
				sum2 += b[j][i];
			}
			if (sum1 != sum || sum2 != sum)
				return false;
		}
		return true;
	}
};

void print(const State &s)
{
	ll sum = 0, s1, s2;
	int i, j;
	s2 = 0;
	forn(i, n) {
		sum += s.b[i][i];
	}

	cout << sum << "\n";
	forn(i, n) {
		forn(j, n)
			cout << s.b[i][j] << " ";
		cout << "\n";
	}

	exit(0);
}

bool used[20];
ll sum, sum2, diag;
ull total, lft;
State st;

ll FIRST;

const int posX[] = {2, 3, 1, 3, 1, 2};
const int posY[] = {1, 1, 2, 2, 3, 3};
const int MAX_IT = 20;

void rec3(int k)
{
	int i, j;
	if (k == 4) {
		if (diag != sum)
			return;
		/**/

		j = 0;
		forn(i, 16) {
			if (!used[i]) {
				st.b[posY[j]][posX[j]] = a[i];
				++j;
				if (j == 6)
					break;
			}
		}
		for (int it = 0; it < MAX_IT; ++it) {
			if (st.ok()) {
				print(st);
			}
			int i1 = rand() % 6, i2 = rand() % 6;
			swap(st.b[posY[i1]][posX[i1]], st.b[posY[i2]][posX[i2]]);
		}

		/**/
		return;
	}
	forn(i, 16) {
		if (!used[i]) {
			used[i] = true;
			st.b[k][k] = a[i];
			diag += a[i];
			rec3(k + 1);
			diag -= a[i];
			used[i] = false;
		}
	}
}

void rec2(int y)
{
	int i;
	if (y == 4) {
		if (sum2 != sum)
			return;
		diag = FIRST;
		rec3(1);
		return;
	}
	forn(i, 16) {
		if (!used[i]) {
			used[i] = true;
			st.b[y][0] = a[i];
			sum2 += a[i];
			lft += a[i];
			rec2(y + 1);
			lft -= a[i];
			sum2 -= a[i];
			used[i] = false;
		}
	}
}

void rec(int x)
{
	int i;
	if (x == 4) {
		if (total != 4*(ull)sum)
			return;
		sum2 = FIRST;
		lft = 0;
		rec2(1);
		return;
	}
	forn(i, 16)  {
		if (!used[i]) {
			used[i] = true;
			st.b[0][x] = a[i];
			sum += a[i];
			rec(x + 1);
			sum -= a[i];
			used[i] = false;
		}
	}
}

int main()
{
	srand(31415);

	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);

	ios::sync_with_stdio(false);

	int i, j, k;
	cin >> n;
	forn(i, n*n) {
		cin >> a[i];
		total += a[i];
	}
	//solve();

	if (n <= 3) {
		ll s1, s2;
		State st;
		vi p(n*n);
		forn(i, n*n)
			p[i] = i;
		do {
			s1 = s2 = 0;
			k = 0;
			forn(i, n) {
				forn(j, n) {
					st.b[i][j] = a[p[k++]];
				}
			}
			if (st.ok()) {
				print(st);
				return 0;
			}
		} while (next_permutation(p.begin(), p.end()));
		return 0;
	}

	forn(i, 16) {
		sum = FIRST = st.b[0][0] = a[i];
		used[i] = true;
		for (j = i + 1; j < 16; ++j) {
			st.b[0][1] = a[j];
			used[j] = true;
			sum += a[j];
			rec(2);
			sum -= a[j];
			used[j] = false;
		}
		used[i] = false;
	}

	return 0;
}

#include<stdio.h>
#include<iostream>
#include<vector>
#include<cmath>
#include<algorithm>
#include<memory.h>
#include<map>
#include<set>
#include<queue>
#include<list>
#include<sstream>
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define SS stringstream
#define sqr(x) ((x)*(x))
#define m0(x) memset(x,0,sizeof(x))
#define m1(x) memset(x,63,sizeof(x))
#define CC(x) cout << (x) << endl
#define AL(x) x.begin(),x.end()
#define pw(x) (1ull<<(x))
#define M 1000000007
using namespace std;
typedef pair<int,int> pt;
typedef vector<int> vt;

int n,a[55],e[5][5],q,s,f[55],A[5],B[5];

void rec(int x,int y){
	if (y==n){
		if (A[x]*n!=s)return;
		y=0,x++;
	}
	if (x==n){
		for (int i=0;i<n;i++)if (A[i]*n!=s)return;
		for (int i=0;i<n;i++)if (B[i]*n!=s)return;
		q=0;
		for (int i=0;i<n;i++)q+=e[i][i];
		if (q*n!=s)return;
		q=0;
		for (int i=0;i<n;i++)q+=e[i][n-1-i];
		if (q*n!=s)return;
		CC(q);
		for (int i=0;i<n;i++){
			for (int j=0;j<n;j++)cout << e[i][j] << " ";
			puts("");
		}
		exit(0);
	}
	for (int i=0;i<n*n;i++)if (!f[i]){
		if (i>0&&f[i-1]==0&&a[i]==a[i-1])continue;
		e[x][y]=a[i];
		A[x]+=a[i];
		B[y]+=a[i];
		f[i]=1;
		rec(x,y+1);
		f[i]=0;
		A[x]-=a[i];
		B[y]-=a[i];
	}
}

int main(){
//	freopen("1.in","r",stdin);
//	freopen("1.out","w",stdout);
	cin >> n;
	for (int i=0;i<n*n;i++)cin >> a[i],s+=a[i];
	sort(a,a+n*n);
	rec(0,0);
	return 0;
}

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <bitset>
#include <complex>

using namespace std;

typedef unsigned uint;
typedef long long Int;
typedef vector<int> vint;
typedef pair<int,int> pint;
#define mp make_pair

template<class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; }
template<class T> void chmin(T &t, T f) { if (t > f) t = f; }
template<class T> void chmax(T &t, T f) { if (t < f) t = f; }
int in() { int x; scanf("%d", &x); return x; }

int N, NN;
int A[20];
int S;
int B[20][20];

bool check() {
	int x, y, z;
	for (x = 0; x < N; ++x) {
		int sum = 0;
		for (y = 0; y < N; ++y) sum += B[x][y];
		if (sum != S) return 0;
	}
	for (y = 0; y < N; ++y) {
		int sum = 0;
		for (x = 0; x < N; ++x) sum += B[x][y];
		if (sum != S) return 0;
	}
	for (z = 0; z < 2; ++z) {
		int sum = 0;
		for (x = 0; x < N; ++x) sum += B[x][z ? (N - 1 - x) : x];
		if (sum != S) return 0;
	}
	return 1;
}

int f[20] = {};
int get(int a) {
	int i = lower_bound(A, A + NN, a) - A;
	for (; i < NN && f[i]; ++i);
	return (i < NN && A[i] == a && !f[i]) ? i : -1;
}

void solve4() {
	int i00, i01, i02, i03;
	int i10, i11, i12, i13;
	int i20, i21, i22, i23;
	int i30, i31, i32, i33;
	memset(f, 0, sizeof(f));
	for (i00 = 0; i00 < NN; ++i00)
	for (i03 = i00 + 1; i03 < NN; ++i03)
	for (i30 = i03 + 1; i30 < NN; ++i30)
	{
		++f[i00]; ++f[i03]; ++f[i30];
		for (i01 = 0; i01 < NN; ++i01) if (!f[i01]) {
			++f[i01];
			i02 = get(S - A[i00] - A[i01] - A[i03]);
			if (~i02) {
				++f[i02];
				for (i10 = 0; i10 < NN; ++i10) if (!f[i10]) {
					++f[i10];
					i20 = get(S - A[i00] - A[i10] - A[i30]);
					if (~i20) {
						++f[i20];
						--f[i20];
						for (i12 = 0; i12 < NN; ++i12) if (!f[i12]) {
							++f[i12];
							i21 = get(S - A[i03] - A[i12] - A[i30]);
							if (~i21) {
								++f[i21];
								for (i11 = 0; i11 < NN; ++i11) if (!f[i11]) {
									++f[i11];
									i13 = get(S - A[i10] - A[i11] - A[i12]);
									if (~i13) {
										++f[i13];
										i31 = get(S - A[i01] - A[i11] - A[i21]);
										if (~i31) {
											++f[i31];
											for (i22 = 0; i22 < NN; ++i22) if (!f[i22]) {
												++f[i22];
												i23 = get(S - A[i20] - A[i21] - A[i22]);
												if (~i23) {
													++f[i23];
													i32 = get(S - A[i02] - A[i12] - A[i22]);
													if (~i32) {
														++f[i32];
														i33 = get(S - A[i00] - A[i11] - A[i22]);
														if (~i33) {
															if (S == A[i30] + A[i31] + A[i32] + A[i33])
															if (S == A[i03] + A[i13] + A[i23] + A[i33])
															{
																B[0][0] = A[i00]; B[0][1] = A[i01]; B[0][2] = A[i02]; B[0][3] = A[i03];
																B[1][0] = A[i10]; B[1][1] = A[i11]; B[1][2] = A[i12]; B[1][3] = A[i13];
																B[2][0] = A[i20]; B[2][1] = A[i21]; B[2][2] = A[i22]; B[2][3] = A[i23];
																B[3][0] = A[i30]; B[3][1] = A[i31]; B[3][2] = A[i32]; B[3][3] = A[i33];
																return;
															}
														}
														--f[i32];
													}
													--f[i23];
												}
												--f[i22];
											}
											--f[i31];
										}
										--f[i13];
									}
									--f[i11];
								}
								--f[i21];
							}
							--f[i12];
						}
					}
					--f[i10];
				}
				--f[i02];
			}
			--f[i01];
		}
		--f[i00]; --f[i03]; --f[i30];
	}
}

int main() {
	int i;
	int x, y;

	for (; ~scanf("%d", &N); ) {
		NN = N * N;
		for (i = 0; i < NN; ++i) {
			A[i] = in();
		}
		sort(A, A + NN);
		S = accumulate(A, A + NN, 0) / N;
		if (N <= 3) {
			do {
				for (x = 0; x < N; ++x) for (y = 0; y < N; ++y) {
					B[x][y] = A[x * N + y];
				}
				if (check()) {
					break;
				}
			} while (next_permutation(A, A + NN));
		} else {
			solve4();
		}
assert(check());
		printf("%d\n", S);
		for (x = 0; x < N; ++x) {
			for (y = 0; y < N; ++y) {
				if (y) printf(" ");
				printf("%d", B[x][y]);
			}
			puts("");
		}
	}

	return 0;
}

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int,int> ii;

int main() {
	cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);
	int n; cin>>n;
	vector<int> a(n*n);
	for (int i=0; i<n*n; i++)
		cin>>a[i];
	sort(a.begin(), a.end());
	do {
		set<int> st;
		for (int i=0; i<n; i++) {
			int row=0, col=0;
			for (int j=0; j<n; j++) {
				row+=a[i*n+j];
				col+=a[j*n+i];
			}
			st.insert(row);
			st.insert(col);
		}
		int d1=0, d2=0;
		for (int i=0; i<n; i++)
			d1+=a[i*n+i], d2+=a[i*n+(n-1-i)];
		st.insert(d1);
		st.insert(d2);

		if(st.size()==1) {
			cout << *st.begin() << endl;
			for (int i=0; i<n; i++) {
				for (int j=0; j<n; j++)
					cout << a[i*n+j] << " ";
				cout << endl;
			}
			return 0;
		}
	} while(next_permutation(a.begin(), a.end()));
	return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <string.h>

using namespace std;

int b[16],c[16];
int a[4][4],j;
int n,sum,sum1;
int s[16];
int d[16],r[16];

void get(int num){
/*    ans++;
    if (ans%1000000==0)
        cerr << ans << " " << a[0][0] <<  endl;*/
    if (num==2*n && s[0]!=s[1])
        return;
    if (num==3*n && s[0]!=s[2])
        return;
    if (num==4*n && s[0]!=s[3])
        return;
    if (num==(n-1)*n + 2 && d[0]!=d[1])
        return;
    if (num==(n-1)*n + 3 && d[0]!=d[2])
        return;
    if (num==(n-1)*n + 4 && d[0]!=d[3])
        return;
    if (num==n*n){
        sum = 0;
        sum1 = 0;
        for (int i=0; i<n; i++)
            sum1+=a[i][i];
        for (int i=0; i<n; i++)
            sum += a[n-1-i][i];
        if (sum!=sum1)
            return;
        cout << sum << endl;
        for (int i=0; i<n; i++){
            for (int j=0; j<n; j++)
                cout << a[i][j] << " ";
            cout << endl;
        }
        exit(0);
    }
    for (int i=0; i<=j; i++)
        if (c[i]){
            c[i]--;
            a[num/n][num%n] = r[i];
            s[num/n] += r[i];
            d[num%n] += r[i];
            get(num+1);
            s[num/n] -= r[i];
            d[num%n] -= r[i];
            c[i]++;
        }

}

int main(void){
    //freopen("input.txt","r",stdin);//freopen("output.txt","w",stdout);

    cin >> n;
    for (int i=0; i<n*n; i++)
        cin >> b[i];

    sort(b,b+n*n);
    j = 0;
    c[0] = 1;
    r[0] = b[0];
    for (int i=1; i<n*n; i++)
        if (b[i]==b[i-1])
            c[j]++;
        else{
            j++;
            r[j]=b[i];
            c[j]++;
        }

    get(0);

    return 0;
}

//HACK ME, PLEASE! ^_^
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <utility>
#include <math.h>
#include <cstdlib>
#include <memory.h>
#include <queue>

#define pb push_back
#define i64 long long
#define mp make_pair
#define pii pair <int,int>
#define vi vector <int>
#define vii vector <pii>
#define f first
#define s second
#define foran(i,a,b) for (int i=a;i<(int)b;i++)
#define forn(i,n) for (int i=0;i<(int)n;i++)
#define ford(i,n) for (int i=(int)n-1;i>=0;i--)

const double eps = 1e-9;
const int int_inf = 2000000000;
const i64 i64_inf = 1000000000000000000LL;
const double pi = acos(-1.0);

using namespace std;
int n;
int a[20];
int s;
int c[4][4];

int main() {
//  freopen("input.txt","r",stdin);
//  freopen("output.txt","w",stdout);
  cin >> n;
  forn(i, n * n)
      scanf("%d",&a[i]), s += a[i];
  s /= n;
  sort(a, a + n * n);

  do
  {
//  forn(i, n*n) cout << a[i] << " "; cout << endl;

    forn(i, n * n)
        c[i / n][i % n] = a[i];
    bool f = 1;

    forn(i, n)
    {
        int sum = 0;
        forn(j, n)
            sum += c[i][j];
        if (sum != s) { f = 0; break; }
    }
    if (!f) continue;
    forn(i, n)
    {
        int sum = 0;
        forn(j, n)
            sum += c[j][i];
        if (sum != s) { f = 0; break; }
    }
    if (!f) continue;
    int sum = 0;
    forn(i, n)
        sum += c[i][i];
    if (sum != s) continue;
    sum = 0;
    forn(i, n)
        sum += c[i][n-i-1];
    if (sum != s) continue;

    cout << s << endl;
    forn(i, n)
    {
        forn(j, n)
            cout << c[i][j] << " ";
        cout << endl;
    }
    break;
  } while (next_permutation(a,a + n * n));

  return 0;
}

#include <stdio.h>
#include <iostream>
#include <string>
#include <stdlib.h>
#include <ctime>
#include <vector>
#include <utility>
#include <algorithm>

using namespace std;

#define fi first
#define se second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)

typedef long long LL;

vector<LL> A(10);
LL Ans[10][10];
int mark[100];
long long mag;
long long Str[10];
long long Stl[10];
long long D[10];
int n;

//void prov()
//{
//    for(int i = 0; i < n; i++)
//    {
//        if(Str[i] != mag || Stl[i] != mag)
//            return false;
//    }
//    if(D[0] != mag && D[1] != mag)
//        return false;
//    return true;
//}
void rec(int i, int j)
{
    //cout << i << " " << j << endl;
    if(j == 0 && i > 0)
    {
        if(Str[i-1] != mag)
            return;
    }
    if(i == n-1 && j > 0)
    {
        if(Stl[j-1] != mag || D[0] != mag)
            return;
    }
    if(i >= n)
    {
        if(D[1] != mag)
            return;
        for(int i2 = 0; i2 < n; i2++)
        {
            for(int j2 = 0; j2 < n; j2++)
            {
                cout << Ans[i2][j2] << " ";
            }
            cout << endl;
        }
        exit(0);
    }
    int i2;
    int j2;
    if(j == n -1)
    {
        i2 = i + 1;
        j2 = 0;
    }
    else
    {
        i2 = i;
        j2 = j + 1;
    }
    for(int k = 0; k < n*n; k++)
    {
        if(mark[k] == 0)
        {
            mark[k] = 1;
            Ans[i][j] = A[k];
            Str[i] += A[k];
            Stl[j] += A[k];
            if(i == n - 1 - j)
                D[0] += A[k];
            if(i == j)
                D[1] += A[k];

            rec(i2, j2);

            Str[i] -= A[k];
            Stl[j] -= A[k];

            if(i == n - 1 - j)
                D[0] -= A[k];
            if(i == j)
                D[1] -= A[k];
            mark[k] = 0;
        }
    }
}

int main()
{
  //  freopen("in.txt", "r", stdin);
    cin >> n;
    long long sum = 0;
    for(int i = 0; i < n*n; i++)
    {
        cin >> A[i];
        sum += A[i];
    }
    mag = sum/n;
    cout << mag << endl;
    rec(0, 0);
    return 0;
}

#include <bits/stdc++.h>
using namespace std;

#define all(s) s.begin(), s.end()
#define vi vector<int>
#define pb push_back
#define ii pair<int, int>
#define x first
#define y second

const int REP = 1000;

long long Rand(long long l, long long h) {
    return l + ((long long)rand() * (RAND_MAX + 1) * (RAND_MAX + 1) * (RAND_MAX + 1) +
                (long long)rand() * (RAND_MAX + 1) * (RAND_MAX + 1) +
                (long long)rand() * (RAND_MAX + 1) +
                rand()) % (h - l + 1);
}

int n, s;

long long calc(vi &v) {
    long long res = 0;
    for (int i = 0; i < n; i++) {
        int temp1 = 0;
        int temp2 = 0;
        for (int j = 0; j < n; j++) {
            temp1 += v[i * n + j];
            temp2 += v[i + j * n];
        }
        res += abs(temp1 - s) + abs(temp2 - s);
    }
    int temp1 = 0;
    int temp2 = 0;
    for (int i = 0; i < n; i++) {
        temp1 += v[i * n + i];
        temp2 += v[i * n + n - 1 - i];
    }
    return res + abs(temp1 - s) + abs(temp2 - s);
}

int main() {
    srand(time(NULL));
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> n;
    vi v;
    for (int i = 1; i <= n * n; i++) {
        int u;
        cin >> u;
        v.pb(u);
        s += u;
    }
    s /= n;
    if (n == 1) {
        cout << s << endl << s;
        return 0;
    }
    while (1) {
        random_shuffle(all(v));
        for (int rep = 1; rep < REP; rep++) {
            int p = Rand(0, v.size() - 1);
            int q = Rand(0, v.size() - 1);
            while (q == p) {
                q = Rand(0, v.size() - 1);
            }
            long long cur = calc(v);
            swap(v[p], v[q]);
            long long nx = calc(v);
            if (cur < nx) {
                swap(v[p], v[q]);
            }
            // for (auto u : v) {
            //     cout << u << ' ';
            // }
            // cout << calc(v);
            // cout << '\n';
            if (calc(v) == 0) {
                cout << s << '\n';
                for (int i = 0; i < n; i++) {
                    for (int j = 0; j < n; j++) {
                        cout << v[i * n + j] << ' ';
                    }
                    cout << '\n';
                }
                exit(0);
            }
        }
    }
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
// typedef vector<lli> vi;
// typedef vector< vi > graph;
#define EB push_back
ll n;
ll arr[10];
ll s;
bool check()
{
	ll i,j;
	for(i=0;i<n;i++)
	{
		ll ss = 0;
		ll j;
		for(j=i*n;j<(i+1)*n;j++)
			ss+=arr[j];
		if(ss!=s)
			return false;
	}
	for(i=0;i<n;i++)
	{
		ll ss = 0;
		for(j=i;j<n*n;j+=n)
			ss+=arr[j];
		if(ss!=s)
			return false;
	}
	if(n==3){
	if(arr[0]+arr[4]+arr[8]!=s)
		return false;
	if(arr[2]+arr[4]+arr[6]!=s)
		return false;
}
else if(n==2)
{
	if(arr[0]+arr[3]!=s)
		return false;
	if(arr[1]+arr[2]!=s)
		return false;
}
	return true;
}

int main()
{
	cin>>n;
	s =0;
	ll i;
	for(i=0;i<n*n;i++)
		cin>>arr[i],s+=arr[i];
	s/=n;
	if(n==1)
	{
		cout<<s<<"\n"<<s;
		return 0;
	}
	sort(arr,arr+n*n);
	do
	{
		if(check())
		{
			cout<<s<<"\n";
			for(i=0;i<n*n;i++)
			{
				cout<<arr[i]<<" ";
				if(i%n==n-1)
					cout<<"\n";
			}
			return 0;
		}
	}while(next_permutation(arr,arr+n*n));
}

//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <ctime>

int n;
int sum = 0;
int a[16];
int mat[4][4];
int rows[4], cols[4];
int gotx[4], goty[4], gotd[2];
int x[16], y[16];
int diag[2];

bool dfs(int state, int mask) {
	//std::cout << "on state " << state << ", mask " << mask << std::endl;
	/*if(state == 0) {
		if(n == 1) {
			mat[0][0] = a[0];
			return true;
		} else if(n == 2) {
			for(int i = 0; i < n * n; i++) {
				if((mask & 1 << i) == 0) continue;
				for(int j = i + 1; j < n * n; j++) {
					if((mask & 1 << j) == 0) continue;
					if(a[i] + a[j] == sum) {
						int nxtMask = mask;
						mat[0][0] = a[i];
						nxtMask ^= 1 << i;
						mat[1][1] = a[j];
						nxtMask ^= 1 << j;
						rows[0] = cols[0] = a[i];
						rows[1] = cols[1] = a[j];
						if(dfs(state + 1, nxtMask)) {
							return true;
						}
					}
				}
			}
		} else if(n == 3) {
			for(int i = 0; i < n * n; i++) {
				if((mask & 1 << i) == 0) continue;
				for(int j = i + 1; j < n * n; j++) {
					if((mask & 1 << j) == 0) continue;
					for(int k = j + 1; k < n * n; k++) {
						if((mask & 1 << k) == 0) continue;
						if(a[i] + a[j] + a[k] == sum) {
							int nxtMask = mask;
							mat[0][0] = a[i];
							nxtMask ^= 1 << i;
							mat[1][1] = a[j];
							nxtMask ^= 1 << j;
							mat[2][2] = a[k];
							nxtMask ^= 1 << k;
							rows[0] = cols[0] = a[i];
							rows[1] = cols[1] = a[j];
							rows[2] = cols[2] = a[k];
							//std::cout << "choosing diagonal (" << i << ", " << j << ", " << k << ")\n";
							if(dfs(state + 1, nxtMask)) {
								return true;
							}
						}
					}
				}
			}
		} else {
			for(int i = 0; i < n * n; i++) {
				if((mask & 1 << i) == 0) continue;
				for(int j = i + 1; j < n * n; j++) {
					if((mask & 1 << j) == 0) continue;
					for(int k = j + 1; k < n * n; k++) {
						if((mask & 1 << k) == 0) continue;
						for(int l = k + 1; l < n * n; l++) {
							if((mask & 1 << l) == 0) continue;
							if(a[i] + a[j] + a[k] + a[l] == sum) {
								int nxtMask = mask;
								mat[0][0] = a[i];
								nxtMask ^= 1 << i;
								mat[1][1] = a[j];
								nxtMask ^= 1 << j;
								mat[2][2] = a[k];
								nxtMask ^= 1 << k;
								mat[3][3] = a[l];
								nxtMask ^= 1 << l;
								rows[0] = cols[0] = a[i];
								rows[1] = cols[1] = a[j];
								rows[2] = cols[2] = a[k];
								rows[3] = cols[3] = a[l];
								if(dfs(state + 1, nxtMask)) {
									return true;
								}
							}
						}
					}
				}
			}
		}
	} else if(state == 2) {
		if(n == 2) {
			for(int i = 0; i < n * n; i++) {
				if((mask & 1 << i) == 0) continue;
				for(int j = 0; j < n * n; j++) {
					if((mask & 1 << j) == 0) continue;
					if(a[i] + a[j] == sum) {
						int nxtMask = mask;
						mat[n][0] = a[i];
						nxtMask ^= 1 << i;
						mat[1][1] = a[j];
						nxtMask ^= 1 << j;
						rows[0] = cols[0] = a[i];
						rows[1] = cols[1] = a[j];
						if(dfs(state + 1, nxtMask)) {
							return true;
						}
					}
				}
			}
		} else if(n == 3) {
			for(int i = 0; i < n * n; i++) {
				if((mask & 1 << i) == 0) continue;
				for(int j = 0; j < n * n; j++) {
					if((mask & 1 << j) == 0) continue;
					for(int k = 0; k < n * n; k++) {
						if((mask & 1 << k) == 0) continue;
						if(a[i] + a[j] + a[k] == sum) {
							int nxtMask = mask;
							mat[0][0] = a[i];
							nxtMask ^= 1 << i;
							mat[1][1] = a[j];
							nxtMask ^= 1 << j;
							mat[2][2] = a[k];
							nxtMask ^= 1 << k;
							rows[0] = cols[0] = a[i];
							rows[1] = cols[1] = a[j];
							rows[2] = cols[2] = a[k];
							//std::cout << "choosing diagonal (" << i << ", " << j << ", " << k << ")\n";
							if(dfs(state + 1, nxtMask)) {
								return true;
							}
						}
					}
				}
			}
		} else {
			for(int i = 0; i < n * n; i++) {
				if((mask & 1 << i) == 0) continue;
				for(int j = 0; j < n * n; j++) {
					if((mask & 1 << j) == 0) continue;
					for(int k = 0; k < n * n; k++) {
						if((mask & 1 << k) == 0) continue;
						for(int l = 0; l < n * n; l++) {
							if((mask & 1 << l) == 0) continue;
							if(a[i] + a[j] + a[k] + a[l] == sum) {
								int nxtMask = mask;
								mat[0][0] = a[i];
								nxtMask ^= 1 << i;
								mat[1][1] = a[j];
								nxtMask ^= 1 << j;
								mat[2][2] = a[k];
								nxtMask ^= 1 << k;
								mat[3][3] = a[l];
								nxtMask ^= 1 << l;
								rows[0] = cols[0] = a[i];
								rows[1] = cols[1] = a[j];
								rows[2] = cols[2] = a[k];
								rows[3] = cols[3] = a[l];
								if(dfs(state + 1, nxtMask)) {
									return true;
								}
							}
						}
					}
				}
			}
		}
	} else if(n == 2 || (state - 1) / (n - 1) == n) {
		//std::cout << "ended " << (state - 1) / (n - 1) << ", " << n << std::endl;
		return true;
	} else {*/
	for(int i = 0; i < 2; i++) {
		if(gotd[i] == n && diag[i] != sum) {
			return false;
		}
	}
	if(state == n * n) return true;
	int row = x[state];
	int col = y[state];
	bool d1 = row == col;
	bool d2 = row + col == n - 1;
	for(int i = 0; i < n * n; i++) {
		if((mask & (1 << i)) != 0 && rows[row] + a[i] <= sum && cols[col] + a[i] <= sum && (!d1 || diag[0] + a[i] <= sum) && (!d2 || diag[1] + a[i] <= sum)) {
			if(gotx[row] + 1 == n && rows[row] + a[i] < sum) {
				continue;
			}
			if(goty[col] + 1 == n && cols[col] + a[i] < sum) {
				continue;
			}
			gotx[row]++;
			goty[col]++;
			rows[row] += a[i];
			cols[col] += a[i];
			if(d1) {
				gotd[0]++;
				diag[0] += a[i];
			}
			if(d2) {
				gotd[1]++;
				diag[1] += a[i];
			}
			if(dfs(state + 1, mask ^ (1 << i))) {
				mat[row][col] = a[i];
				return true;
			}
			gotx[row]--;
			goty[col]--;
			if(d1) {
				gotd[0]--;
				diag[0] -= a[i];
			}
			if(d2) {
				gotd[1]--;
				diag[1] -= a[i];
			}
			rows[row] -= a[i];
			cols[col] -= a[i];
		}
	}
	//}
	return false;
}

int main() {
	std::ios_base::sync_with_stdio(false); std::cin.tie(NULL);
	std::cin >> n;
	int mn = 1e9;
	long long ans = 0;
	for(int i = 0; i < n * n; i++) {
		std::cin >> a[i];
		mn = std::min(mn, a[i]);
		ans += a[i];
	}
	std::cout << ans / n << std::endl;
	if(n == 1) {
		std::cout << a[0] << std::endl;
		return 0;
	} else if(n == 2) {
		x[0] = 0;
		x[1] = 1;
		x[2] = 0;
		x[3] = 1;
		y[0] = 0;
		y[1] = 1;
		y[2] = 1;
		y[3] = 0;
	} else if(n == 3) {
		x[0] = 0;
		x[1] = 1;
		x[2] = 2;
		x[3] = 0;
		x[4] = 2;
		x[5] = 0;
		x[6] = 1;
		x[7] = 1;
		x[8] = 2;
		y[0] = 0;
		y[1] = 1;
		y[2] = 2;
		y[3] = 2;
		y[4] = 0;
		y[5] = 1;
		y[6] = 0;
		y[7] = 2;
		y[8] = 1;
	} else if(n == 4) {
		int wtfx[] = {0, 1, 2, 3, 0, 1, 2, 3, 0, 0, 1, 1, 2, 2, 3, 3};
		int wtfy[] = {0, 1, 2, 3, 3, 2, 1, 0, 1, 2, 0, 3, 0, 3, 1, 2};
		for(int i = 0; i < 16; i++) {
			x[i] = wtfx[i];
			y[i] = wtfy[i];
		}
	}
	long long cur = 0;
	for(int i = 0; i < n * n; i++) {
		a[i] -= mn;
		cur += a[i];
		//std::cout << a[i] << (i + 1 == n * n ? '\n' : ' ');
	}
	srand(time(0));
	std::random_shuffle(a, a + n * n);
	sum = cur / n;
	dfs(0, (1 << (n * n)) - 1);
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < n; j++) {
			std::cout << mat[i][j] + mn << (j + 1 == n ? '\n' : ' ');
		}
	}
}

#include <iostream>
#include <fstream>
#include <cstdio>
#include <set>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <string>
#include <map>
#include <cstring>

using namespace std;

#define NextLine() { char c = getchar(); while (c != 10 && c != EOF) { c = getchar(); } }

int n;
int a[20];
int s;

void Load()
{
	scanf("%d", &n);
	int i;
	s = 0;
	for (i = 0; i < n * n; i++)
	{
		scanf("%d", &a[i]);
		s += a[i];
	}
	random_shuffle(a, a + (n * n));
	s /= n;
}

int good[100000];
int kindagood[110000];

int cur[4][4];
int usd;

int Go(int ci, int cj)
{
	//cerr << ci << " " << cj << "\n";
	int i, j;
	/*for (i = 0; i < n; i++)
	{
		for (j = 0; j < n; j++)
		{
			cerr << cur[i][j] << " ";
		}
		cerr << "\n";
	}*/
	if (ci == n)
	{
		return 1;
	}
	if (cj == n)
	{
		int crow = 0;
		for (i = 0; i < n; i++) crow |= (1 << cur[ci][i]);
		if (!good[crow]) return 0;
		return Go(ci + 1, 0);
	}
	for (i = 0; i < n * n; i++)
	{
		if (usd & (1 << i)) continue;
		cur[ci][cj] = i;
		usd ^= (1 << i);
		int bad = 0;
		// check row
		int crow = 0;
		for (j = 0; j <= cj; j++) crow |= (1 << cur[ci][j]);
		if (!kindagood[crow]) bad = 1;
		// check col
		int ccol = 0;
		for (j = 0; j <= ci; j++) ccol |= (1 << cur[j][cj]);
		if (!kindagood[ccol]) bad = 1;
		// check main
		int cc = 0;
		for (j = 0; j < n; j++)
		{
			if (cur[j][j] != -1) cc |= (1 << cur[j][j]);
		}
		if (!kindagood[cc]) bad = 1;
		// check not main
		cc = 0;
		for (j = 0; j < n; j++)
		{
			if (cur[j][n - 1 - j] != -1) cc |= (1 << cur[j][n - 1 - j]);
		}
		if (!kindagood[cc]) bad = 1;
		if (!bad)
		{
			if (Go(ci, cj + 1)) return 1;
		}
		usd ^= (1 << i);
		cur[ci][cj] = -1;
	}
	return 0;
}

void Solve()
{
	int i, j;
	for (i = 0; i < (1 << (n * n)); i++)
	{
		int cnt, sum;
		cnt = sum = 0;
		for (j = 0; j < (n * n); j++)
		{
			if (i & (1 << j))
			{
				cnt++;
				sum += a[j];
			}
		}
		if (sum == s && cnt == n)
		{
			good[i] = 1;
			for (j = i; j > 0; j = ((j - 1) & i))
			{
				kindagood[j] = 1;
			}
		}
	}
	kindagood[0] = 1;
	/*for (i = 0; i < (1 << (n * n)); i++)
	{
		if (kindagood[i]) cerr << i << " " << good[i] << "\n";
	}*/
	//cerr << "kinda built\n";
	memset(cur, 0xFF, sizeof(cur));
	usd = 0;
	Go(0, 0);
	printf("%d\n", s);
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < n; j++)
		{
			printf("%d ", a[cur[i][j]]);
		}
		printf("\n");
	}
}

int main()
{
	srand(1875);
	Load();
	Solve();
	return 0;
}

#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <set>
#include <deque>
#include <queue>
using namespace std;
#define FAST ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0)
#define OUT(n) cout<<fixed<<setprecision(n)
#define F first
#define S second
#define int long long
#define double long double
#define pb push_back
#define pf push_front
#define FOR(i,s,e) for(int i = s ; i<=e ; i++)
#define BAC(i,e,s) for(int i = e ; i>=s ; i--)
typedef pair<int,int> pii;
typedef pair<pii,int> tpi;
typedef pair<pii,pii> qpi;
const int MOD = 1e9 + 7;
const int INF = 4e18;
const int MAX = 9;
const double PI = 3.14159265358979323846;
bool IN_RANGE(pii v , int n , int m , int d ) { return v.F >= d && v.F <= n && v.S >= d && v.S <= m ;}
bool IN_R( int x , int n , int d) { return x >= d && x <= n ;}
int MA = -INF , MI = INF;
int C2I(char x , char base) { return x - base; }
void PAIROUT( pii x ) {	cout<<x.F<<" "<<x.S; }

int each , n , k;
vector<int> arr;
int square[MAX+5][MAX+5];
void middle();
void check()
{
    FOR(a,0,k-1)
    {
        FOR(b,0,k-1)
        {
            FOR(c,0,k-1)
            {
                if( arr[a] + arr[b] + arr[c] == each )
                {
                    square[0][0] = arr[a] , square[0][1] = arr[b] , square[0][2] = arr[c];
                    middle();
                }
            }
        }
    }
}

void middle()
{

    FOR(i,0,k-1)
    {
        square[1][1] = arr[i];
        square[2][1] = each - (square[0][1] + square[1][1]);
        square[2][0] = each - (square[1][1] + square[0][2]);
        square[1][0] = each - (square[0][0] + square[2][0]);
        square[1][2] = each - (square[1][0] + square[1][1]);
        square[2][2] = each - (square[2][0] + square[2][1]);

        vector<int> temp;

        FOR(i,0,2)
        {
            FOR(j,0,2)
                temp.pb( square[i][j] );
        }
        sort( temp.begin() , temp.end() );
        if( temp != arr )
            continue;
        int x;
        FOR(i,0,2)
        {
            x = square[i][0] + square[i][1] + square[i][2];
            if( x != each )
                continue;
            x = square[0][i] + square[1][i] + square[2][i];
            if( x != each )
                continue;
        }
        x = square[0][0] + square[1][1] + square[2][2];
        if(x != each )
            continue;
        x = square[0][2] + square[1][1] + square[2][0];
        if(x != each)
            continue;

        cout<<each<<"\n";
        FOR(i,0,2)
        {
            FOR(j,0,2)
                cout<<square[i][j]<<" ";
            cout<<"\n";
        }
        exit(0);
    }
}
main()
{
	FAST , OUT(10);
	int x;
    cin>>n;
    k = n*n;
    FOR(i,0,k-1)
        cin>>x , arr.pb(x) , each += x;
    each /= n;
    sort(arr.begin() , arr.end());

    if( n!=3  )
    {
        cout<<each<<"\n";
        FOR(i,0,n-1)
        {
            FOR(j,0,n-1)
                cout<<x<<" ";
            cout<<"\n";
        }
    }
    else
        check();
	return 0;
}

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <cassert>
#include <climits>
using namespace std;

#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)

int N, A[20];
int K;

int sum[1 << 16];
int crr[10][10];

int &get(pair<int, int> p) { return crr[p.first][p.second]; }

vector<int> along[10][10];
int G;
vector<pair<int, int> > group[20];
vector<pair<int, int> > fre[20];
vector<int> ord;

void add(int i, int j, int g) {
  along[i][j].pb(g);
  group[g].pb(mp(i, j));
}

int S;

void search(int o, int k, int b) {
  if (o == G) throw 0;  // done
  int g = ord[o];

  if (k == (int)fre[g].size()) {
    if (g == 0) {
      S = 0;
      rep (i, N) S += crr[group[g][i].first][group[g][i].second];
      // printf("S = %d\n", S);
    } else {
      int s = 0;
      rep (i, N) s += crr[group[g][i].first][group[g][i].second];
      if (s != S){
        // printf("NG (%d) vvv\n", o); rep (i, N) rep (j, N) printf("%d%c", crr[i][j], j == N - 1 ? '\n' : ' '); puts("^^^");
        return;
      }
    }
    search(o + 1, 0, b);
    return;
  }
  else {
    int prv = INT_MIN;
    rep (q, K) if (b & (1 << q)) {
      if (prv == A[q]) continue;

      get(fre[g][k]) = A[q];
      // printf("%d %d <- %d\n", fre[g][k].first, fre[g][k].second, A[q]);
      search(o, k + 1, b - (1 << q));

      prv = A[q];
    }
  }
}

int main() {
  while (1 == scanf("%d", &N)) {
    K = N * N;
    rep (i, K) scanf("%d", &A[i]);

    rep (b, 1 << K) {
      sum[b] = 0;
      rep (i, N) if (b & (1 << i)) sum[b] += A[i];
    }

    G = N + N + 2;
    rep (i, G) group[i].clear();
    rep (i, N) rep (j, N) along[i][j].clear();
    rep (i, N) rep (j, N) add(i, j, i);
    rep (j, N) rep (i, N) add(i, j, N + j);
    rep (i, N) add(i, i, N + N);
    rep (i, N) add(i, N - i - 1, N + N + 1);

    // decide the order
    memset(crr, -1, sizeof(crr));
    ord.resize(G);
    vector<bool> usd(G);
    rep (i, G) {
      int best_f = INT_MAX, best_g = -1;
      rep (g, G) if (!usd[g]) {
        int f = 0;
        rep (i, N) if (crr[group[g][i].first][group[g][i].second] == -1) ++f;
        if (f < best_f) {
          best_f = f;
          best_g = g;
        }
      }

      usd[best_g] = true;
      ord[i] = best_g;
      fre[best_g].clear();
      rep (i, N) {
        int a = group[best_g][i].first;
        int b = group[best_g][i].second;
        if (crr[a][b] == -1) {
          fre[best_g].pb(mp(a, b));
          crr[a][b] = 0;
        }
      }
    }

    // rep (i, G) { int g = ord[i]; rep (j, fre[g].size()) printf("(%d,%d) ", fre[g][j].first, fre[g][j].second);  puts("");   }

    try {
      rep (i, N) rep (j, N) crr[i][j] = -100;
      search(0, 0, (1 << K) - 1);
      puts("FAIL?");
    } catch (...) {
      printf("%d\n", S);
      rep (i, N) rep (j, N) printf("%d%c", crr[i][j], j == N - 1 ? '\n' : ' ');
    }
  }

  return 0;
}

#include<bits/stdc++.h>
using namespace std;

#define double long double
#define int long long
#define vec vector
#define pb push_back
#define unset unordered_set
#define ii pair<int, int>
#define X first
#define Y second
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define FORE(i, a, b) for(int i = (a); i <= (b); i++)
#define FORD(i, a, b) for(int i = (a); i >= (b); i--)
#define bitcount(n) __builtin_popcountll(n)
template <typename T,typename U>
std::pair<T,U> operator+(const std::pair<T,U> & l,const std::pair<T,U> & r) {
  return {l.first+r.first,l.second+r.second};
}
typedef void (*callback_function)(void); // type for conciseness
const long long INF64 = 1e18;
const int INF32 = 1e9;
const int MOD = 1e9 + 7;
const double PI = acos(-1.0L);
const double EPS = static_cast<double>(1e-9);
typedef long long ll;

const int N = 1e6 + 7;

// 29gg

void input() {
  //freopen("??.in", "r", stdin);
  //freopen("??.out", "w", stdout);
  // 34gg

}

void preprocess() {

}

void debug() {
  //  44gg
  if (true) {

  }
}

void solve() {
  preprocess();
  // 50gg
  int n; cin >> n;
  int n2 = n * n;
  int a[n2]; FOR(i, 0, n2) {cin >> a[i];}
  if (n == 1) {
    cout << a[0] << endl << a[0] << endl; return;
  }
  sort(a, a + n2);
  do {
    int s = 0;
    bool ok = true;
    FOR(i, 0, n) {s += a[i];}
    FOR(i, 1, n) {
      int s2 = 0;
      FOR(j, 0, n) {s2 += a[i * n + j];}
      if (s2 != s) {ok = false; break;}
    }
    FOR(j, 0, n) {
      int s2 = 0;
      FOR(i, 0, n) {
        s2 += a[i * n + j];
      }
      if (s2 != s) {ok = false; break;}
    }
    int s2 = 0;
    for (int i = 0; i < n2; i += (n + 1)) {
      s2 += a[i];
      //cerr << "(" << i << "," << a[i] << ") ";
    }
    //cerr << endl;
    if (s2 != s) {ok = false;}
    s2 = 0;
    for (int i = n - 1; i < n2 - 1; i += (n - 1)) {
      s2 += a[i];
    }
    if (s2 != s) {ok = false;}
    if (ok) {
      cout << s << endl;
      FOR(i, 0, n) {
        FOR(j, 0, n) {
          cout << a[i * n + j] << ' ';
        }
        cout << endl;
      }
      return;
    }
  } while (next_permutation(a, a + n2));
  //debug();
}

void output() {

}

#undef int
int main() {
#define int long long
	ios_base::sync_with_stdio(false);
	cin.tie();
  //auto beginProgram = chrono::steady_clock::now();

  {
    input();
    solve();
    output();
  }

  //auto endProgram = chrono::steady_clock::now();

  return 0;
}

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cmath>
#include <memory.h>
#include <string>
#include <set>
#include <map>
#include <queue>
using namespace std;
#define forn(i,n) for(int i=0;i<(n);++i)
#define forv(i,v) forn(i,(int)(v).size())
#define iinf 1000000000
#define linf 1000000000000000000LL
#define dinf 1e200
#define all(v) (v).begin(),(v).end()
#define pb push_back
#define mp make_pair
#define lng long long
#define eps 1e-11
#define EQ(a,b) (fabs((a)-(b))<eps)
#define SQ(a) ((a)*(a))
#define PI 3.14159265359
#define index asdindex
#define FI first
#define SE second
#define prev asdprev
#define ends asdends
#define PII pair<int,int>
#define X first
#define Y second
#define mset(a,b) memset(a,b,sizeof(a))

int n;
int src[16];
int s;

int f(){
    int res=0;
    forn(i, n){
        int sum = 0;
        for(int j=i*n;j<(i+1)*n;++j){
            sum += src[j];
        }
        res+=abs(sum-s);
    }
    forn(i, n){
        int sum = 0;
        for(int j=i;j<n*n;j+=n){
            sum += src[j];
        }
        res+=abs(sum-s);
    }
    {
        int sum = 0;
        for(int i=0;i<n*n;i+=n+1)sum += src[i];
        res+=abs(sum-s);
    }
    int sum = 0;
    int j=0;
    for(int i=n-1;i<n*n;i+=n-1,++j){
        if (j==n)break;
        sum += src[i];
    }
    res+=abs(sum-s);
    return res;
}

void print(){
    cout << s << endl;
    forn(i,n*n){
        cout<<src[i]<<" ";
        if (i%n==n-1)cout<<endl;
    }
    exit(0);
}

int main(){
#ifdef __ASD__
    freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#endif
    cin >> n;
    s=0;
    forn(i,n*n){
        cin>>src[i];
        s+=src[i];
    }
    s=s/n;
    for(;;){
        random_shuffle(src, src+n*n);
        forn(i, 100){
            int q = f();
            if (q==0)
                print();
            forn(i1, n*n){
                for(int i2=i1+1;i2<n*n;++i2){
                    swap(src[i1], src[i2]);
                    int new_q = f();
                    if (new_q>=q){
                        swap(src[i1], src[i2]);
                    }else{
                        q = new_q;
                    }
                }
            }
l1:;
        }
    }

    return 0;
}

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <string>
#include <map>
#include <utility>
#include <vector>
#include <set>
#include <algorithm>

#define maxn 5
#define Fup(i, s, t) for (int i = s; i <= t; i ++)
#define Fdn(i, s, t) for (int i = s; i >= t; i --)
#define Path(i, s) for (int i = s; i; i = d[i].next)

using namespace std;

map <int, int>id, pos;
int ans[maxn][maxn];
int use[maxn * maxn], tt[maxn * maxn], ss[maxn];
bool flag;
int n, sum, S;

bool success()
{
    int s1 = 0, s2 = 0;
    Fup(i, 1, n){
        s1 += ans[i][i];
        s2 += ans[i][n - i + 1];
    }
    if (s1 == sum && s2 == sum)
        return true;
    return false;
}

void DFS(int x, int y, int s)
{
    if (x == n){
        if (success()){
            Fup(i, 1, n){
                Fup(j, 1, n - 1)
                    cout << ans[i][j] << ' ';
                cout << ans[i][n] << endl;
            }
            flag = true;
        }
        return;
    }
    if (y == n){
        if (pos[sum - s] && use[pos[sum - s]]){
            use[pos[sum - s]] --;
            ans[x][y] = sum - s;
            ss[y] += ans[x][y];
            if (x == n - 1){
                if (pos[sum - ss[y]] && use[pos[sum - ss[y]]]){
                    use[pos[sum - ss[y]]] --;
                    ans[n][y] = sum - ss[y];
                    DFS(x + 1, 1, 0);
                    if (flag)
                        return;
                    use[pos[sum - ss[y]]] ++;
                }
                ss[y] -= ans[x][y];
                use[pos[sum - s]] ++;
                return;
            }
            DFS(x + 1, 1, 0);
            if (flag)
                return;
            ss[y] -= ans[x][y];
            use[pos[sum - s]] ++;
        }
        return;
    }
    Fup(i, 1, S)
        if (use[i]){
            use[i] --;
            ans[x][y] = id[i];
            ss[y] += ans[x][y];
            if (x == n - 1){
                if (pos[sum - ss[y]] && use[pos[sum - ss[y]]]){
                    use[pos[sum - ss[y]]] --;
                    ans[n][y] = sum - ss[y];
                    DFS(x, y + 1, s + id[i]);
                    if (flag)
                        return;
                    use[pos[sum - ss[y]]] ++;
                }
                ss[y] -= ans[x][y];
                use[i] ++;
                continue;
            }
            DFS(x, y + 1, s + id[i]);
            ss[y] -= ans[x][y];
            use[i] ++;
            if (flag)
                return;
        }
}

void init()
{
    cin >> n;
    sum = 0;
    memset(use, 0, sizeof(use));
    Fup(i, 1, n * n){
        int x;
        cin >> x;
        if (!pos[x]){
            pos[x] = ++ S;
            id[S] = x;
        }
        use[pos[x]] ++;
        sum += x;
    }
}

void solve()
{
    sum /= n;
    cout << sum << endl;
    if (n == 1){
        cout << id[1] << endl;
        return;
    }
    flag = false;
    DFS(1, 1, 0);
}

int main()
{
//    freopen("temp.in", "r", stdin);
//    freopen("temp.out", "w", stdout);
    ios::sync_with_stdio(false);
    init();
    solve();
    return 0;
}

#include <cstdio>
#include <iostream>
#include <cmath>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>

using namespace std;

int main()
{
#ifdef DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	int n;
	cin >> n;
	vector < vector <int> > a(n, vector <int>(n));
	vector <int> b(n * n), q(n * n);
	for (int i = 0; i < n * n; ++i)
	{
		cin >> b[i];
		q[i] = i;
	}
	do
	{
		for (int i = 0; i < n; ++i)
			for (int j = 0; j < n; ++j)
				a[i][j] = b[q[n * i + j]];
		int s = a[0][0];
		if (n == 3)
		{
			s = a[0][0] + a[0][1] + a[0][2];
			if (s != a[1][0] + a[1][1] + a[1][2])
				continue;
			if (s != a[2][0] + a[2][1] + a[2][2])
				continue;
			if (s != a[0][0] + a[1][0] + a[2][0])
				continue;
			if (s != a[0][1] + a[1][1] + a[2][1])
				continue;
			if (s != a[0][2] + a[1][2] + a[2][2])
				continue;
			if (s != a[0][0] + a[1][1] + a[2][2])
				continue;
			if (s != a[0][2] + a[1][1] + a[2][0])
				continue;
		}
		if (n == 2)
		{
			s = a[0][0] + a[0][1];
			if (s != a[1][0] + a[1][1])
				continue;
			if (s != a[0][0] + a[1][0])
				continue;
			if (s != a[0][1] + a[1][1])
				continue;
			if (s != a[0][0] + a[1][1])
				continue;
			if (s != a[0][1] + a[1][0])
				continue;
		}
		cout << s << endl;
		for (int i = 0; i < n; ++i)
		{
			for (int j = 0; j < n; ++j)
				cout << a[i][j] << ' ';
			cout << endl;
		}
		break;
	}
	while (next_permutation(q.begin(), q.end()));
	return 0;
}

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <algorithm>

using namespace std;

#define FOR(i,a,b) for (int i = (a); (i) <= (b); ++(i))
#define mp make_pair
#define inf 2000000000
#define pb push_back
#define X first
#define Y second
#define LL long long

int n,t;
int a[20];
int used[20];
int b[5][5];
bool ok;

void go(int x, int y, int n)
{
	//cout << " go " << x<< " " << y << " " << n << endl;
	if (x > n)
	{
		bool good = true;
		int s = 0;
		FOR(i,1,n)
			s += b[1][i];
		FOR(i,2,n)
		{
			int sum = 0;
			FOR(j,1,n)
				sum += b[i][j];
			if (sum != s)
			{
				good = false;
				break;
			}
		}
		FOR(j,1,n)
		{
			int sum = 0;
			FOR(i,1,n)
				sum += b[i][j];
			if (sum != s)
			{
				good = false;
				break;
			}
		}
		int sum = 0;
		FOR(i,1,n)
			sum += b[i][i];
		if (sum != s) good = false;

		sum = 0;
		FOR(i,1,n)
			sum += b[i][n-i+1];
		if (sum != s) good = false;

		if (!good) return;
		//cout << " vict\n";
		cout << s << endl;
		FOR(i,1,n)
		{
			FOR(j,1,n)
				cout << b[i][j]<< " ";
			cout << endl;
		}
		ok = true;
		return;
	}
	if (ok) return;
	int xx = x;
	int yy = y;
	yy++;
	if (yy > n)
	{
		xx++;
		yy = 1;
	}
	for (int i = 1;  i <= t; ++i)
	if (used[i] == 0)
	{
		used[i] = 1;
		b[x][y] = a[i];
		go(xx,yy,n);
		used[i] = 0;
	}

}

int main()
{
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	cin >> n;
	t = n*n;
	for (int i = 1; i <= t; ++i)
	{
		cin >> a[i];
	}
	if (n == 1)
	{
		cout <<a[1] << endl;
		return 0;
	}
	go(1,1,n);
    return 0;
}

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <numeric>
#include <cassert>
using namespace std;
static const double EPS = 1e-10;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)
#define rev(i,n) for(int i=n-1;i>=0;i--)
#define all(a) a.begin(),a.end()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define SS stringstream
#define DBG1(a) rep(_X,sz(a)){printf("%d ",a[_X]);}puts("");
#define DBG2(a) rep(_X,sz(a)){rep(_Y,sz(a[_X]))printf("%d ",a[_X][_Y]);puts("");}
#define bitcount(b) __builtin_popcount(b)
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)

#define delete(a,n) a.erase(remove(all(a),n),a.end())
template<typename T, typename S> vector<T>& operator<<(vector<T>& a, S b) { a.push_back(b); return a; }
template<typename T> void operator>>(vector<T>& a, int b) {while(b--)if(!a.empty())a.pop_back();}
bool isprime(int n){ if(n<2)return false;  for(int i=2;i*i<=n;i++)if(n%i==0)return false;  return true;}
ll b_pow(ll x,ll n){return n ? b_pow(x*x,n/2)*(n%2?x:1) : 1ll;}
string itos(int n){stringstream ss;ss << n;return ss.str();}

map<int,int> use;

int n;

int table[] = {0,5,10,15, 3,6,9,12, 1,2, 13,14, 4,8, 7,11};

int ans[16];
int get = 0;
int sum = 0;
int tate_check(int x){
    int cnt = 0;
    for(int i = 0 ; i < 4 ; i++)
        cnt += ans[i*4+x];
    return cnt;
}

int yoko_check(int y){
    int cnt = 0;
    for(int i = 0 ; i < 4 ; i++)
        cnt += ans[i+y*4];
    return cnt;
}
//　＼1
int naname_check1(){
    int cnt = 0;
    for(int i = 0 ; i < 4 ; i++)
        cnt += ans[i+i*4];
    return cnt;
}
//　／　2
int naname_check2(){
    int cnt = 0;
    for(int i = 0 ; i < 4 ; i++)
        cnt += ans[(3-i)+i*4];
    return cnt;
}

int dfs(int pos){
    if(pos == n*n ){
        cout << get << endl;
        for(int i = 0 ; i < 4 ; i++){
            for(int j= 0 ; j < 4 ; j++) cout << ans[i*4+j] << (j==3?"\n":" ");
        }
        exit(0);
    }
    int p = table[pos];
    EACH(it,use){
        if(it->second){
            it->second--;
            sum += it->first;
            ans[p] = it->first;
            if( pos == 3){
                get = sum;
                dfs(pos+1);
            }else if(pos == 7 ){
                if( naname_check2() == get){
                    dfs(pos+1);
                }
            }else if(pos == 9){
                if( yoko_check(0) == get){
                    dfs(pos+1);
                }
            }else if(pos == 10){
                if( tate_check(1) == get){
                    dfs(pos+1);
                }
            }else if(pos == 11){
                if( tate_check(2) == get){
                    dfs(pos+1);
                }
            }else if(pos == 13){
                if( tate_check(0) == get){
                    dfs(pos+1);
                }
            }else if(pos == 14){
                if( yoko_check(1) == get){
                    dfs(pos+1);
                }
            }else if(pos == 14){
                if( yoko_check(2) == get && tate_check(3) == get){
                    dfs(pos+1);
                }
            }else{
                dfs(pos+1);
            }
            sum -= it->first;
            it->second++;
        }
    }
}

int solve(int n){
    int a[16] = {};
    for(int i = 0 ; i < n*n ; i++)
        cin >> a[i];
        sort(a,a+n*n);
    do{
        int s = 0 ,f =1;
        for(int i = 0 ; i < n ; i++) s += a[i];

        for(int i = 0 ; i < n ; i++){
            int t = 0;
            for(int j = 0 ; j < n ; j++)
                t += a[i*n+j];
            if(t != s) f = 0;
        }
        for(int i = 0 ; i < n ; i++){
            int t = 0;
            for(int j = 0 ; j < n ; j++)
                t += a[j*n+i];
            if(t != s) f = 0;
        }
        int x = 0;
        for(int i = 0 ; i < n ; i++) x += a[i*n+i];
        if( x != s) f=0;

        x = 0;
        for(int i = 0 ; i < n ; i++) x += a[i*n+(n-i-1)];
        if( x != s) f=0;

        if(f){
            cout << s << endl;
            for(int i = 0 ; i < n ; i++){
                for(int j = 0 ; j < n ; j++) cout << a[i*n+j] << (j==n-1?"\n":" ");
            }
            return 0;
        }
    }while(next_permutation(a,a+n*n));
}

int main(){
    ios_base::sync_with_stdio(false);
    cin >> n;
    if( n <= 3){
        solve(n);
    }else{
        for(int i = 0 ; i < n*n ; i++){
            int t;
            cin >> t;
            use[t]++;
        }
        dfs(0);
        for(int i = 0 ; i < n*n ; i++){
            int t;
            cin >> t;
            use[t]++;
        }
        dfs(0);
    }

}

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
	int n,a[9],s=0;
	scanf("%d",&n);
	for(int i=0;i<n*n;++i)
	{
		scanf("%d",&a[i]);
		s+=a[i];
	}
	if(n==1)printf("%d",a[0]);
	else if(n==2)
	{
		for(int i=0;i<2;++i)printf("%d %d\n",a[0],a[1]);
		for(int i=0;i<2;++i)printf("%d %d\n",a[2],a[3]);
	}
	else
	{
		int ans[9];
		s/=3;
		ans[4]=s/3;
		int ts=s-ans[4],ck[9];
	//	printf("%d\n%d\n",s,ts);
		for(int i=0;i<9;++i)
		{
			if(a[i]==ans[4])continue;
			for(int j=0;j<9;++j)
			{
				if(a[j]==ans[4])continue;
				ans[0]=a[i];ans[2]=a[j];
				ans[8]=ts-ans[0];ans[6]=ts-ans[2];
				ans[1]=s-ans[0]-ans[2];
				ans[3]=s-ans[0]-ans[6];
				ans[5]=s-ans[2]-ans[8];
				ans[7]=s-ans[6]-ans[8];
				memcpy(ck,ans,9*sizeof(int));
				sort(ck,ck+9);
				sort(a,a+9);
				bool flag=true;
				for(int i=0;i<9;++i)
					if(a[i]!=ck[i])flag=false;
				if(!flag)continue;
				for(int k=0;k<9;k+=3)
					printf("%d %d %d\n",ans[k],ans[k+1],ans[k+2]);
				return 0;
			}
		}
	}
	return 0;
}

#include <algorithm>
#include <vector>
#include <cstring>
#include <cmath>
#include <functional>
#include <set>
#include <map>
#include <iostream>
#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <sstream>
#include <cassert>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define mp make_pair

#define sz(x) ((int)((x).size()))
#define len(s) ((int)((s).length()))

#define rep(i, n) for (int i = 0; i < (n); i++)
#define fori(i, l, r)  for (int i = (l); i <= (r); i++)
#define forid(i, r, l) for (int i = (r); i >= (l); i--)
#define forv(i, v) for (int i = 0; i < sz(v); i++)
#define fors(i, s) for (int i = 0; i < len(s); i++)
#define forall(v) (v).begin(), (v).end()
#define foreach(i, v) for (__typeof(v.begin()) i = (v).begin(); i != (v).end(); i++)

#ifdef DEBUG
#define eprintf(...) fprintf (stderr, __VA_ARGS__)
#else
#define eprintf(...) assert (true)
#endif

#ifdef DEBUG
#define oprintf(...) fprintf (stdout, __VA_ARGS__)
#else
#define oprintf(...) assert (true)
#endif

template<class T> inline void setmax(T &a, T b) { if (b > a) a = b; }
template<class T> inline void setmin(T &a, T b) { if (b < a) a = b; }

typedef long long int64;

typedef vector<string> vs;
typedef vector<int> vi;
typedef vector<double> vd;

typedef pair<int,int> pii;
typedef pair<double,double> pdd;

int a[20], n;
vector<int> p;

inline int get(int i, int j) {
  return a[p[i * n + j]];
}

bool check() {
  int s = 0, val = 0;
  for (int i = 0; i < n; i++) {
    val += get(i, i);
    s += get(n - 1 - i, i);
  }
  if (val != s) return false;

  for (int i = 0; i < n; i++) {
    int s1 = 0, s2 = 0;
    for (int j = 0; j < n; j++) {
      s1 += get(i, j);
      s2 += get(j, i);
    }
    if (s1 != val || s2 != val)
      return false;
  }
  return true;
}

int main(void) {
#ifdef DEBUG
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  cin >> n;

  for (int i = 0; i < n * n; i++)
    cin >> a[i];

  p.resize(n * n);

  for (int i = 0; i < n * n; i++)
    p[i] = i;

  while (next_permutation(p.begin(), p.end())) {
    if (check()) {
      for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++) {
          cout << get(i, j) << (j < n - 1 ? ' ' : '\n');
        }
      }
      return 0;
    }
  }
  assert(0);

  return 0;
}

#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
using namespace std;

map <int, int> mp;
int sum, tot, rec[5], n, N, val[20], cnt[20], mat[5][5], a[20];
bool g[20][20];

void init(int dep)
{
    if (dep == n)
    {
        if (tot == sum) for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) g[rec[i]][rec[j]] = 1;
        return;
    }
    for (int i = !dep ? 0 : rec[dep-1]; i < N; ++i) if (cnt[i])
    {
        rec[dep] = i;
        --cnt[i];
        tot += val[i];
        init(dep + 1);
        tot -= val[i];
        ++cnt[i];
    }
}

bool dfs(int dep)
{
    if (!dep)
    {
        for (int i = 0; i < N; ++i) if (cnt[i])
        {
            int v = sum - val[i] - val[mat[1][1]] - val[mat[2][2]];
            if (mp.find(v) == mp.end()) continue;
            int j = mp[v];
            --cnt[i], --cnt[j];
            if (cnt[i] >= 0 && cnt[j] >= 0)
            {
                mat[0][0] = i, mat[3][3] = j;
                if (dfs(dep + 1)) return 1;
            }
            ++cnt[i], ++cnt[j];
        }
    }
    else if (dep == 1)
    {
        for (int i = 0; i < N; ++i) if (cnt[i] && g[i][mat[0][0]])
        {
            int v = sum - val[i] - val[mat[1][1]] - val[mat[1][2]];
            if (mp.find(v) == mp.end()) continue;
            int j = mp[v];
            --cnt[i], --cnt[j];
            if (cnt[j] >= 0 && g[j][mat[3][3]])
            {
                mat[1][0] = i, mat[1][3] = j;
                if (dfs(dep + 1)) return 1;
            }
            ++cnt[i], ++cnt[j];
        }
    }
    else if (dep == 2)
    {
        for (int i = 0; i < N; ++i) if (cnt[i])
        {
            int u = sum - val[i] - val[mat[0][0]] - val[mat[1][0]], v = sum - val[i] - val[mat[2][1]] - val[mat[2][2]], w = sum - u - val[mat[2][1]] - val[mat[1][2]];
            if (v + w + val[mat[1][3]] + val[mat[3][3]] != sum || mp.find(u) == mp.end() || mp.find(v) == mp.end() || mp.find(w) == mp.end()) continue;
            int j = mp[u], k = mp[v], l = mp[w];
            --cnt[i], --cnt[j], --cnt[k], --cnt[l];
            if (cnt[j] >= 0 && cnt[k] >= 0 && cnt[l] >= 0)
            {
                mat[2][0] = i, mat[3][0] = j, mat[2][3] = k, mat[0][3] = l;
                if (dfs(dep + 1)) return 1;
            }
            ++cnt[i], ++cnt[j], ++cnt[k], ++cnt[l];
        }
    }
    else
    {
        for (int i= 0; i < N; ++i) if (cnt[i])
        {
            int u = sum - val[i] - val[mat[0][0]] - val[mat[0][3]], v = sum - val[i] - val[mat[1][1]] - val[mat[2][1]], w = sum - v - val[mat[3][0]] - val[mat[3][3]];
            if (u + w + val[mat[1][2]] + val[mat[2][2]] != sum || mp.find(u) == mp.end() || mp.find(v) == mp.end() || mp.find(w) == mp.end()) continue;
            int j = mp[u], k = mp[v], l = mp[w];
            --cnt[i], --cnt[j], --cnt[k], --cnt[l];
            if (cnt[j] >= 0 && cnt[k] >= 0 && cnt[l] >= 0)
            {
                mat[0][1] = i, mat[0][2] = j, mat[3][1] = k, mat[3][2] = l;
                for (int r = 0; r < n; ++r) for (int c = 0; c < n; ++c) printf("%d%c", val[mat[r][c]], c+1==n?'\n':' ');
                return 1;
            }
            ++cnt[i], ++cnt[j], ++cnt[k], ++cnt[l];
        }
    }
    return 0;
}

bool small(int x, int y)
{
    if (x == n-1 && y == n-1)
    {
        int s1 = 0, s2 = 0;
        for (int i = 0; i < n-1; ++i) s1 += val[mat[i][n-1]], s2 += val[mat[n-1][i]];
        if (s1 == s2 && mp.find(sum - s1) != mp.end() && cnt[mp[sum - s1]])
        {
            mat[x][y] = mp[sum - s1];
            for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) printf("%d%c", val[mat[i][j]], j+1==n?'\n':' ');
            return 1;
        }
    }
    else if (y == n-1)
    {
        int v = sum;
        for (int i = 0; i < n-1; ++i) v -= val[mat[x][i]];
        if (mp.find(v) == mp.end()) return 0;
        int i = mp[v];
        if (cnt[i])
        {
            mat[x][y] = i;
            --cnt[i];
            if (small(x + 1, 0)) return 1;
            ++cnt[i];
        }
    }
    else if (x == n-1)
    {
        int v = sum;
        for (int i = 0; i < n-1; ++i) v -= val[mat[i][y]];
        if (mp.find(v) == mp.end()) return 0;
        int i = mp[v];
        if (cnt[i])
        {
            mat[x][y] = i;
            --cnt[i];
            if (small(x, y + 1)) return 1;
            ++cnt[i];
        }
    }
    else
    {
        for (int i = 0; i < N; ++i) if (cnt[i])
        {
            mat[x][y] = i;
            --cnt[i];
            if (small(x, y + 1)) return 1;
            ++cnt[i];
        }
    }
    return 0;
}

int main()
{
    while (scanf("%d", &n) != EOF)
    {
        mp.clear();
        memset(g, 0, sizeof(g)); sum = tot = 0;
        for (int i = 0; i < n * n; ++i)
        {
            scanf("%d", &a[i]);
            sum += a[i];
        }
        sum /= n;
        printf("%d\n", sum);
        sort(a, a + n * n);
        for (int i = 0, j = 0; i < n * n; i = j)
        {
            for (; j < n * n && a[j] == a[i]; ++j);
            mp[a[i]] = N, val[N] = a[i], cnt[N++] = j - i;
        }
        if (n < 4)
        {
            small(0, 0);
            continue;
        }
        init(0);
        bool found = 0;
        for (int i = 0; !found && i < N; ++i) if (cnt[i])
        {
            --cnt[i]; mat[1][1] = i;
            for (int j = i; !found && j < N; ++j) if (cnt[j] && g[i][j])
            {
                --cnt[j]; mat[2][2] = j;
                for (int k = i; !found && k < N; ++k) if (cnt[k] && g[i][k] && g[j][k])
                {
                    --cnt[k]; mat[1][2] = k;
                    for (int l = i; !found && l < N; ++l) if (cnt[l] && g[k][l] && g[i][l] && g[j][l])
                    {
                        --cnt[l]; mat[2][1] = l;
                        if (dfs(0)) found = 1;
                        ++cnt[l];
                    }
                    ++cnt[k];
                }
                ++cnt[j];
            }
            ++cnt[i];
        }
    }
    return 0;
}

#include <iostream>
#include <fstream>
#include <cstdio>

#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <queue>
#include <deque>
#include <cmath>
#include <set>
#include <map>

#include <ctime>
#include <cstdlib>
#include <cassert>
#include <windows.h>

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;

const int N = (int) 4;

int n, sum, s, a[N * N], f[N][N];
int str[N], col[N], diag;
bool used[N];

void PrintAns () {
    printf("%d\n", s);
    for ( int i = 0; i < n; i++ ) {
        for ( int j = 0; j < n; j++ )
            printf("%d ", f[i][j]);
        puts("");
    }
}

bool Test ( int x, int y, int val ) {
    return true;

    if ( str[x] + val > s )
        return false;
    if ( col[y] + val > s )
        return false;
    if ( x == y && diag + val > s )
        return false;

    return true;
}

bool CheckAns () {
    for ( int i = 0; i < n; i++ ) {
        int res1 = 0, res2 = 0;
        for ( int j = 0; j < n; j++ )
            res1 += f[i][j], res2 += f[j][i];

        if ( res1 != s || res2 != s )
            return false;
    }

    int res = 0;
    for ( int i = 0; i < n; i++ )
        res += f[i][i];

    return (res == s);
}

void Brute ( int x, int y ) {
    if ( x == n )
        x = 0, y++;

    if ( y == n ) {
        if ( CheckAns() ) {
            PrintAns();
            exit(0);
        }

        return;
    }

    for ( int j = 0; j <  n * n; j++ )
        if ( !used[j] && Test(x, y, a[j]) ) {
            /*
            str[x] += a[j];
            col[y] += a[j];
            if ( x == y )
                diag += a[j];
            */

            used[j] = true;
            f[x][y] = a[j];

            Brute(x + 1, y);
            /*
            str[x] -= a[j];
            col[y] -= a[j];
            if ( x == y )
                diag -= a[j];
            */

            used[j] = false;
        }
}

int main () {
    //freopen(".in", "r", stdin);
    //freopen(".out", "w", stdout);
    //ifstream cin (".in");
    //ofstream cout(".out");

    scanf("%d", &n);
    for ( int i = 0; i < n * n; i++ ) {
        scanf("%d", a + i);
        sum += a[i];
    }

    s = sum / n;

    Brute(0, 0);

    return 0;
}

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<string.h>
#include<vector>
#include<map>
#define mod 1000000009
using namespace std;
int n;
int p[4][4];
int a[(1<<16)];
int sum1[4];
int sum2[4];
int dio1,dio2;
bool ok;
int mask;
void dfs(int x)
{
    int i,j;
    if(x==n*n)
    {
        if(dio1 == dio2 && dio1==sum1[0] && sum1[1]==dio1 && sum1[2]==dio1 && sum2[0]==dio1 && sum2[1]==dio1 && sum2[2]==dio1)
        {
            ok=true;
            printf("%d\n",dio1);
            for(i=0;i<n;i++)
            {
                printf("%d",p[i][0]);
                for(j=1;j<n;j++)
                    printf(" %d",p[i][j]);
                printf("\n");
            }
        }
        return;
    }
    for(i=0;i<n*n && !ok;i++)
        if((mask&(1<<i))==0)
        {
            mask|=(1<<i);
            p[x/n][x%n]=a[i];
            sum1[x/n]+=a[i];
            sum2[x%n]+=a[i];
            if(x/n == x%n)
                dio1+=a[i];
            if(x==2 || x==4 || x==6)
                dio2+=a[i];
            dfs(x+1);
            sum1[x/n]-=a[i];
            sum2[x%n]-=a[i];
            if(x/n == x%n)
                dio1-=a[i];
            if(x==2 || x==4 || x==6)
                dio2-=a[i];
            mask^=(1<<i);
        }
}
void dfs2(int x)
{
    int i,j;
    if(x==n*n)
    {
        if(dio1 == dio2 && dio1==sum1[0] && sum1[1]==dio1 && sum2[0]==dio1 && sum2[1]==dio1)
        {
            ok=true;
            printf("%d\n",dio1);
            for(i=0;i<n;i++)
            {
                printf("%d",p[i][0]);
                for(j=1;j<n;j++)
                    printf(" %d",p[i][j]);
                printf("\n");
            }
        }
        return;
    }
    for(i=0;i<n*n && !ok;i++)
        if((mask&(1<<i))==0)
        {
            mask|=(1<<i);
            p[x/n][x%n]=a[i];
            sum1[x/n]+=a[i];
            sum2[x%n]+=a[i];
            if(x/n == x%n)
                dio1+=a[i];
            if(x==1 || x==2)
                dio2+=a[i];
            dfs2(x+1);
            sum1[x/n]-=a[i];
            sum2[x%n]-=a[i];
            if(x/n == x%n)
                dio1-=a[i];
            if(x==1 || x==2)
                dio2-=a[i];
            mask^=(1<<i);
        }
}
int sum;
void super_dfs(int x)
{
    int i,j;
    if(x==16)
    {
        if(dio1==dio2 && dio1==sum1[0] && dio1==sum1[3])
            if(dio1==sum2[1])
            {
                ok=true;
                printf("%d\n",dio1);
                for(i=0;i<n;i++)
                {
                    printf("%d",p[i][0]);
                    for(j=1;j<n;j++)
                        printf(" %d",p[i][j]);
                    printf("\n");
                }
            }
        return;
    }
    int tem;
    int znac;
    if(x==7)
    {
        if(ok)
            return;
        tem=(1<<16)-1;
        tem^=mask;
        while(tem>0)
        {
            znac=tem-(tem&(tem-1));
            if(a[znac]==sum1[0]-sum1[1])
            {
                mask|=znac;
                p[1][3]=a[znac];
                sum1[1]=sum1[0];
                sum2[3]+=a[znac];
                super_dfs(x+1);
                sum1[1]-=a[znac];
                sum2[3]-=a[znac];
                mask^=znac;
                break;
            }
            tem&=(tem-1);
        }
        return;
    }
    if(x==8)
    {
        tem=(1<<16)-1;
        tem^=mask;
        while(tem>0 && !ok)
        {
            znac=tem-(tem&(tem-1));
            mask|=znac;
            p[(x>>2)][(x&3)]=a[znac];
            sum1[(x>>2)]+=a[znac];
            sum2[(x&3)]+=a[znac];
            if((x>>2)==(x&3))
                dio1+=a[znac];
            if(x==3 || x==6 || x==12)
                dio2+=a[znac];
            int tt=(1<<16)-1;
            tt^=mask;
            while(tt>0)
            {
                int zc=tt-(tt&(tt-1));
                if(a[zc]+sum2[0]==sum)
                {
                    sum2[0]=sum;
                    sum1[3]+=a[zc];
                    dio2+=a[zc];
                    mask|=zc;
                    super_dfs(x+1);
                    dio2-=a[zc];
                    sum2[0]-=a[zc];
                    sum1[3]-=a[zc];
                    mask^=zc;
                }
                tt&=(tt-1);
            }
            if((x>>2)==(x&3))
                dio1-=a[znac];
            if(x==3 || x==6 || x==12)
                dio2-=a[znac];
            sum1[(x>>2)]-=a[znac];
            sum2[(x&3)]-=a[znac];
            mask^=znac;
            tem&=(tem-1);
        }
        return;
    }
    if(x==9)
    {
        if(ok)
            return;
        tem=(1<<16)-1;
        tem^=mask;
        while(tem>0)
        {
            znac=tem-(tem&(tem-1));
            if(a[znac]==sum2[0]-dio2)
            {
                mask|=znac;
                p[2][1]=a[znac];
                sum2[1]+=a[znac];
                sum1[2]+=a[znac];
                dio2=sum2[0];
                super_dfs(x+1);
                sum2[1]-=a[znac];
                sum1[2]-=a[znac];
                dio2-=a[znac];
                mask^=znac;
                break;
            }
            tem&=(tem-1);
        }
        return;
    }
    if(x==11)
    {
        if(ok)
            return;
        if(sum1[1]-sum1[2]!=dio1-sum2[3])
            return;
        tem=(1<<16)-1;
        tem^=mask;
        while(tem>0)
        {
            znac=tem-(tem&(tem-1));
            if(a[znac]==dio1-sum2[3])
            {
                mask|=znac;
                p[2][3]=a[znac];
                sum2[3]=dio1;
                sum1[2]+=a[znac];
                super_dfs(13);
                sum2[3]-=a[znac];
                sum1[2]-=a[znac];
                mask^=znac;
                break;
            }
            tem&=(tem-1);
        }
        return;
    }
    if(x==13)
    {
        if(ok)
            return;
        tem=(1<<16)-1;
        tem^=mask;
        while(tem>0)
        {
            znac=tem-(tem&(tem-1));
            if(a[znac]==sum2[0]-sum2[1])
            {
                mask|=znac;
                p[3][1]=a[znac];
                sum2[1]=sum2[0];
                sum1[3]+=a[znac];
                super_dfs(x+1);
                sum2[1]-=a[znac];
                sum1[3]-=a[znac];
                mask^=znac;
                break;
            }
            tem&=(tem-1);
        }
        return;
    }
    if(x==14)
    {
        if(ok)
            return;
        tem=(1<<16)-1;
        tem^=mask;
        while(tem>0)
        {
            znac=tem-(tem&(tem-1));
            if(a[znac]==sum2[1]-sum2[2])
            {
                mask|=znac;
                p[3][2]=a[znac];
                sum2[2]=sum2[1];
                sum1[3]+=a[znac];
                super_dfs(x+1);
                sum2[2]-=a[znac];
                sum1[3]-=a[znac];
                mask^=znac;
                break;
            }
            tem&=(tem-1);
        }
        return;
    }
    tem=(1<<16)-1;
    tem^=mask;
    while(tem>0 && !ok)
    {
        znac=tem-(tem&(tem-1));
        mask|=znac;
        p[(x>>2)][(x&3)]=a[znac];
        sum1[(x>>2)]+=a[znac];
        if(x==3)
            sum=sum1[0];
        sum2[(x&3)]+=a[znac];
        if((x>>2)==(x&3))
            dio1+=a[znac];
        if(x==3 || x==6 || x==12)
            dio2+=a[znac];
        super_dfs(x+1);
        if((x>>2)==(x&3))
            dio1-=a[znac];
        if(x==3 || x==6 || x==12)
            dio2-=a[znac];
        sum1[(x>>2)]-=a[znac];
        sum2[(x&3)]-=a[znac];
        mask^=znac;
        tem&=(tem-1);
    }
}
int main()
{
    scanf("%d",&n);
    int i;
    for(i=0;i<n*n;i++)
        scanf("%d",&a[(1<<i)]);
    if(n==1)
    {
        printf("%d\n",a[1]);
        printf("%d\n",a[1]);
        return 0;
    }
    if(n==2)
    {
        for(i=0;i<n*n;i++)
            a[i]=a[(1<<i)];
        dfs2(0);
        return 0;
    }
    if(n==3)
    {
        for(i=0;i<n*n;i++)
            a[i]=a[(1<<i)];
        dfs(0);
        return 0;
    }
    if(n==4)
    {
        super_dfs(0);
        return 0;
    }
    return 0;
}
/*
4
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1

4
-53765840 41030320 -11107568 -1627952 -20587184 7851664 -82204688 69469168 3111856 12591472 31550704 22071088 -15847376 -25326992 -44286224 -34806608
*/

#include<stdio.h>
#include<map>
#include<iostream>
using namespace std;

map<int,int> mat;
int n,r,a[10][10];

bool check()
{
    int i,num1=r,num2=r;
    for(i=0;i<n;i++)
    {
        num1-=a[i][i];
        num2-=a[i][n-1-i];
    }
    if(num1!=0||num2!=0) return false;
    return true;
}
bool dfs(int row,int col)
{
    if(row==0&&col==3&&a[0][0]>a[0][1]) return false;
    if(row==n-1)
    {
        int num=r,i;
        for(i=0;i<n;i++) num-=a[n-1][i];
        if(num) return false;
        for(i=0;i<n;i++)
        {
            map<int,int>::iterator it;
            it=mat.find(a[n-1][i]);
            if(it==mat.end()) break;
            if(it->second==0) break;
            it->second--;
        }
        if(i==n&&check()) return true;
        for(i=0;i<n;i++) mat[a[n-1][i]]++;
        return false;
    }
    else if(col==n-1)
    {
        int num=r,i;
        for(i=0;i<n-1;i++) num-=a[row][i];
        map<int,int>::iterator it;
        it=mat.find(num);
        if(it==mat.end()) return false;
        if(it->second==0) return false;
        it->second--;
        a[row+1][n-1]=a[row][n-1]-num;
        a[row][n-1]=num;
        if(dfs(row+1,0)) return true;
        a[row][n-1]+=a[row+1][n-1];
        it->second++;
        return false;
    }
    else
    {
        map<int,int>::reverse_iterator it;
        for(it=mat.rbegin();it!=mat.rend();it++)
        if(it->second>0)
        {
            it->second--;
            a[row+1][col]=(a[row][col]-it->first);
            a[row][col]=it->first;
            if(dfs(row,col+1)) return true;
            a[row][col]+=a[row+1][col];
            it->second++;
        }
        return false;
    }
}
int main()
{
    //freopen("1.txt","r",stdin);
    int i,j,x;
    scanf("%d",&n);
    mat.clear();
    for(r=i=0;i<n*n;i++) {scanf("%d",&x);mat[x]++;r+=x;}
    r/=n;
    for(i=0;i<n;i++) a[0][i]=r;
    /*
    map<int,int>::iterator it;
    for(it=mat.begin();it!=mat.end();it++)
    printf("%d %d\n",it->first,it->second);
    */
    dfs(0,0);
    printf("%d\n",r);
    for(i=0;i<n;i++)
    for(j=0;j<n;j++) printf("%d%c",a[i][j],j==n-1?'\n':' ');
}

#include <bits/stdc++.h>
using namespace std;

#define all(s) s.begin(), s.end()
#define vi vector<int>
#define pb push_back
#define ii pair<int, int>
#define x first
#define y second

const int REP = 1000;

long long Rand(long long l, long long h) {
    return l + ((long long)rand() * (RAND_MAX + 1) * (RAND_MAX + 1) * (RAND_MAX + 1) +
                (long long)rand() * (RAND_MAX + 1) * (RAND_MAX + 1) +
                (long long)rand() * (RAND_MAX + 1) +
                rand()) % (h - l + 1);
}

int n, s;

long long calc(vi &v) {
    long long res = 0;
    for (int i = 0; i < n; i++) {
        int temp1 = 0;
        int temp2 = 0;
        for (int j = 0; j < n; j++) {
            temp1 += v[i * n + j];
            temp2 += v[i + j * n];
        }
        res += abs(temp1 - s) + abs(temp2 - s);
    }
    int temp1 = 0;
    int temp2 = 0;
    for (int i = 0; i < n; i++) {
        temp1 += v[i * n + i];
        temp2 += v[i * n + 2 - i];
    }
    return res + abs(temp1 - s) + abs(temp2 - s);
}

int main() {
    srand(time(NULL));
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> n;
    vi v;
    for (int i = 1; i <= n * n; i++) {
        int u;
        cin >> u;
        v.pb(u);
        s += u;
    }
    s /= n;
    if (n == 1) {
        cout << s << endl << s;
        return 0;
    }
    //cout << calc(v) << endl;
    while (1) {
        random_shuffle(all(v));
        for (int rep = 1; rep < REP; rep++) {
            int p = Rand(0, v.size() - 1);
            int q = Rand(0, v.size() - 1);
            while (q == p) {
                q = Rand(0, v.size() - 1);
            }
            long long cur = calc(v);
            swap(v[p], v[q]);
            long long nx = calc(v);
            if (cur < nx) {
                swap(v[p], v[q]);
            }
            // for (auto u : v) {
            //     cout << u << ' ';
            // }
            // cout << calc(v);
            // cout << '\n';
            if (nx == 0) {
                cout << s << '\n';
                for (int i = 0; i < n; i++) {
                    for (int j = 0; j < n; j++) {
                        cout << v[i * n + j] << ' ';
                    }
                    cout << '\n';
                }
                exit(0);
            }
        }
    }
    return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#pragma comment (linker, "/STACK:16777216")
#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <list>
#include <stack>
#include <string>
#include <fstream>
#include <math.h>
#include <limits>
#include <set>
#include <map>
#include <sstream>
#include <stdio.h>
#include <time.h>
#include <memory.h>
#include <cassert>
#include <complex>
using namespace std;

///////////////// macros and typedefs ///////////////////
#define rep(i, n) for (int i = 0, _n = (n); i < _n; ++i)
#define repd(i, n) for (int i = (n)-1; i >= 0; --i)
#define _fill(a, x) memset((a), (x), sizeof((a)))
#define DEB(k) cerr<<"debug: "#k<<"="<<k<<endl;
#define all(c) (c).begin(), (c).end()
#define mp(a, b) make_pair(a, b)
#define l(c) (int)((c).size())
#define sqr(a) ((a)*(a))
#define inf 0x7f7f7f7f
#define pb push_back
#define ppb pop_back
#define x first
#define y second
typedef long long ll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef pair<int,int> pi;

int n;
int a[16];
int d[4][4];

void solution()
{
   cin >> n;
   rep(i, n*n)
      cin >> a[i];
   int m = n*n;
   sort(a, a+m);
   do {
      rep(i, m)
         d[i/n][i%n] = a[i];
      int s = d[0][0]+d[0][1]+d[0][2];
      int s2 = d[1][0]+d[1][1]+d[1][2];
      int s3 = d[2][0]+d[2][1]+d[2][2];
      int s4 = d[0][0]+d[1][0]+d[2][0];
      int s5 = d[0][1]+d[1][1]+d[2][1];
      int s6 = d[0][2]+d[1][2]+d[2][2];
      int s7 = d[0][0]+d[1][1]+d[2][2];
      int s8 = d[0][2]+d[1][1]+d[2][0];
      if (s != s2 || s != s3 || s != s4 || s != s5 || s != s6 || s != s7 || s != s8)
         continue;
      cout << s << endl;
      rep(i, n) {
         rep(j, n)
            cout << d[i][j] << " ";
         cout << endl;
      }
      break;
   } while (next_permutation(a, a+m));
}

int main()
{
#ifdef MY_JUDGE
   freopen("my.in", "rt", stdin);
   //freopen("my.out", "wt", stdout);
#endif
   solution();
#ifdef MY_JUDGE
   double time = clock()*1./CLOCKS_PER_SEC;
   fprintf(stderr, "Time: %.2lf sec\n", time);
#endif
   return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <map>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

const int Max=100;

int a[Max];
int vis[Max];
int ma[Max][Max];
int n,sum,flag;

bool istu(){
	int i,j,t=0;
	for(i=0;i<n;i++){
		for(j=0;j<n;j++){
			t+=ma[i][j];
		}
		if(t!=sum)
			return false;
		t=0;
	}
	t=0;
	for(i=0;i<n;i++){
		t+=ma[i][i];
	}
	if(t!=sum)
		return false;
	t=0;
	for(i=0;i<n;i++){
		t+=ma[i][n-1-i];
	}
	if(t!=sum)
		return false;
	return true;
}

void dfs(int x,int y){
	if(x+1<n)
	{
		for(int i=0;i<n*n;i++){
			if(vis[i]==false){
				vis[i]=true;
				ma[x+1][y]=a[i];
				dfs(x+1,y);
				vis[i]=false;
			}
		}
	}
	else if(y+1<n){
		x=0;
		for(int i=0;i<n*n;i++){
			if(vis[i]==false){
				vis[i]=true;
				ma[x][y+1]=a[i];
				dfs(x,y+1);
				vis[i]=false;
			}
		}
	}
	else{
		if(istu() && flag==1){
				for(int i=0;i<n;i++){
					for(int j=0;j<n;j++){
						printf("%d ",ma[i][j]);
					}
					printf("\n");
				}
				flag=0;
				return;
			}
	}
}

int main()
{
	int i,j,u,t;
	while(scanf("%d",&n) == 1){
		flag=1;
		sum=0;
		for(i=0;i<n*n;i++){
			scanf("%d",&a[i]);
			sum+=a[i];
		}
		sum/=n;
		printf("%d\n",sum);
		for(t=0;t<n*n;t++){
			memset(vis,false,sizeof(vis));
			ma[0][0]=a[t];
			vis[t]=true;
			dfs(0,0);
		}
	}
	return 0;
}

#include <iostream>
#include <fstream>
#include <algorithm>
#include <locale>
#include <complex>
using namespace std;
const double maxTemp = 400000000, minTemp = 0.00001, dropRate = 0.00001;
int n, a[19], s, sumRow[9], sumCol[9], sumDia[2];

int Energy(int a[19])
{
    fill(sumRow, sumRow + n, 0);
    fill(sumCol, sumCol + n, 0);
    fill(sumDia, sumDia + 2, 0);
    for(int i = 0; i < n * n; i++)
    {
        sumRow[i / n] += a[i];
        sumCol[i % n] += a[i];
        if(i / n == i % n)
        {
            sumDia[0] += a[i];
        }
        if(i / n + i % n == n - 1)
        {
            sumDia[1] += a[i];
        }
    }
    int res = abs(sumDia[0] - s) + abs(sumDia[1] - s);
    for(int i = 0; i < n; i++)
    {
        res += abs(sumRow[i] - s) + abs(sumCol[i] - s);
    }
    return res;
}

int Delta(int a[19], int x, int y)
{
    int e = Energy(a);
    swap(a[x], a[y]);
    int ee = Energy(a);
    swap(a[x], a[y]);
    return ee - e;
}

double Prob(int delta, double temp)
{
    if(delta <= 0)
    {
        return 1;
    }
    return exp(-(double)delta / temp);
}

int main()
{
    //ifstream cin("d1.inp");
    //ofstream cout("d1.out");
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cin >> n;
    for(int i = 0; i < n * n; i++)
    {
        cin >> a[i];
    }
    s = 0;
    for(int i = 0; i < n * n; i++)
    {
        s += a[i];
    }
    s /= n;
    srand(time(NULL));
    random_shuffle(a, a + n * n);
    for(double temp = maxTemp; temp >= minTemp; temp = temp * (1 - dropRate))
    {
        int x = rand() % (n * n), y = rand() % (n * n);
        if((double)rand() / RAND_MAX <= Prob(Delta(a, x, y), temp))
        {
            swap(a[x], a[y]);
        }
    }
    cout << s << "\n";
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n; j++)
        {
            cout << a[i * n + j] << " ";
        }
        cout << "\n";
    }
    return 0;
}

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <stdlib.h>
#include <time.h>

using namespace std;

#define onlylove "My Sky"
#define mysky "TT"

#define X first
#define Y second
#define sz(A) int(A.size())
#define mpair make_pair
#define LL(x) (x << 1)
#define RR(x) ((x << 1) | 1 )
#define sqr(x) ((x)*(x))
#define Rep(i , n) for(int i = 0 , _a = n ; i < _a ; i++)
#define For( i , a , b) for(int _a = a , _b = b , i = _a ; i <= _b ; i++)
#define Ford(i , a , b) for(int _a = a , _b = b , i = _a ; i >= _b ; i--)

typedef long long ll;
typedef pair<int , int> pt;

const int maxn = 5;
int n;
int a[20];
ll target;

void ReadData() {
    cin >> n;
    Rep(i , sqr(n)) cin >> a[i];
}

int Code(int x , int y) {
    return x * n + y;
}

ll f(int a[]) {
    ll chinh = 0 , phu = 0;
    ll cot[5] , hang[5];
    ll res = 0;
    Rep(i , n) {
        hang[i] = 0;
        Rep(j , n) hang[i] += a[Code(i , j)];
        res += abs(hang[i] - target);
    }
    Rep(j , n) {
        cot[j] = 0;
        Rep(i , n) cot[j] += a[Code(i , j)];
        res += abs(cot[j] - target);
        Rep(i , n) if (i == j) chinh += a[Code(i , j)];
        Rep(i , n) if (i + j == n - 1) phu += a[Code(i , j)];
    }
    res += abs(chinh - target) + abs(phu - target);
    return res;
}

int pos[20];
int b[20];

void Rand(int a[20]) {
    Rep(i , sqr(n)) {
        a[i] = i;
    }
    random_shuffle(a , a + 1 + sqr(n));
}

void Process() {
    Rep(i , sqr(n)) target += a[i];
    target /= n;
    cout << target << "\n";
    Rep(step , 1200) {
        Rand(pos);
        For(i , 0 , sqr(n) - 1) b[i] = a[pos[i]];
        ll last = f(b);
        Rep(leo , 5000) {
            int p1 = rand() % sqr(n) , p2 = rand() % sqr(n);
            if (p1 == p2) p2 = (p2 + 1) % sqr(n);
            swap(b[p1] , b[p2]);
            ll cur = f(b);
            if (cur > last)  {
                swap(b[p1] , b[p2]);
            } else {
                last = cur;
            }
        }
        if (last == 0) {
            Rep(i , n){
                Rep(j , n) cout << b[Code(i , j)] << " "; cout << "\n";
            }
            return;
        }
    }
}

int main() {
    std ::ios_base::sync_with_stdio(false);
    cin.tie(0);

    srand(time(NULL));
   // freopen("input.inp","r",stdin);
  //  freopen("out1.out" , "w" , stdout);

    ReadData(); Process();

    return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <string.h>

using namespace std;

int b[30],c[30];
int a[4][4],j;
int n,sum,sum1;
int s[30];
int d[30],r[30];

void get(int num){
/*    ans++;
    if (ans%1000000==0)
        cerr << ans << " " << a[0][0] <<  endl;*/
    if (num==2*n && s[0]!=s[1])
        return;
    if (num==3*n && s[0]!=s[2])
        return;
    if (num==4*n && s[0]!=s[3])
        return;
    if (num==(n-1)*n + 2 && d[0]!=d[1])
        return;
    if (num==(n-1)*n + 3 && d[0]!=d[2])
        return;
    if (num==(n-1)*n + 4 && d[0]!=d[3])
        return;
    if (num==n*n){
        sum = 0;
        sum1 = 0;
        for (int i=0; i<n; i++)
            sum1+=a[i][i];
        for (int i=0; i<n; i++)
            sum += a[n-1-i][i];
        if (sum!=sum1)
            return;
        cout << sum << endl;
        for (int i=0; i<n; i++){
            for (int j=0; j<n; j++)
                cout << a[i][j] << " ";
            cout << endl;
        }
        exit(0);
    }
    for (int i=0; i<=j; i++)
        if (c[i]){
            c[i]--;
            a[num/n][num%n] = r[i];
            s[num/n] += r[i];
            d[num%n] += r[i];
            get(num+1);
            s[num/n] -= r[i];
            d[num%n] -= r[i];
            c[i]++;
        }

}

int main(void){
    //freopen("input.txt","r",stdin);//freopen("output.txt","w",stdout);

    cin >> n;
    for (int i=0; i<n*n; i++)
        cin >> b[i];

    sort(b,b+n*n);
    j = 0;
    c[0] = 1;
    r[0] = b[0];
    for (int i=1; i<n*n; i++)
        if (b[i]==b[i-1])
            c[j]++;
        else{
            j++;
            r[j]=b[i];
            c[j]++;
        }

    get(0);

    return 0;
}

#pragma comment (linker, "/STACK:16777216")
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <map>
#include <set>
#include <stack>
#include <queue>

using namespace std;

#define FOR(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
#define rep(i,n) for (int (i) = 0; (i) < (n); ++(i))
#define repd(i,n) for (int (i) = n; (i)--; )
#define fe(i,a) for (int (i) = 0; (i) < int((a).size()); ++(i))
#define mem(a, val) memset((a),val,sizeof(a))
#define inf 1000000000
#define pb push_back
#define ppb pop_back
#define all(c) (c).begin(), (c).end()
#define pi acos(-1.0)
#define sqr(a) ((a)*(a))
#define mp(a,b) make_pair((a), (b))
#define xx first
#define yy second
#define MOD 1000000007LL

typedef vector<int> vint;
typedef vector< pair<int, int> > vpii;
typedef long long LL;
typedef pair<int, int> pii;

long long a[16], n, nn, sum = 0;

int diff(int x, int y) {
  if (x != y) swap(a[x], a[y]);
  long long res = 0, s1, s2, s3, s4;
  s3 = s4 = 0;
  rep(i, n) {
    s1 = s2 = 0;
    rep(j, n) {
      s1 += a[i*n + j];
      s2 += a[j*n + i];
    }
    s3 += a[i*n + i];
    s4 += a[i*n + (n - 1 - i)];
    res += abs(sum - s1);
    res += abs(sum - s2);
  }
  res += abs(sum - s3);
  res += abs(sum - s4);

  if (x != y) swap(a[x], a[y]);
  return res;
}

void out() {
  printf("%d\n", sum);
  rep(i, n) {
    rep(j, n)
      printf("%d%c", a[i*n + j], j != n - 1 ? ' ' : '\n');
  }
  exit(0);
}

int main() {
//  freopen("input.txt", "r", stdin);

  scanf("%d", &n);
  rep(i, n*n) {
    scanf("%d", a + i);
    sum += a[i];
  }
  sum /= n;

  int x, y;
  nn = n*n;
  long long last, cur;
  while (true) {
    rep(i, 100) {
      x = rand() % nn;
      y = rand() % nn;
      swap(a[x], a[y]);
    }
    last = diff(0, 0);
    rep(t, 1000) {
      rep(i, nn)
        rep(j, nn) {
          if (last > (cur = diff(i, j))) {
            swap(a[i], a[j]);
            last = cur;
          }
          if (last == 0) out();
        }
    }
  }

  return 0;
}

#include <algorithm>
#include <iostream>
#include <cassert>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <map>
#include <set>

#define SIZE(A) ((int)A.size())
#define LENGTH(A) ((int)A.length())
#define MP(A,B) make_pair(A,B)
#define PB(A) push_back(A)

typedef long long ll;
typedef double ld;

using namespace std;

int n, m, Magic, md, pd;
int a[99], b[99], cnt[99], row[99], col[99];
int t[99][99];

void giveans()
{
 	printf("%d\n", Magic);
 	for (int i = 0; i < n; i++)
 		for (int j = 0; j < n; j++)
 			printf("%d%c", t[i][j], " \n"[j+1==n]);
	exit(0);
}

void dfs2(int, int);
void dfs(int x, int y)
{
	if (x == n)
	{
//		cerr << "here\n";
//		for (int i = 0; i < n; i++)
//			for (int j = 0; j < n; j++)
//				printf("%d%c", t[i][j], " \n"[j+1==n]);
//		puts("");
	 	if (row[n-1] == Magic && md == Magic && pd == Magic) giveans();
	}
	else
	if (x == 2 && y == 0)
		dfs(x, y+1);
	else
	if (x == 2 && y == 3)
		dfs(x+1, 0);
	else
	if (x == n-1)
	{
		int tmp, tmp1, tmp2;
		if (y == 0)
			tmp = lower_bound(b, b+m, Magic-pd)-b;
		else
			tmp = lower_bound(b, b+m, Magic-col[y])-b;

		if (b[tmp]!=Magic-col[y]) return;
		if (!cnt[tmp]) return;

		cnt[tmp]--;
		t[x][y] = b[tmp];

		if (y == n-1) md += b[tmp];
		if (!y) pd += b[tmp];
		row[x] += b[tmp];
		col[y] += b[tmp];

		//Steps
		bool ok = pd==Magic;
		if (y == 0 && ok)
		{
			tmp1 = lower_bound(b, b+m, Magic-col[0])-b;

			if (b[tmp1]!=Magic-col[0] || !cnt[tmp1]) ok = 0;
			else
			{
				cnt[tmp1]--;
				col[0] += b[tmp1];
				row[2] += b[tmp1];
				t[2][0] = b[tmp1];

				tmp2 = lower_bound(b, b+m, Magic-row[2])-b;

				if (b[tmp2]!=Magic-row[2] || !cnt[tmp2])
				{
				 	ok = 0;
				 	col[0] -= b[tmp1];
				 	row[2] -= b[tmp1];
				 	cnt[tmp1]++;
				}
				else
				{
					cnt[tmp2]--;
					t[2][3] = b[tmp2];
					col[3] += b[tmp2];
					row[2] += b[tmp2];
				}
			}
		}

		//End of Steps

		if (ok)
		{
//-------------------------------
			if (y == n-1) dfs(x+1, 0);
			else dfs(x, y+1);
//-------------------------------
		}

		if (y == 0 && ok)
		{
			cnt[tmp1]++;
			col[0] -= b[tmp1];
			row[2] -= b[tmp1];
			cnt[tmp2]++;
			col[3] -= b[tmp2];
			row[2] -= b[tmp2];
		}

		if (!y) pd -= b[tmp];
		if (y == n-1) md -= b[tmp];
		col[y] -= b[tmp];
		row[x] -= b[tmp];

		cnt[tmp]++;
	}
	else
	if (y == n-1)
	{
		int tmp = lower_bound(b, b+m, Magic-row[x])-b;
		if (b[tmp]!=Magic-row[x]) return;
		if (!cnt[tmp]) return;

		cnt[tmp]--;
		t[x][y] = b[tmp];

		if (!x) pd += b[tmp];
		col[y] += b[tmp];
		row[x] += b[tmp];

//-------------------------------
		dfs(x+1, 0);
//-------------------------------

		col[y] -= b[tmp];
		row[x] -= b[tmp];
		if (!x) pd -= b[tmp];

		cnt[tmp]++;
	}
	else
	{
		for (int i = 0; i < m; i++)
			if (cnt[i])
			{
			 	cnt[i]--;

				t[x][y] = b[i];

				row[x] += b[i];
				col[y] += b[i];
				if (x == y) md += b[i];
				if (x + y == n-1) pd += b[i];

				dfs(x, y+1);

				if (x + y == n-1) pd -= b[i];
				if (x == y) md -= b[i];
				col[y] -= b[i];
				row[x] -= b[i];

			 	cnt[i]++;
			}
	}
}

int main()
{
	srand(13451);
	#ifndef ONLINE_JUDGE
		freopen("input", "r", stdin);
		freopen("output", "w", stdout);
	#endif

	scanf("%d", &n);
	srand(rand()*10+rand()+n);

	for (int i = 0; i < n*n; i++)
		scanf("%d", a+i),
		Magic += a[i],
		b[i] = a[i];

	if (n == 1)
	{
	 	printf("%d\n%d\n", a[0], a[0]);
	 	return 0;
	}

	sort(b, b + n*n);
	m = unique(b, b + n*n)-b;
	b[m] = 1000000000;

	for (int i = 0; i < n*n; i++)
		cnt[lower_bound(b, b + m, a[i])-b]++;

	Magic /= n;

	if (n < 4) dfs2(0, 0);

	int p[200];
	for (int i = 0; i < m; i++)
		p[i] = i;
	random_shuffle(p, p+m);

	for (int i = 0; i < m; i++)
	{
		cnt[i]--;
		t[0][0] = b[i];
		row[0] += b[i];
		col[0] += b[i];
		md += b[i];

		dfs(0, 1);

		row[0] -= b[i];
		col[0] -= b[i];
		md -= b[i];
		cnt[i]++;
	}

	return 0;
}

void dfs2(int x, int y)
{
	if (x == n)
	{
//		cerr << "here\n";
//		for (int i = 0; i < n; i++)
//			for (int j = 0; j < n; j++)
//				printf("%d%c", t[i][j], " \n"[j+1==n]);
//		puts("");
	 	if (row[n-1] == Magic && md == Magic && pd == Magic) giveans();
	}
	else
	if (x == n-1)
	{
		int tmp = lower_bound(b, b+m, Magic-col[y])-b;
		if (b[tmp]!=Magic-col[y]) return;
		if (!cnt[tmp]) return;

		cnt[tmp]--;
		t[x][y] = b[tmp];

		if (y == n-1) md += b[tmp];
		if (!y) pd += b[tmp];
		row[x] += b[tmp];

		if (pd == Magic)
		{
//-------------------------------
			if (y == n-1) dfs2(x+1, 0);
			else dfs2(x, y+1);
//-------------------------------
		}

		if (!y) pd -= b[tmp];
		if (y == n-1) md -= b[tmp];
		row[x] -= b[tmp];

		cnt[tmp]++;
	}
	else
	if (y == n-1)
	{
		int tmp = lower_bound(b, b+m, Magic-row[x])-b;
		if (b[tmp]!=Magic-row[x]) return;
		if (!cnt[tmp]) return;

		cnt[tmp]--;
		t[x][y] = b[tmp];

		if (!x) pd += b[tmp];
		col[y] += b[tmp];

//-------------------------------
		dfs2(x+1, 0);
//-------------------------------

		col[y] -= b[tmp];
		if (!x) pd -= b[tmp];

		cnt[tmp]++;
	}
	else
	{
		for (int i = 0; i < m; i++)
			if (cnt[i])
			{
			 	cnt[i]--;

				t[x][y] = b[i];

				row[x] += b[i];
				col[y] += b[i];
				if (x == y) md += b[i];
				if (x + y == n-1) pd += b[i];

				dfs2(x, y+1);

				if (x + y == n-1) pd -= b[i];
				if (x == y) md -= b[i];
				col[y] -= b[i];
				row[x] -= b[i];

			 	cnt[i]++;
			}
	}
}

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

inline int next_samebit(int mask){
    int lowbit=mask&-mask;
    int slack=lowbit+mask;
    return slack|(mask^slack)/lowbit/4;
}

int n,a[16],at,row[4],col[4],val[4][4];

bool dfs(int r, int c, int mask){
    if(r==3 && c==1 && val[0][3]+val[1][2]+val[2][1]+val[3][0]!=at) return false;
    if(c==4){
        if(r==3) return val[0][0]+val[1][1]+val[2][2]+val[3][3]==at;
        c=0,++r;
    }
    for(int s=mask;s;s&=s-1){
        int x=__builtin_ctz(s);
        if(c==3 && row[r]+a[x]!=at) continue;
        if(r==3 && col[c]+a[x]!=at) continue;
        row[r]+=a[x];
        col[c]+=a[x];
        val[r][c]=a[x];
        if(dfs(r,c+1,mask^1<<x)) return true;
        row[r]-=a[x];
        col[c]-=a[x];
    }
    return false;
}

int main(){
    scanf("%d",&n);
    int m=n*n,all=0,sum=0;
    for(int i=0;i<m;all+=a[i++]) scanf("%d",a+i);
    if(n==1){
        sum=a[0];
    }else if(n==2){
        sort(a,a+m);
        do{
            sum=a[0]+a[1];
            if(sum==a[3]+a[4]
            && sum==a[0]+a[3]
            && sum==a[1]+a[4]
            && sum==a[0]+a[4]
            && sum==a[1]+a[3]) break;
        }while(next_permutation(a,a+m));
    }else if(n==3){
        sort(a,a+m);
        do{
            sum=a[0]+a[1]+a[2];
            if(sum==a[3]+a[4]+a[5]
            && sum==a[6]+a[7]+a[8]
            && sum==a[0]+a[3]+a[6]
            && sum==a[1]+a[4]+a[7]
            && sum==a[2]+a[5]+a[8]
            && sum==a[0]+a[4]+a[8]
            && sum==a[2]+a[4]+a[6]) break;
        }while(next_permutation(a,a+m));
    }else if(n==4){
        int mask=(1<<m)-1;
        for(int s=255;s<=mask;s=next_samebit(s)){
            for(int i=sum=0;i<m;i++) if(s&1<<i) sum+=a[i];
            if((all*2+sum)%10) continue;
            at=(all*2+sum)/10;
            if(dfs(0,0,mask)) break;
        }
    }
    printf("%d\n",sum);
    for(int i=0;i<n;i++){
        for(int j=0;j<n-1;j++) printf("%d ",a[i*n+j]);
        printf("%d\n",a[i*n+n-1]);
    }
}

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
const ll INF=ll(1e15);
const double alpha=0.98;
const double eps=1e-6;
int a[20],r[20],n,m,ans[20][20];
void fill(int c[])
{
    for(int i=0;i<n;i++)
        ans[i/m][i%m]=c[i];
}
ll check(int &s)
{
    int s1=0,s2=0;
    ll f=0;
    for(int i=0;i<m;i++)
        s1+=ans[i][i],s2+=ans[i][m-i-1];
    f+=abs(s1-s2);
    for(int i=0;i<m;i++)
    {
        int s3=0,s4=0;
        for(int j=0;j<m;j++)
            s3+=ans[i][j],s4+=ans[j][i];
       f+=abs(s3-s1)+abs(s4-s1)+abs(s3-s2)+abs(s4-s2)+abs(s3-s4);
    }
    s=s1;
    return f;
}
int main()
{
    srand(time(NULL));
    cin>>n;
    m=n;
    n*=n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        r[i]=a[i];
    }
    ll maxi=INF;
    double t=98.0;
    int best;
    while (t>eps && maxi)
    {
        for(int i=0;i<30;i++)
        {
            swap(r[rand()%n],r[rand()%n]);
            fill(r);
            ll cur=check(best);
            if (cur<maxi || (rand()%1001)/1000.0<exp(maxi-cur/t))
            {
                maxi=cur;
                memcpy(a,r,n*sizeof r[0]);
            } else memcpy(r,a,n*sizeof a[0]);
        }
        t*=alpha;
    }
    fill(a);
    check(best);
    cout<<best<<endl;
    for(int i=0;i<m;i++,cout<<endl)
        for(int j=0;j<m;j++)
            cout<<ans[i][j]<<" ";
    return 0;
}

//God & me
#include <bits/stdc++.h>
#define pb push_back
#define X first
#define Y second
//#define int long long
using namespace std;
template<class T,class L>bool smax(T&x,L y){return x<y?(x=y,1):0;}template<class T,class L>bool smin(T&x,L y){return x>y?(x=y,1):0;}
typedef pair<int,int> pii;

const int maxn=16;
int n,a[maxn],b[maxn][maxn],ex;
bool mark[maxn];
vector<vector<int> >all;
void bt(int s=0){
  if(s==n){
    for(int i=0,sum=0;i<n;i++,sum=0){
      for(int j=0;j<n;j++)
	sum+=b[j][i];
      if(sum!=ex)return;
    }
    {
      int sum=0;
      for(int i=0;i<n;i++)
	sum+=b[i][i];
      if(sum!=ex)return;
    }
    {
      int sum=0;
      for(int i=0;i<n;i++)
	sum+=b[i][n-i-1];
      if(sum!=ex)return;
    }
    for(int i=0;i<n;i++,cout<<'\n')
      for(int j=0;j<n;j++)
	cout<<b[i][j]<<' ';
    exit(0);
  }
  for(auto v:all){
    bool reval=1;
    for(auto x:v)
      if(mark[x]){reval=0;break;}
    if(!reval)continue;
    do{
      for(int i=0;i<n;i++)
	b[s][i]=a[v[i]],mark[v[i]]=1;
      bt(s+1);
      for(int i=0;i<n;i++)
	mark[v[i]]=0;
    }while(next_permutation(v.begin(),v.end()));
  }
}
main(){
  ios::sync_with_stdio(0),cin.tie();
  cin>>n;
  for(int i=0;i<n*n;i++)
    cin>>a[i],ex+=a[i];
  ex/=n;
  for(int i=0;i<1<<n*n;i++)
    if(__builtin_popcount(i)==n){
      int sum=0;
      for(int j=0;j<n*n;j++)
	sum+=(i>>j&1)*a[j];
      if(sum==ex){
	all.pb({});
	for(int j=0;j<n*n;j++)
	  if(i>>j&1)
	    all.back().pb(j);
	sort(all.back().begin(),all.back().end());
      }
    }
  bt();
  return 0;
}

#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <memory.h>
#include <cstring>
#include <ctime>
#include <cmath>
#include <set>
#include <map>
#include <algorithm>
using namespace std;

#define forn( i,n ) for ( int i=0; i<(int)(n); i++ )
#define pb push_back
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;

int s, n, b[7][7], a[49], used[49], n2;

bool check() {
  forn(i, n) {
    int sr = 0;
    forn(j, n) sr += b[i][j];
    if (sr != s) return false;
    sr = 0;
    forn(j, n) sr += b[j][i];
    if (sr != s) return false;
  }

  int sr = 0;
  forn(i, n) sr += b[i][i];
  if (sr != s) return false;

  sr = 0;
  forn(i, n) sr += b[i][n-1-i];
  if (sr != s) return false;

  return true;
}

void rec(int i, int j, int sr) {
  if (i == n) {
    if (!check()) return;
    forn(q, n) {
      forn(w, n) {
        if (w) printf(" ");
        printf("%d", b[q][w]);
      }
      printf("\n");
    }
    exit(0);
  }

  if (j == n-1) {
    int r1 = s - sr;
    if (i == 0 && r1 < b[0][0]) return;
    int r2 = r1;
    int r3 = r1;
    if (i == n - 1) {
      r2 = 0, r3 = 0;
      forn(z, n-1) r2 += b[z][z], r3 += b[z][n-1];
      r2 = s - r2, r3 = s - r3;
    }
    if (r1 != r2 || r1 != r3) return;
    forn(q, n2)
      if (!used[q] && a[q] == r1) {
        used[q] = true;
        b[i][j] = a[q];
        rec(i+1, 0, 0);
        used[q] = false;
        break;
      }
    return;
  }

  if (i == n-1) {
    int sw = 0;
    forn(z, n-1) sw += b[z][j];
    int t1 = s - sw;
    int t2 = t1;
    if (j == 0) {
      int ss = 0;
      forn(z, n-1) ss += b[z][n-1-z];
      t2 = s - ss;
    }

    if (t1 != t2) return;

    forn(q, n2)
      if (!used[q] && a[q] == t1) {
        used[q] = true;
        b[i][j] = a[q];
        rec(i, j+1, sr + a[q]);
        used[q] = false;
      }
    return;
  }

  int lu = a[0] - 1;
  forn(q, n2)
    if (!used[q] && a[q] != lu) {
      used[q] = true;
      b[i][j] = lu = a[q];
      rec(i, j+1, sr+lu);
      used[q] = false;
    }
}

int aa(int x) {
  return x < 0 ? -x : x;
}

int main()
{
  scanf("%d", &n);
  n2 = n*n;
  forn(i, n2) scanf("%d", &a[i]);
  sort(a, a+n2);
  reverse(a, a+n2);
  s = 0;
  forn(i, n2) s += a[i];
  s /= n;
  printf("%d\n", s);
  rec(0, 0, 0);
  return 0;
}

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

int n;
int s;
bool isOK(vector<int> &b)
{
    for (int i = 0; i < n; i++)
    {
        int sum = 0;
        for (int j = 0; j < n; j++)
            sum += b[i*n+j];
        if (sum != s)
            return false;
    }
    for (int i = 0; i < n; i++)
    {
        int sum = 0;
        for (int j = 0; j < n; j++)
            sum += b[j*n+i];
        if (sum != s)
            return false;
    }
    int sum = 0;
    for (int i = 0; i < n; i++)
        sum += b[i*n+i];
    if (sum != s)
        return false;
    sum = 0;
    for (int i = 0; i < n; i++)
        sum += b[i*n+n-1-i];
    if (sum != s)
        return false;
    return true;

}
void writeTable(vector<int> &b)
{
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
        {
            printf("%d", b[i*n+j]);
            if (j+1 < n)
                printf(" ");
            else
                printf("\n");
        }

}
int main()
{
/*  scanf("%d", &n);
    for (int i = 0; i < n*n; i++)
        scanf("%d",b[i]);
    s = 0;
    for (int i = 0; i < n*n; i++)
        s += b[i];
    s /= n;
    int v[4];
    int n8 = 1;
    for (int i = 0; i < 8; i++)
        n8 *= n;
    for (int i = 0; i < n8; i++)
    {
        int i1 = i;
        for (int j = 0; j < n; j++)
        {
            v[j] = i1%n;
            i1 /= n;
        }
        bool isOK = true;
        for (int j1 = 0; j1 < n; j1++)
            for (int j2 = j1+1; j2 < n; j2++)
                if (v[j1] == v[j2])
                    isOK = false;
        if (!isOK)
            continue;
        int sum = 0;
        for (int j = 0; j < n; j++)
            sum += b[v[j]];
        if (sum != s)
            continue;
        Ryad r;
        r.code = 0;
        for (int j = 0; j < n; j++)
        {
            r.a[j] = v[j];
            r.code += (1 << v[j]);
        }
        ryads.push_back(r);
    }
*/
    vector<int> b;
    scanf("%d", &n);
    b.assign(n*n, 0);
    s = 0;
    for (int i = 0; i < n*n; i++)
    {
        scanf("%d", &b[i]);
        s += b[i];
    }
    s /= n;
    //printf("%d\n", s);
    while (true)
    {
        //next_permutation(b.begin(), b.end());
        //writeTable(b);
        //printf("==============================|||\n");
        random_shuffle(b.begin(), b.end());
        if (isOK(b))
        {
            writeTable(b);
            return 0;
        }
    }
}

#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<sstream>
#include<deque>
#include<math.h>
#include<cstring>
#include <bitset>
#include<algorithm>
#include<stdio.h>
#include<stdlib.h>

#define all(v) v.begin(),v.end()
#define mpair make_pair

using namespace std;
typedef long double ld;
const ld epsilon = 1e-9;
typedef long long ll;

int mat[16];
void solveSmall(int n)
{
	int sz = n * n;
	sort(mat, mat + sz );
	int s = 0;
	for (int i = 0; i < sz; i++)
		s += mat[i];
	s /= n;
	vector<int> rows(n);
	vector<int> cols(n);
	do {
		for (int i = 0; i < n; i++)
		{
			rows[i] = cols[i] = 0;
		}
		for (int i = 0; i < sz; i++)
		{
			rows[i / n] += mat[i];
			cols[i % n] += mat[i];
		}
		bool does = true;
		for (int i = 0; i < n; i++)
		{
			if (rows[i] != s || cols[i] != s)
			{
				does = false;
				break;
			}

		}
		if (!does)
			continue;
		int diag = 0, diag2 = 0;
		for (int i = 0; i < n; i++)
		{
			diag += mat[i * n + i];
			diag2 += mat[n - i - 1 + i * n];
		}
		if (diag != s || diag2 != s)
			continue;
		cout << s << endl;
		for (int i = 0; i < sz; i++)
		{
			cout << mat[i];
			if (i % n != n - 1)
				cout << " ";
			else
				cout << endl;
		}
		return ;
	} while(next_permutation(mat, mat + sz));
}

int cnt[8];
bool used[16];
int te;
int S;
int SUM;
void initialize()
{
	te = S - mat[cnt[1]] - mat[cnt[2]] - mat[cnt[3]] ;
	bool found = false;
	for (int k = 0; k < 16; k++)
	{
		if (mat[k] == te && !used[k])
		{
			found = true;
		}
	}
	if (!found)
	{
		for (int i = 7; i > 3; i--)
		{
			used[cnt[i]] = false;
		}
		for (int i = 3; i >= 0; i--)
		{
			used[cnt[i]] = false;
			for (int j = cnt[i] + 1; j < 16; j++)
			{
				if (!used[j])
				{
					used[j] = true;
					if (i == 3)
					{
						te = S - mat[cnt[1]] - mat[cnt[2]] - mat[j];
						bool found = false;
						for (int k = 0; k < 16; k++)
						{
							if (mat[k] == te && !used[k])
							{
								found = true;
							}
						}
						if (!found)
						{
							used[j] = false;
							continue;
						}
					}
					cnt[i] = j;
					te = 0;

					for (int k = i + 1; k < 8; k++)
					{
						for (; te < 16; te++)
						{
							if(!used[te])
							{
								used[te] = true;
								cnt[k] = te;
								break;
							}
						}
					}
					return;
				}
			}
		}
	}
}
bool getNextCnt()
{
	for (int i = 7; i > 5; i--)
	{
		used[cnt[i]] = false;
		for (int j = cnt[i] + 1; j < 16; j++)
		{
			if (!used[j])
			{
				cnt[i] = j;
				te = 0;
				used[j] = true;
				for (int k = i + 1; k < 8; k++)
				{
					for (; te < 16; te++)
					{
						if(!used[te])
						{
							used[te] = true;
							cnt[k] = te;
							break;
						}
					}
				}
				return true;
			}
		}
	}
	used[cnt[5]] = false;
	SUM = mat[cnt[1]] + mat[cnt[2]] + mat[cnt[4]];
	for (int j = cnt[5] + 1; j < 16; j++)
	{
		if (!used[j] && SUM + mat[j] == S)
		{
			cnt[5] = j;
			te = 0;
			used[j] = true;
			for (int k = 6; k < 8; k++)
			{
				for (; te < 16; te++)
				{
					if(!used[te])
					{
						used[te] = true;
						cnt[k] = te;
						break;
					}
				}
			}
			return true;
		}
	}
	for (int i = 4; i >= 0; i--)
	{
		used[cnt[i]] = false;
		for (int j = cnt[i] + 1; j < 16; j++)
		{
			if (!used[j])
			{
				used[j] = true;
				if (i == 3)
				{
					te = S - mat[cnt[1]] - mat[cnt[2]] - mat[j];
					bool found = false;
					for (int k = 0; k < 16; k++)
					{
						if (mat[k] == te && !used[k])
						{
							found = true;
						}
					}
					if (!found)
					{
						used[j] = false;
						continue;
					}
				}
				cnt[i] = j;
				te = 0;

				for (int k = i + 1; k < 8; k++)
				{
					for (; te < 16; te++)
					{
						if(!used[te])
						{
							used[te] = true;
							cnt[k] = te;
							break;
						}
					}
				}
				return true;
			}
		}

	}
	return false;
}
void solve4()
{

	for (int i = 0; i < 8; i++)
		cnt[i] = i;
	int n = 4;
	int sz = n * n;
	int s = 0;
	for (int i = 0; i < sz; i++)
		s += mat[i];
	for (int i = 0; i <sz; i++)
		used[i] = i < 8;
	sort(mat, mat + sz);
	s /= n;
	int S = s;

	int realMat[16];
	int a, x, y, c, z, t, q, b;
	vector<int> rows(n);
	vector<int> cols(n);
	int  tmpMat[16];
	initialize();
	do {
		a = mat[cnt[0]];
		x = mat[cnt[1]];
		y = mat[cnt[2]];
		c = mat[cnt[3]];
		z = mat[cnt[4]];
		t = mat[cnt[5]];
		q = mat[cnt[6]];
		b = mat[cnt[7]];
		realMat[2] = a;
		realMat[5] = x;
		realMat[6] = y;
		realMat[7] = c;
		realMat[9] = z;
		realMat[10] = t;
		realMat[12] = q;
		realMat[15] = b;

		realMat[3] = s - z - y - q;
		realMat[4] = s - x - y - c;
		realMat[14] = s - a - y- t;
		realMat[13] = s - q - b - realMat[14];
		realMat[1] = s - x - z - realMat[13];
		realMat[0] = s - realMat[1] - realMat[2] - realMat[3];
		realMat[8] = s - realMat[0] - realMat[4] - realMat[12];
		realMat[11] = s - realMat[10] - realMat[9] - realMat[8];

		memcpy(tmpMat, realMat, sizeof(realMat));
		bool does = true;
		sort(tmpMat, tmpMat + sz);
		for (int i = 0; i < sz; i++)
		{
			if(tmpMat[i] != mat[i])
			{
				does = false;
				break;
			}
		}
		if (!does)
			continue;
		for (int i = 0; i < n; i++)
		{
			rows[i] = cols[i] = 0;
		}
		for (int i = 0; i < sz; i++)
		{
			rows[i / n] += realMat[i];
			cols[i % n] += realMat[i];
		}
		for (int i = 0; i < n; i++)
		{
			if (rows[i] != s || cols[i] != s)
			{
				does = false;
				break;
			}

		}
		if (!does)
			continue;
		int diag = 0, diag2 = 0;
		for (int i = 0; i < n; i++)
		{
			diag += realMat[i * n + i];
			diag2 += realMat[n - i - 1 + i * n];
		}
		if (diag != s || diag2 != s)
			continue;
		cout << s << endl;
		for (int i = 0; i < sz; i++)
		{
			cout << realMat[i];
			if (i % n != n - 1)
				cout << " ";
			else
				cout << endl;
		}
		return ;
	} while(getNextCnt());

}

int main()
{
	//freopen("bobi.in", "r", stdin);
	//freopen("bobi2.in", "w", stdout);
	int n;
	cin >> n;
	for (int i = 0; i < n * n; i++)
	{
		cin >> mat[i];
	}
	if (n <= 3)
	{
		solveSmall(n);
	}
	else
		solve4();
	return 0;
}

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <string>
#include <map>
#include <utility>
#include <vector>
#include <set>
#include <algorithm>

#define maxn 5
#define Fup(i, s, t) for (int i = s; i <= t; i ++)
#define Fdn(i, s, t) for (int i = s; i >= t; i --)
#define Path(i, s) for (int i = s; i; i = d[i].next)

using namespace std;

map <int, int>id, pos;
int ans[maxn][maxn];
int use[maxn * maxn], tt[maxn * maxn], ss[maxn];
bool flag;
int n, sum, S;

bool success()
{
    int s1 = 0, s2 = 0;
    Fup(i, 1, n){
        s1 += ans[i][i];
        s2 += ans[i][n - i + 1];
    }
    if (s1 == sum && s2 == sum)
        return true;
    return false;
}

void DFS(int x, int y, int s)
{
    if (x == n){
        if (success()){
            Fup(i, 1, n){
                Fup(j, 1, n - 1)
                    cout << ans[i][j] << ' ';
                cout << ans[i][n] << endl;
            }
            flag = true;
        }
        return;
    }
    if (y == n){
        if (pos[sum - s] && use[pos[sum - s]]){
            use[pos[sum - s]] --;
            ans[x][y] = sum - s;
            ss[y] += ans[x][y];
            if (x == n - 1){
                if (pos[sum - ss[y]] && use[pos[sum - ss[y]]]){
                    use[pos[sum - ss[y]]] --;
                    ans[n][y] = sum - ss[y];
                    DFS(x + 1, 1, 0);
                    if (flag)
                        return;
                    use[pos[sum - ss[y]]] ++;
                }
                ss[y] -= ans[x][y];
                use[pos[sum - s]] ++;
                return;
            }
            DFS(x + 1, 1, 0);
            if (flag)
                return;
            ss[y] -= ans[x][y];
            use[pos[sum - s]] ++;
        }
        return;
    }
    Fup(i, 1, S)
        if (use[i]){
            use[i] --;
            ans[x][y] = id[i];
            ss[y] += ans[x][y];
            if (x == n - 1){
                if (pos[sum - ss[y]] && use[pos[sum - ss[y]]]){
                    use[pos[sum - ss[y]]] --;
                    ans[n][y] = sum - ss[y];
                    DFS(x, y + 1, s + id[i]);
                    if (flag)
                        return;
                    use[pos[sum - ss[y]]] ++;
                }
                ss[y] -= ans[x][y];
                use[i] ++;
                continue;
            }
            DFS(x, y + 1, s + id[i]);
            ss[y] -= ans[x][y];
            use[i] ++;
            if (flag)
                return;
        }
}

void init()
{
    cin >> n;
    sum = 0;
    memset(use, 0, sizeof(use));
    Fup(i, 1, n * n){
        int x;
        cin >> x;
        if (!pos[x]){
            pos[x] = ++ S;
            id[S] = x;
        }
        use[pos[x]] ++;
        sum += x;
    }
}

void solve()
{
    sum /= n;
    cout << sum << endl;
    flag = false;
    DFS(1, 1, 0);
}

int main()
{
//    freopen("temp.in", "r", stdin);
//    freopen("temp.out", "w", stdout);
    ios::sync_with_stdio(false);
    init();
    solve();
    return 0;
}

# include <stdio.h>
# include <stdlib.h>

int a,sum,sum1,sum2;
int c[20];
int d[9][9];
int v[20];

void btr(int x)
{
	if(x>=a*2 && x%a==0)
	{
		sum=0;

		for(int j=0;j<a;j++)
		sum=sum+d[x/a-2][j];

		for(int j=0;j<a;j++)
		sum=sum-d[x/a-1][j];

		if(sum!=0)
		return;
	}

	if(a*a==x)
	{
		sum=0;
		sum1=0;
		sum2=0;

		for(int i=0;i<a;i++)
		{sum=sum+d[i][i]-d[i][a-1-i]; sum1=sum1+d[0][i];}

		for(int i=1;i<a;i++)
		{
			sum2=0;

			for(int j=0;j<a;j++)
			sum2=sum2+d[j][i];

			if(sum2!=sum1)
			return;
		}

		if(sum!=0)
		return;

		printf("%d\n",sum1);

		for(int i=0;i<a;i++)
		{
			for(int j=0;j<a-1;j++)
			printf("%d ",d[i][j]);

			printf("%d\n",d[i][a-1]);
		}

		exit(0);
	}

	for(int i=0;i<a*a;i++)
	if(v[i]==0)
	{
		v[i]=1;
		d[x/a][x%a]=c[i];
		btr(x+1);
		v[i]=0;
	}
}

int main()
{
	scanf("%d",&a);

	for(int i=0;i<a*a;i++)
	scanf("%d",&c[i]);

	if(a==1)
	{printf("%d\n",c[0]);return 0;}

	btr(0);
}

#include <algorithm>
#include <functional>
#include <numeric>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cassert>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <bitset>
#include <sstream>

using namespace std;

#define forn(i, n) for(int i = 0; i < int(n); ++i)
#define for1(i, n) for(int i = 1; i <= int(n); ++i)
#define ford(i, n) for(int i = int(n) - 1; i >= 0; --i)
#define fore(i, l, r) for(int i = int(l); i < int(r); ++i)
#define sz(v) int((v).size())
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define X first
#define Y second

typedef long long li;
typedef long double ld;

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 ld EPS = 1e-9;
const ld PI = 3.1415926535897932384626433832795;

const int N = 4;

struct pt{
    int X, Y;
    pt(){ X = Y = 0; }
    pt(int cX, int cY) : X(cX), Y(cY){}
};

int MAGIC;

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 bool put(int i, int j, int v){
    mn[i][j] = v;
    forn(k, szst[i][j]){
        sum[st[i][j][k]] += v;
		if(sum[st[i][j][k]] > s){
			for(int q = k; q >= 0; --q)
					sum[st[i][j][q]] -= v;
			return false;
		}
	}
	return true;
}

inline void take(int i, int j){
    forn(k, szst[i][j])
        sum[st[i][j][k]] -= mn[i][j];
}

void solve(int x, int y){
    if(y == n){
        x++;
        y = 1;
    }

    if(x == n){
        printf("%d\n", s - n*MAGIC);
        forn(i, n){
            forn(j, n){
                printf("%d", mn[i][j] - MAGIC);
                if(j + 1 != n)
                    printf(" ");
            }
            puts("");
        }

        exit(0);
    }

    forn(i, m){
        if(used[i] < a[i].Y){
            if(!put(x, y, a[i].X))
				continue;
            used[i]++;

            bool good = true;
            forn(j, szfn[x][y])
                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[n];
        solve(0, 1);
		s = INF;
		return;
    }

    forn(i, m)
        if(used[i] < a[i].Y){
            used[i]++;
            put(idx, 0, a[i].X);

            pickRow(idx + 1);

            used[i]--;
            take(idx, 0);
        }
}

int main(){
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "rt", stdin);
        //freopen("output.txt", "wt", stdout);
    #endif

    cin >> n;

    vector<int> c;
    c.resize(n*n);

    forn(i, n*n){
        scanf("%d", &c[i]);
    }

    sort(all(c));

    MAGIC = 1 - c[0];

    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] + MAGIC, cnt);
            cnt = 1;
        }else
            cnt++;
    }
    a[m++] = pt(c[n*n - 1] + MAGIC, cnt);

    forn(i, n)
    forn(j, n){
        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);
	//solve(0, 0);

    return 0;
}

#define _CRT_SECURE_NO_DEPRECATE
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <iostream>
#include <iterator>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <sstream>
#include <fstream>
#include <ctime>
#include <cstring>
#pragma comment(linker, "/STACK:66777216")
using namespace std;
#define pb push_back
#define ppb pop_back
#define pi 3.1415926535897932384626433832795028841971
#define mp make_pair
#define x first
#define y second
#define pii pair<int,int>
#define pdd pair<double,double>
#define INF 1000000000
#define FOR(i,a,b) for (int _n(b), i(a); i <= _n; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;i--)
#define all(c) (c).begin(), (c).end()
#define SORT(c) sort(all(c))
#define rep(i,n) FOR(i,1,(n))
#define rept(i,n) FOR(i,0,(n)-1)
#define L(s) (int)((s).size())
#define C(a) memset((a),0,sizeof(a))
#define VI vector <int>
#define ll long long
//#define ull unsigned long long
#define ull unsigned int

int a,b,c,d,i,j,n,m,k;
void brute() {
	int mas[4][4];
	VI num;
	C(mas);
	rept(i, n * n) {
		scanf("%d", &a);
		num.pb(a);
	}
	SORT(num);
	do {
		a = 0;
		rept(i, n) {
			rept(j, n) {
				mas[i][j] = num[a++];
			}
		}

		int s = -INF;
		bool ok = 1;
		rept(i, n) {
			int sum = 0;
			rept(j, n) sum += mas[i][j];
			if (s == -INF) s = sum; else
			if (sum != s) {
				ok = 0;
				break;
			}

			sum = 0;
			rept(j, n) sum += mas[j][i];
			if (sum != s) {
				ok = 0;
				break;
			}
		}
		int sum = 0;
		rept(i, n) sum += mas[i][i];
		if (sum != s) ok = 0;
		sum = 0;
		rept(i, n) sum += mas[n - i - 1][i];
		if (sum != s) ok = 0;
		if (ok == 1) {
			printf("%d\n", s);
			rept(i, n) {
				rept(j, n) {
					if (j) printf(" ");
					printf("%d", mas[i][j]);
				}
				printf("\n");
			}
			exit(0);
		}
	} while (next_permutation(all(num)));
}

int mas[16];
int sum[2001], num[1 << 16], inv[2001];
VI perm[2001][24], nperm[2001][24];

const int N = 1000003;
const int mod = 1000003;
ull key[N + 1];
int val[N + 1];
int nx[N + 1], last[mod], cc;
int rl[1024], r;
bool bad[17][17];
inline void add(ull k, int v) {
	int h = k % mod;
	key[cc] = k; val[cc] = v; nx[cc] = last[h]; last[h] = cc++;
}
inline int fnd(ull k) {
	for (int w = last[k % mod]; w >= 0; w = nx[w]) {
		if (key[w] == k) return val[w];
	}
	return 0;
}
//unordered_map<unsigned ll, unsigned ll> q;

void die(ull rh, int n1, int n2, int cp1, int cp2, int sum) {
	int p2 = rh % 24;
	rh /= 24;
	int p1 = rh % 24;
	rh /= 24;
	int v2 = rh % k;
	rh /= k;
	int v1 = rh;

	printf("%d\n", sum);
	rept(i, 4) {
		VI cur;
		if (!i) cur = perm[n1][cp1]; else
		if (i == 1) cur = perm[n2][cp2]; else
		if (i == 2) cur = perm[v2][p2]; else
		cur = perm[v1][p1];
		rept(j, 4) {
			if (j) printf(" ");
			printf("%d", cur[j]);
		}
		printf("\n");
	}
	exit(0);
}
int main() {
	srand(25091992);
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);

	memset(last, -1, sizeof(last));
	scanf("%d", &n);
	if (n <= 3) brute();
	rept(i, n * n) {
		scanf("%d", &mas[i]);
	}
	random_shuffle(mas, mas + n * n);

	rept(mask, 1 << (n * n)) {
		c = 0; d = 0;
		rept(i, n * n) if (mask & 1 << i) {
			c += mas[i];
			++d;
		}
		if (d != 4) continue;
		sum[k] = c;
		inv[k] = mask;
		num[mask] = ++k;

		VI cur;
		rept(i, n * n) if (mask & 1 << i) cur.pb(mas[i]);
		SORT(cur);
		a = 0;
		do {
			perm[k - 1][a++] = cur;
		} while (next_permutation(all(cur)));
	}
	int ts = 0;
	rept(i, n * n) ts += mas[i];

	rept(i, k) {
		if (ts != sum[i] * 4) continue;
		rept(j, k) {
			if (inv[i] & inv[j]) continue;
			if (sum[i] != sum[j]) continue;

			int s = sum[i];
			rept(p1, 24) {
				if (L(perm[i][p1]) == 0) break;
				const VI &cp1 = perm[i][p1];
				rept(p2, 24) {
					if (perm[j][p2].empty()) break;
					const VI &cp2 = perm[j][p2];
					ull h = 1, rh = 1;
					rept(z, 4) {
						h = h * 1234567891 + cp1[z] + cp2[z];
						rh = rh * 1234567891 + s - cp1[z] - cp2[z];
					}
					h = h * 1234567891 + cp1[0] + cp2[1];
					rh = rh * 1234567891 + s - (cp1[3] + cp2[2]);

					h = h * 1234567891 + cp1[3] + cp2[2];
					rh = rh * 1234567891 + s - (cp1[0] + cp2[1]);

					h = h * 1234567891 * (inv[i] | inv[j]);
					rh = rh * 1234567891 * ((~(inv[i] | inv[j])) & ((1 << (n * n)) - 1));

					int t = fnd(rh);
					if (t) {
						die(t, i, j, p1, p2, s);
					}

					if (i >= j) continue;
					int val = k * 24 * 24 * i + 24 * 24 * j + 24 * p1 + p2;
					add(h, val);
				}
			}
		}
	}
}

#pragma comment(linker,"/STACK:300000000")
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:4800)

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <memory.h>
#include <cstdio>
#include <sstream>
#include <deque>
#include <bitset>
#include <numeric>
#include <ctime>
#include <queue>

using namespace std;

#define show(x) cout << #x << " = " << (x) << endl;
#define fori(i,n) for(int i = 0; i < (n); i++)
#define forab(i,a,b) for(int i = (a); i <= (b); i++)
#define sz(v) int((v).size())
#define all(v) (v).begin(),(v).end()
const double pi = 3.1415926535897932384626433832795;
template<class T> T abs(const T &a) { return a >= 0 ? a : -a; };
template<class T> T sqr(const T &x) { return x * x; }
typedef pair<int,int> ii;
typedef long long ll;
///////////////////////////////////////

int n;
vector<vector<int> > b;

int needSum;

bool checkDiag()
{
    int sum1 = 0, sum2 = 0;
    fori(i,n)
    {
        sum1 += b[i][i];
        sum2 += b[i][n-i-1];
    }
    return sum1 == needSum && sum2 == needSum;
}

bool checkVert()
{
    int sum[5] = {};
    fori(i,n)
        fori(j,n)
            sum[j] += b[i][j];
    fori(i,n)
        if(sum[i] != needSum)
            return false;
    return true;
}

bool checkHor()
{
    fori(i,n)
        if(accumulate(all(b[i]),0) != needSum)
            return false;
    return true;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
#endif

    cin >> n;
    vector<int> a(n*n);
    b.resize(n,vector<int>(n));
    fori(i,n*n)
        cin >> a[i];
    sort(all(a));

    do {
        fori(i,n*n)
            b[i/n][i%n] = a[i];
        needSum = accumulate(all(b[0]),0);
        if(checkHor() && checkVert() && checkDiag())
            break;
    } while(next_permutation(all(a)));
    fori(i,n)
    {
        fori(j,n)
            cout << b[i][j] << ' ';
        cout << endl;
    }
}

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
const ll INF=ll(1e15);
const double alpha=0.99988;
const double eps=1e-6;
int a[20],r[20],n,m,ans[20][20],fir[20];
void fill(int c[])
{
    for(int i=0;i<n;i++)
        ans[i/m][i%m]=c[i];
}
ll check(int &s)
{
    int s1=0,s2=0;
    ll f=0;
    for(int i=0;i<m;i++)
        s1+=ans[i][i],s2+=ans[i][m-i-1];
    f+=abs(s1-s2);
    for(int i=0;i<m;i++)
    {
        int s3=0,s4=0;
        for(int j=0;j<m;j++)
            s3+=ans[i][j],s4+=ans[j][i];
       f+=abs(s3-s1)+abs(s4-s1)+abs(s3-s2)+abs(s4-s2)+abs(s3-s4);
    }
    s=s1;
    return f;
}
void annealing(unsigned int seed)
{
    srand(seed);
    for(int i=0;i<n;i++)
        r[i]=a[i]=fir[i];
    ll maxi=INF;
    double t=178.0;
    int best;
    while (t>eps && maxi)
    {
        for(int i=0;i<5;i++)
        {
            swap(r[rand()%n],r[rand()%n]);
            fill(r);
            ll cur=check(best);
            if (cur<maxi || (rand()%1001)/1000.0<exp(maxi-cur/t))
            {
                maxi=cur;
                memcpy(a,r,n*sizeof r[0]);
                if (!maxi)
                    break;
            } else memcpy(r,a,n*sizeof a[0]);
        }
        t*=alpha;
    }
    fill(a);
    maxi=check(best);
    if (!maxi)
    {
        cout<<best<<endl;
        for(int i=0;i<m;i++,cout<<endl)
            for(int j=0;j<m;j++)
                cout<<ans[i][j]<<" ";
        exit(0);
    }
}
int main()
{
    cin>>n;
    m=n;
    n*=n;
    for(int i=0;i<n;i++)
        cin>>fir[i];
    annealing(473571609);
    annealing(732801432);
    annealing(1840348433);
    annealing(3298343312u);
    return 0;
}
// xxxx

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <functional>
#include <algorithm>
#include <vector>

using namespace std;

int main(int argc, char *argv[])
{
    int n, m;
    cin >> n;
    m = n * n;
    long long sum = 0;
    long long a[20], p[20], c[20];
    for (int i = 0; i < m; i++) {
        cin >> a[i];
        sum += a[i];
        p[i] = i;
    }
    sum /= n;

    do {
        memset(c, 0, sizeof(c));
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                c[i] += a[p[i * n + j]];
                c[i + n] += a[p[j * n + i]];
            }
        }
        bool found = true;
        for (int i = 0; i < 2 * n; i++)
            if (c[i] != sum) { found = false; break; }
        if (!found) continue;
        long long c1 = 0, c2 = 0;
        for (int i = 0; i < n; i++) {
            c1 += a[p[i * n + i]];
            c2 += a[p[(n - i - 1) * n + i]];
        }
        if (c1 != sum || c2 != sum) continue;
        break;
    } while (next_permutation(p, p + m));

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) cout << a[p[i * n + j]] << ' ';
        cout << endl;
    }

    return 0;
}

#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<sstream>
#include<deque>
#include<math.h>
#include<cstring>
#include <bitset>
#include<algorithm>
#include<stdio.h>
#include<stdlib.h>

#define all(v) v.begin(),v.end()
#define mpair make_pair

using namespace std;
typedef long double ld;
const ld epsilon = 1e-9;
typedef long long ll;

int mat[16];
void solveSmall(int n)
{
	int sz = n * n;
	sort(mat, mat + sz );
	int s = 0;
	for (int i = 0; i < sz; i++)
		s += mat[i];
	s /= n;
	vector<int> rows(n);
	vector<int> cols(n);
	do {
		for (int i = 0; i < n; i++)
		{
			rows[i] = cols[i] = 0;
		}
		for (int i = 0; i < sz; i++)
		{
			rows[i / n] += mat[i];
			cols[i % n] += mat[i];
		}
		bool does = true;
		for (int i = 0; i < n; i++)
		{
			if (rows[i] != s || cols[i] != s)
			{
				does = false;
				break;
			}

		}
		if (!does)
			continue;
		int diag = 0, diag2 = 0;
		for (int i = 0; i < n; i++)
		{
			diag += mat[i * n + i];
			diag2 += mat[n - i - 1 + i * n];
		}
		if (diag != s || diag2 != s)
			continue;
		cout << s << endl;
		for (int i = 0; i < sz; i++)
		{
			cout << mat[i];
			if (i % n != n - 1)
				cout << " ";
			else
				cout << endl;
		}
		return ;
	} while(next_permutation(mat, mat + sz));
}

int cnt[8];
bool used[16];
int te;
bool getNextCnt()
{
	int lim = 15;
	for (int i = 7; i >= 0; i--)
	{
		used[cnt[i]] = false;
		for (int j = cnt[i] + 1; j < 16; j++)
		{
			if (!used[j])
			{
				cnt[i] = j;
				te = 0;
				used[j] = true;
				for (int k = i + 1; k < 8; k++)
				{
					for (; te < 16; te++)
					{
						if(!used[te])
						{
							used[te] = true;
							cnt[k] = te;
							break;
						}
					}
				}

				return true;
			}
		}

	}
	return false;
}
void solve4()
{

	for (int i = 0; i < 8; i++)
		cnt[i] = i;
	int n = 4;
	int sz = n * n;
	int s = 0;
	for (int i = 0; i < sz; i++)
		s += mat[i];
	for (int i = 0; i <sz; i++)
		used[i] = i < 8;
	sort(mat, mat + sz);
	s /= n;
	int realMat[16];
	int a, x, y, c, z, t, q, b;
	vector<int> rows(n);
	vector<int> cols(n);
	int  tmpMat[16];
	do {
		a = mat[cnt[0]];
		x = mat[cnt[1]];
		y = mat[cnt[2]];
		c = mat[cnt[3]];
		z = mat[cnt[4]];
		t = mat[cnt[5]];
		q = mat[cnt[6]];
		b = mat[cnt[7]];
		realMat[2] = a;
		realMat[5] = x;
		realMat[6] = y;
		realMat[7] = c;
		realMat[9] = z;
		realMat[10] = t;
		realMat[12] = q;
		realMat[15] = b;

		realMat[3] = s - z - y - q;
		realMat[4] = s - x - y - c;
		realMat[14] = s - a - y- t;
		realMat[13] = s - q - b - realMat[14];
		realMat[1] = s - x - z - realMat[13];
		realMat[0] = s - realMat[1] - realMat[2] - realMat[3];
		realMat[8] = s - realMat[0] - realMat[4] - realMat[12];
		realMat[11] = s - realMat[10] - realMat[9] - realMat[8];

		memcpy(tmpMat, realMat, sizeof(realMat));
		bool does = true;
		sort(tmpMat, tmpMat + sz);
		for (int i = 0; i < sz; i++)
		{
			if(tmpMat[i] != mat[i])
			{
				does = false;
				break;
			}
		}
		if (!does)
			continue;
		cout << s << endl;
		for (int i = 0; i < sz; i++)
		{
			cout << realMat[i];
			if (i % n != n - 1)
				cout << " ";
			else
				cout << endl;
		}
		return ;
	} while(getNextCnt());

}

int main()
{
	//freopen("bobi.in", "r", stdin);
	int n;
	cin >> n;
	for (int i = 0; i < n * n; i++)
	{
		cin >> mat[i];
	}
	if (n <= 3)
	{
		solveSmall(n);
	}
	else
		solve4();
	return 0;
}

#include <iostream>
#include <fstream>
#include <cstdio>

#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <queue>
#include <deque>
#include <cmath>
#include <set>
#include <map>

#include <ctime>
#include <cstdlib>
#include <cassert>
#include <windows.h>

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;

const int N = (int) 4;

int n, sum, s, a[N * N], f[N][N];
int str[N], col[N], diag;
bool used[N];

void PrintAns () {
    printf("%d\n", s);
    for ( int i = 0; i < n; i++ ) {
        for ( int j = 0; j < n; j++ )
            printf("%d ", f[i][j]);
        puts("");
    }
}

bool Test ( int x, int y, int val ) {
    return true;

    if ( str[x] + val > s )
        return false;
    if ( col[y] + val > s )
        return false;
    if ( x == y && diag + val > s )
        return false;

    return true;
}

bool CheckAns () {
    for ( int i = 0; i < n; i++ ) {
        int res1 = 0, res2 = 0;
        for ( int j = 0; j < n; j++ )
            res1 += f[i][j], res2 += f[j][i];

        if ( res1 != s || res2 != s )
            return false;
    }

    int res = 0;
    for ( int i = 0; i < n; i++ )
        res += f[i][i];

    return (res == s);
}

void Brute ( int x, int y ) {
    if ( x == n )
        x = 0, y++;

    if ( y == n ) {
        if ( CheckAns() ) {
            PrintAns();
            exit(0);
        }

        return;
    }

    for ( int j = 0; j <  n * n; j++ )
        if ( !used[j] && Test(x, y, a[j]) ) {
            /*
            str[x] += a[j];
            col[y] += a[j];
            if ( x == y )
                diag += a[j];
            */

            used[j] = true;
            f[x][y] = a[j];

            Brute(x + 1, y);
            /*
            str[x] -= a[j];
            col[y] -= a[j];
            if ( x == y )
                diag -= a[j];
            */

            used[j] = false;
        }
}

int main () {
    //freopen(".in", "r", stdin);
    //freopen(".out", "w", stdout);
    //ifstream cin (".in");
    //ofstream cout(".out");

    scanf("%d", &n);
    for ( int i = 0; i < n * n; i++ ) {
        scanf("%d", a + i);
        sum += a[i];
    }

    s = sum / n;

    Brute(0, 0);

    return 0;
}

#pragma comment(linker, "/STACK:16777216")
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <cmath>
#define eps 1.0e-6
#define inf 2000000000
#define forn(i, n) for(int i = 0; i < (int)n; ++i)

using namespace std;

typedef vector<int> vi;
typedef vector< vector<int> > vvi;
typedef vector<bool> vb;
typedef pair<int, int> ii;
typedef long long ll;

set<int> nums;
int a[20];
int b[20];
bool f[100500];
int r[5];
int c[5];
int sum = 0;
int n, m, lim = 12;
bool ok = false;
int d1 = 0, d2 = 0;

void rec2(int k) {
    if(ok)
        return;
    if(k == lim) {
        int ms = 0;
        c[n-1] = 0;
          forn(i, n) {
              int dn = sum - r[i];
              bool fl = false;
              if(nums.count(dn))
              forn(j, m)
              if(!f[j] && ((1<<j)&ms) == 0 && a[j] == dn) {
                  ms |= (1<<j);
                 b[lim + i] = a[j];
                 c[n-1] += a[j];
                 fl = true;
                 break;
              }
//            cout << ms << ' ' << fl << endl;
              if(!fl)
                  return;
          }
          if(d1 + b[n * n - 1]!=sum || d2 + b[n * (n - 1)]!= sum || c[n-1]!=sum)
              return;
//        cout << d1 << ' ' << d2 << endl;
//        cout << d1 + b[n * n - 1] << ' ' << d2 + b[n * (n - 1)] << ' ' << c[n-1]<< endl;
//        cout << d1 << endl;
          cout << sum << endl;
                ok = true;
                forn(i, n) {
                    forn(j, n)
                        cout << b[n * i + j] << ' ';
                    cout << endl;
                }

        return;
    }
    if(k % n == n - 1) {
        int dn = sum - c[k / n];
        if(nums.count(dn))
        forn(i, m)
            if(!f[i] && a[i] == dn) {
                int cc = k / n;
                            int cr = k % n;
                            f[i] = true;
                            c[cc] += a[i];
                            r[cr] += a[i];
                            if(cc == cr)
                                d1 += a[i];
                            if(cc == n - 1 - cr)
                                d2 += a[i];
                            b[k] = a[i];
                            rec2(k + 1);
                            c[cc] -= a[i];
                            r[cr] -= a[i];
                            if(cc == cr)
                                 d1 -= a[i];
                            if(cc == n - 1 - cr)
                                 d2 -= a[i];
                            f[i] = false;
                return;
            }
        return;
    }
    forn(i, m)
        if((!i || a[i] != a[i-1]) && !f[i]) {
            int cc = k / n;
            int cr = k % n;
            f[i] = true;
            c[cc] += a[i];
            r[cr] += a[i];
            if(cc == cr)
                d1 += a[i];
            if(cc == n - 1 - cr)
                d2 += a[i];
            b[k] = a[i];
            rec2(k + 1);
            c[cc] -= a[i];
            r[cr] -= a[i];
            if(cc == cr)
                 d1 -= a[i];
            if(cc == n - 1 - cr)
                 d2 -= a[i];
            f[i] = false;
        }
}

int main(int argc, char **argv) {
    scanf("%d", &n);
    m = n * n;
    lim = n * (n - 1);
    forn(i, m) {
        scanf("%d", a + i);
        sum += a[i];
        nums.insert(a[i]);
    }
    sort(a, a + m);
//    random_shuffle(a, a + m);
    sum /= n;
//    if(n <= 3)
//      rec(0);
//    else {
        rec2(0);
//    }
    return 0;
}

#include<iostream>
#include<algorithm>
#include <cstdlib>

using namespace std;

int n, totalSum, size;
int res[20], a[20];
int mark[20];
int sum;
int n2;

inline bool check()
{
	sum = 0;
	for (int j = 0; j < n; j++)
		sum += res[n2 - n + j];
	if (sum != totalSum)
		return false;

	sum = 0;
	for (int i = 0; i < n2; i += n + 1)
		sum += res[i];
	if (sum != totalSum)
		return false;

	for (int i = 0; i < n2; i++)
	{
		cout << res[i] << " ";
		if (i % n == n - 1)
			cout << endl;
	}
	exit(0);
}

bool backTrack(int x)
{
	if(n == 4 && x == n - 1 && res[1] == res[2])
		return false;
	if(x == n && res[n - 1] > res[0])
		return false;
	if(x > 0 && x % n == 0)
	{
		sum = 0;
		for (int i = 0; i < n; i++)
			sum += res[x - n + i];
		if (sum != totalSum)
			return false;
	}
	else if(x > n2 - n)
	{
		if(x == n2 - n + 1)
		{
			sum = 0;
			for (int i = n - 1; i < n2 - 1; i += n - 1)
				sum += res[i];
			if (sum != totalSum)
				return false;
		}

		sum = 0;
		for (int i = 0; i < n; i++)
			sum += res[x - n2 + n - 1 + i * n];
		if (sum != totalSum)
			return false;
	}
	if (x == n2)
		return check();
	for (int i = 0; i < size; i++)
		if (mark[i] != 0)
		{
			mark[i]--;
			res[x] = a[i];
			backTrack(x + 1);
			mark[i]++;
		}
}

int main()
{
	ios::sync_with_stdio(false);
	cin >> n;
	n2 = n * n;
	for (int i = 0; i < n * n; i++)
	{
		int x;
		cin >> x;
		totalSum += x;
		bool exist = false;
		for (int j = 0; j < size; j++)
			if (a[j] == x)
			{
				mark[j]++;
				exist = true;
				break;
			}
		if (!exist)
		{
			mark[size] = 1;
			a[size++] = x;
		}
	}
	totalSum /= n;
	cout << totalSum << endl;
	if(n == 1)
		cout << a[0] << endl;
	else
		backTrack(0);
	return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <map>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

const int Max=100;

int a[Max];
int vis[Max];
int ma[Max][Max];
int n,sum,flag;

bool istu(){
	int i,j,t=0;
	for(i=0;i<n;i++){
		for(j=0;j<n;j++){
			t+=ma[i][j];
		}
		if(t!=sum)
			return false;
		t=0;
	}
	t=0;
	for(i=0;i<n;i++){
		t+=ma[i][i];
	}
	if(t!=sum)
		return false;
	t=0;
	for(i=0;i<n;i++){
		t+=ma[i][n-1-i];
	}
	if(t!=sum)
		return false;
	return true;
}

void dfs(int x,int y){
	if(x+1<n)
	{
		for(int i=0;i<n*n;i++){
			if(vis[i]==false){
				vis[i]=true;
				ma[x+1][y]=a[i];
				dfs(x+1,y);
				vis[i]=false;
			}
		}
	}
	else if(y+1<n){
		x=0;
		for(int i=0;i<n*n;i++){
			if(vis[i]==false){
				vis[i]=true;
				ma[x][y+1]=a[i];
				dfs(x,y+1);
				vis[i]=false;
			}
		}
	}
	else{
		if(istu() && flag==1){
				for(int i=0;i<n;i++){
					for(int j=0;j<n;j++){
						printf("%d ",ma[i][j]);
					}
					printf("\n");
				}
				flag=0;
				return;
			}
	}
}

int main()
{
	int i,j,u,t;
	while(scanf("%d",&n) == 1){
		flag=1;
		sum=0;
		for(i=0;i<n*n;i++){
			scanf("%d",&a[i]);
			sum+=a[i];
		}
		sum/=n;
		printf("%d\n",sum);
		for(t=0;t<n*n;t++){
			memset(vis,false,sizeof(vis));
			ma[0][0]=a[t];
			vis[t]=true;
			dfs(0,0);
		}
	}
	return 0;
}

//Name         : Shinchan Nohara
//Age          : 5 years
//Organisation : Kasukabe Defense Force

#include <iostream>
#include <iostream>
#include <ctime>
#include <vector>
#include <list>
#include <queue>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <cassert>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <iterator>
#include <fstream>
using namespace std;

typedef long long 		int64;
typedef vector<int> 		vi;
typedef string 			ST;
typedef stringstream 		SS;
typedef vector< vector<int> > 	vvi;
typedef pair<int,int> 		ii;
typedef vector<string> 		vs;
/*
#ifdef __cplusplus
	#undef __cplusplus
	#define __cplusplus 199712L
#endif
#if __cplusplus > 199711L	// for g++0x, value of __cplusplus must be greater thana 199711L.
	#define tr(i, c)	for(auto i = begin(c); i != end(c); i++)
#else
	#define tr(i, c)	for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#endif
*/

#define tr(i, c)	for(__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)

#define PI		M_PI
#define E 		M_E
#define	ep		1e-9

#define	Sf		scanf
#define	Pf		printf

#define forn(i, n)	for(int i = 0, lets_stop_here = (int)n; i <  lets_stop_here; i++)
#define forab(i, a, b)	for(int i = a, lets_stop_here = (int)b; i <= lets_stop_here; i++)
#define rep(i, a, b)	for(int i = a, lets_stop_here = (int)b; i >= lets_stop_here; i--)

#define	all(c)		(c).begin(), (c).end()
#define	CL(a, b)	memset(a, b, sizeof(a))
#define mp		make_pair

#define pb		push_back

#define	present(x, c)	((c).find(x) != (c).end())	//map & set//
#define	cpresent(x, c)	(find( (c).begin(), (c).end(), x) != (c).end())	//vector & list//

#define read(n)		scanf("%d", &n)
#define write(n)	printf("%d ", n)
#define writeln(n)	printf("%d\n", n)

int main()
{
	int n;
	cin >> n;
	vi num(n*n);

	forn(i, n*n)
		cin >> num[i];

	sort(all(num));

	do {
		int sum[3] ={ 0}, sumd0 = 0, sumd1 = 0;
/*
		int sum1 = num[0] + num[1] + num[2];
		int sum2 = num[3] + num[4] + num[5];
		int sum3 = num[6] + num[7] + num[8];
		int sumd0 = num[0] + num[4] + num[8];
		int sumd1 = num[2] + num[4] + num[6];
*/
		forn(i,n) {
			forn(j, n)
				sum[i] += num[i*n + j];
			sumd0 += num[n*i + i];
			sumd1 += num[n*i + n-1-i];
		}
		if(sum[0] == sum[1] && sum[1] == sum[2] && sumd0 == sum[1] && sum[1] == sumd1) {
			cout << sumd0 << endl;
			for(int i = 0; i <n; i++) {
				for(int j = 0; j < n; j++)
					Pf("%d ", num[n*i + j]);
				cout << endl;
			}
			return 0;
		}
	} while(next_permutation(all(num)));

	return 0;
}

/**
 * @author Nikhil Kushwaha
 * @email nikhilkushwaha04@gmail.com
 * @create date 14-06-2020 09:31:29
 * @modify date 14-06-2020 09:55:17
 * @desc [description]
 */
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<ll> vl;
typedef vector<double> vd;
typedef vector<ld> vld;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<vld> vvld;

#define fast ios_base::sync_with_stdio(false)
#define FOR(i, n) for(int i=0;i<n;i++)
#define RFOR(i, n) for(int i=n-1;i>=0;i--)
#define F first
#define S second
#define u_map unordered_map
#define pb push_back
#define debug(num) cout << #num << ' ' << num
#define vv_debug(vec) cout << #vec << " is: \n"; for(auto i:vec) { for(auto j: i) cout << j << ' '; cout << '\n'; }
#define v_debug(vec) cout << #vec << " is: \n"; for(auto i:vec) { cout << i << ' '; } cout << '\n';

// Calculate a^b mod c log(N)
template<typename T1, typename T2, typename T3>
T2 x_pow_y(T1 x, T3 y, T2 mod) {

    T2 ans=1;
    ll a = x%mod;
    while(y) {
        if(y&1)
            ans=(ans*a)%mod;
        a=(a*a)%mod;
        y>>=1;
    }
    return ans;
}

// Calculate (a^-1)%c log(N)
template<typename T1, typename T2>
T2 inv_mod(T1 x, T2 mod) {
    return x_pow_y(x, mod-2, mod);
}

// Return unsorted factor
template<typename T>
vector<T> find_factors(T &n) {

    vector<T> factors;
    int i;
    for(i=1;i*i<n;i++) {
        if(n%i==0) {
            factors.pb(i);
            factors.pb(n/i);
        }
    }
    if(i*i==n)
    factors.pb(i);
    return factors;
}

// Sieve implementation for prime
// works for less than 10^7
void sieve(vi &prime, size_t max) {
    vi ar(max+1, 1);
    ar[0]=ar[1]=0;

    for(int i=2;i*i<=max;i++) {
        if(!ar[i])
        continue;
        for(int j=i*i;j<=max;j++)
            ar[j]=0;
    }

    for(int i=2;i<=max;i++){
        if(ar[i])
            prime.push_back(i);
    }
}

// Pascal's Triangle DP based solution
// For cases which requires all Coefficient
template<typename T1, typename T2>
void make_pascal_triangle_mod_p(vector<vector<T1>> &binomial_coefficient, size_t size, T2 &mod) {

    for(int i=0;i<=size;i++) {
        vector<T1> temp(i+1, 1);
        for(int j=1;j<=i/2;j++) {
            temp[i-j]=temp[j]=(binomial_coefficient[i-1][j]+binomial_coefficient[i-1][j-1])%mod;
        }
        binomial_coefficient.push_back(temp);
    }
}

// Finding Factorials mod C
template<typename T1, typename T2>
void find_factorial_mod_p(vector<T1> &fact, size_t size, T2 mod) {
    fact.resize(size+1, 1);
    for(size_t i=2;i<=size;i++) {
        fact[i]=(i*fact[i-1])%mod;
    }
}

// Finding nCr%mod in N + 3log(N)
template<typename T1, typename T2>
T2 nCr_mod_p(vector<T1> &fact, size_t n, size_t r, T2 &mod) {
    if(fact.empty())
        find_factorial(fact, n, mod);

    size_t f_n = fact[n];
    size_t f_r = inv_mod(fact[r], mod);
    size_t f_nr = inv_mod(fact[n-r], mod);
    return (f_nr*((f_n*f_r)%mod))%mod;
}

// Finding nCr
size_t nCr(size_t n, size_t r) {
    if(n-r<r)
        r=n-r;
    size_t ans=1;

    for(int i=0;i<n;i++) {
        ans *= (n-i);
        ans /= (i+1);
    }
    return ans;
}

vvi out_;

bool fill_requirements(int s, int n) {

    int r_sum[n], c_sum[n];
    memset(r_sum, 0, n*sizeof(int));
    memset(c_sum, 0, n*sizeof(int));

    for(int i=0;i<n;i++) {
        FOR(j,n) {
            r_sum[i]+=out_[i][j];
            c_sum[j]+=out_[i][j];
        }
    }
    bool flag=true;
    FOR(i,n) {
        if(r_sum[i]!=s) {
            flag=false;
            break;
        }
        if(c_sum[i]!=s) {
            flag=false;
            break;
        }
    }
    int diag_sum=0;
    FOR(i,n) {
        diag_sum+=out_[i][i];
    }
    if(diag_sum!=s)
    flag=false;
    int i=n-1,j=0;
    diag_sum=0;
    while(j!=n-1) {
        diag_sum+=out_[i--][j++];
    }
    if(diag_sum!=s)
    flag=false;
    return flag;
}

bool flag=false;

void fill_array(int s, vi &ar, int n, int i, int j, vi &vis) {
    if(i==n-1&&j==n) {
        flag=fill_requirements(s, n);
        return ;
    }
    if(j==n) {
        j=0;
        i++;
    }
    FOR(indx, n*n) {
        if(vis[indx])
        continue;
        vis[indx]=1;
        out_[i][j]=ar[indx];
        fill_array(s, ar, n, i, j+1, vis);
        if(flag) {
            return;
        }
        else
        {
            vis[indx]=0;
        }
    }
}

// Start Code Here:
void solve() {
    int n;
    cin >> n;
    vi ar(n*n);
    FOR(i,n*n)
    cin >> ar[i];
    int sum=0;
    FOR(i,n*n)
    sum+=ar[i];
    out_ = vvi (n, vi(n));
    int s = sum/n;
    vi vis(n*n);
    fill_array(s, ar, n, 0, 0, vis);
    cout << s << '\n';
    FOR(i, n)
    {
        FOR(j,n)
        cout << out_[i][j] << ' ';
        cout << '\n';
    }

}

int main() {
    fast; cin.tie(0); cout.tie(0);
    // int T;
    // cin >> T;
    // while(T--) {
    //     solve();
    // }
    solve();
    return 0;
}

#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <assert.h>
#include <math.h>
#include <string.h>
using namespace std;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<PII> VPII;
typedef long long ll;
#define FOREACH(it,vec) for(typeof((vec).begin()) it = (vec).begin(); it != (vec).end(); it++)
#define MOD(a,b) (((a)%(b)+(b))%(b))
#define REP(i,n) for (int i = 0; i < (n); i++)

int N;
int S = 0;
int a[20];

int main() {
	scanf("%d", &N);
	REP(i,N*N) {
		scanf("%d", &a[i]);
		S += a[i];
	}
	S /= N;
	if (N <= 3) {
		sort(a,a+(N*N));
		do {
			bool ok = true;
			REP(y,N) {
				int s = 0;
				REP(x,N)
					s += a[x+N*y];
				if (s != S)
					ok = false;
			}
			REP(x,N) {
				int s = 0;
				REP(y,N)
					s += a[x+N*y];
				if (s != S)
					ok = false;
			}
			int s = 0;
			REP(d,N) {
				s += a[d+N*d];
			}
			if (s != S)
				ok = false;
			s = 0;
			REP(d,N) {
				s += a[(N-d-1)+N*d];
			}
			if (s != S)
				ok = false;
			if (ok) {
				REP(y,N) {
					REP(x,N) {
						if (x)
							printf(" ");
						printf("%d", a[x+N*y]);
					}
					printf("\n");
				}
				break;
			}
		} while(next_permutation(a,a+(N*N)));
	}
	return 0;
}

#include <stdio.h>
#include <sstream>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <memory.h>
#include <queue>
#include <string>
#include <string.h>
#include <cmath>
#include <utility>
#include <bitset>
#include <time.h>
#include <climits>

#define PI 3.1415926535897932384626433832795
#define sqr(x) ((x)*(x))
#define OUT_RT cerr << (float(clock()) / CLOCKS_PER_SEC) << endl

typedef long long LL;
typedef unsigned long long ULL;

using namespace std;

long long a[22], s1[3], s2[3], s3, s4, b[22];
int n, nn, c[16], sc[16];

int main() {
//	freopen("input.txt","r",stdin);
//	freopen("output.txt","w",stdout);

	cin >> n;
	nn = n * n;
	for (int i = 0; i < nn; i++) cin >> a[i];

	if (n <= 3) {
		sort(a, a + nn);
		do {
			memset(s1, 0, sizeof(s1));
			memset(s2, 0, sizeof(s2));
			s3 = s4 = 0;

			for (int i = 0; i < nn; i++) {
				s1[i / n] += a[i];
				s2[i % n] += a[i];
			}
			for (int i = 0; i < nn; i += n + 1)
				s3 += a[i];
			for (int i = nn - n, j = 0; j < n; i -= n - 1, j++)
				s4 += a[i];

			bool good = s3 == s4;
			for (int i = 1; i < n; i++) if (s1[i] != s1[0]) good = false;
			for (int i = 1; i < n; i++) if (s2[i] != s2[0]) good = false;
			good &= s3 == s1[0] && s3 == s2[0];

			if (good) {
				cout << s3 << endl;
				for (int i = 0; i  < nn; i++) {
					if (i % n == n - 1) {
						cout << a[i] << endl;
					}else
						cout << a[i] << " ";
				}
				return 0;
			}
		} while(next_permutation(a, a + nn));
	} else
	{
		int cn = 0;
		sort(a, a + nn);
		for (int i = 0; i < nn; i++)
			if(i == 0 || a[i] != a[i-1]) {
				b[cn] = a[i];
				c[cn++] = 1;
			} else
				c[cn-1]++;

		for (int a11 = 0; a11 < cn; a11++) if (c[a11]) {
		c[a11]--;
		for (int a12 = 0; a12 < cn; a12++) if (c[a12]) {
		c[a12]--;
		for (int a13 = 0; a13 < cn; a13++) if (c[a13]) {
		c[a13]--;
		for (int a14 = 0; a14 < cn; a14++) if (c[a14]) {
		c[a14]--;
		for (int a21 = 0; a21 < cn; a21++) if (c[a21]) {
		c[a21]--;
		for (int a22 = 0; a22 < cn; a22++) if (c[a22]) {
		c[a22]--;
		for (int a23 = 0; a23 < cn; a23++) if (c[a23]) {
		c[a23]--;

		LL s = b[a11] + b[a12] + b[a13] + b[a14];
		LL b24 = s - b[a21] - b[a23] - b[a22];
		int j1 = 0;
		for (; j1 < cn; j1++) if (b[j1] == b24) break;
		if (j1 == cn || c[j1] == 0){
			c[a23]++;
			continue;
		}
		c[j1]--;

		for (int a32 = 0; a32 < cn; a32++) if (c[a32]) {
			c[a32]--;

			LL b42 = s - b[a12] - b[a22] - b[a32];
			int j2 = 0;
			for (; j2 < cn; j2++) if (b[j2] == b42) break;
			if (j2 == cn || c[j2] == 0){
				c[a32]++;
				continue;
			}
			c[j2]--;

			for (int a33 = 0; a33 < cn; a33++) if (c[a33]) {
				c[a33]--;

				LL b43 = s - b[a13] - b[a23] - b[a33];
				LL b44 = s - b[a11] - b[a22] - b[a33];
				LL b41 = s - b42 - b43 - b44;
				LL b31 = s - b[a11] - b[a21] - b41;
				LL b34 = s - b[a32] - b31 - b[a33];

				memcpy(sc, c, sizeof(c));

				int j3;
				for (j3 = 0; j3 < cn; j3++) if (b[j3] == b43 && c[j3]) {
					c[j3]--;
					break;
				}
				if (j3 < cn) {
				for (j3 = 0; j3 < cn; j3++) if (b[j3] == b44 && c[j3]) {
					c[j3]--;
					break;
				}
				if (j3 < cn) {
				for (j3 = 0; j3 < cn; j3++) if (b[j3] == b41 && c[j3]) {
					c[j3]--;
					break;
				}
				if (j3 < cn) {
				for (j3 = 0; j3 < cn; j3++) if (b[j3] == b31 && c[j3]) {
					c[j3]--;
					break;
				}
				if (j3 < cn) {
				for (j3 = 0; j3 < cn; j3++) if (b[j3] == b34 && c[j3]) {
					c[j3]--;
					break;
				}
				if (j3 < cn) {
					cout << s << endl;
					cout << b[a11] << " " << b[a12] << " " << b[a13] << " " << b[a14] << endl;
					cout << b[a21] << " " << b[a22] << " " << b[a23] << " " << b24 << endl;
					cout << b31 << " " << b[a32] << " " << b[a33] << " " << b34 << endl;
					cout << b41 << " " << b42 << " " << b43 << " " << b44 << endl;
					return 0;
				}
				}
				}
				}

				}

				memcpy(c, sc, sizeof(c));
				c[a33]++;
			}

			c[j2]++;
			c[a32]++;
		}

		c[j1]++;
		c[a23]++;
		}

		c[a22]++;
		}

		c[a21]++;
		}

		c[a14]++;
		}

		c[a13]++;
		}

		c[a12]++;
		}
		c[a11]++;
		}
	}

	return 0;
}

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
#include <algorithm>
#include <string>
#include <vector>

using namespace std;

#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define pb push_back
#define mp make_pair
#define sz(x) ((int)(x).size())

typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef pair<int, int> pii;

const int pcnt = 8;
const int px[] = { 0, 1, 2, 0, 1, 2, 0, 3 };
const int py[] = { 0, 0, 0, 1, 1, 1, 2, 3 };

const int ocnt = 8;
const int ox[] = { 3, 3, 0, 1, 1, 2, 3, 2 };
const int oy[] = { 0, 1, 3, 2, 3, 3, 2, 2 };
const int ot[] = { 2, 2, 0, 1, 0, 2, 0, 0 };

const int dx[] = { 0, 1, 1, 1 };
const int dy[] = { -1, -1, 0, 1 };

vector<pii> rems;
vi asvals;
int n, s;
vi cvals;

bool go(int cpos) {
  if (cpos >= pcnt) {
/*    if (cvals[0] != 0) return false;
    if (cvals[1] != 1) return false;
    if (cvals[2] != 0) return false;
    if (cvals[3] != 0) return false;
    if (cvals[4] != 1) return false;
    if (cvals[5] != 0) return false;
    if (cvals[6] != 1) return false;
    if (cvals[7] != 0) return false;*/

    vvi mat(n, vi(n, -2));
    vvb hval(n, vb(n, false));
    for (int i = 0; i < pcnt; i++) {
      hval[py[i]][px[i]] = true;
      mat[py[i]][px[i]] = rems[cvals[i]].first;
    }
//    eprintf("botva\n");

    vector<pii> nrems = rems;
    for (int i = 0; i < ocnt; i++) {
      int x0 = ox[i], y0 = oy[i], ty = ot[i];
      assert(!hval[y0][x0]);

      int sum = 0;
      for (int d = -3; d <= 3; d++) if (d) {
        int x = x0 + dx[ty] * d, y = y0 + dy[ty] * d;
        if (x < 0 || y < 0 || x >= n || y >= n) continue;
        assert(hval[y][x]);
        sum += mat[y][x];
      }
      int nval = s - sum;
      int id = lower_bound(asvals.begin(), asvals.end(), nval) - asvals.begin();
/*    for (int y = 0; y < n; y++)
    for (int x = 0; x < n; x++)
      eprintf("%d%c", mat[y][x], "\n "[x + 1 < n]);
      eprintf("nval(%d, %d) = %d\n", x0, y0, nval);*/

      if (id >= sz(asvals)) return false;
      if (nrems[id].first != nval) return false;
      if (!nrems[id].second) return false;
      nrems[id].second--;

      hval[y0][x0] = true;
      mat[y0][x0] = nval;
    }

    for (int y = 0; y < n; y++)
    for (int x = 0; x < n; x++)
      printf("%d%c", mat[y][x], "\n "[x + 1 < n]);
    return true;
  }

  for (int cv = 0; cv < sz(rems); cv++) if (rems[cv].second) {
    rems[cv].second--;
    cvals[cpos] = cv;
    if (go(cpos + 1)) return true;
    rems[cv].second++;
  }
  return false;
}

int main() {
  #ifdef DEBUG
  freopen("std.in", "r", stdin);
  freopen("std.out", "w", stdout);
  #endif

  while (scanf("%d", &n) >= 1) {
    vi as(n * n);
    s = 0;
    for (int i = 0; i < n * n; i++) {
      scanf("%d", &as[i]);
      s += as[i];
    }
    assert((s % n) == 0);
    s /= n;
    sort(as.begin(), as.end());

    if (n <= 3) {
      do {
        bool g = true;
        for (int y = 0; y < n; y++) {
          int cs = 0;
          for (int x = 0; x < n; x++)
            cs += as[y * n + x];
          g = g && cs == s;
        }
        for (int x = 0; x < n; x++) {
          int cs = 0;
          for (int y = 0; y < n; y++)
            cs += as[y * n + x];
          g = g && cs == s;
        }
        int cs1 = 0, cs2 = 0;
        for (int x = 0; x < n; x++) {
          cs1 += as[x * n + x];
          cs2 += as[(n - 1 - x) * n + x];
        }
        g = g && cs1 == s && cs2 == s;
        if (g) {
          printf("%d\n", s);
          for (int y = 0; y < n; y++)
          for (int x = 0; x < n; x++)
            printf("%d%c", as[y * n + x], "\n "[x + 1 < n]);
          goto end;
        }
      } while (next_permutation(as.begin(), as.end()));
    } else {
      asvals = as;
      asvals.erase(unique(asvals.begin(), asvals.end()), asvals.end());
      rems.resize(sz(asvals));
      for (int i = 0; i < sz(as); i++) {
        int id = lower_bound(asvals.begin(), asvals.end(), as[i]) - asvals.begin();
        rems[id].first = as[i];
        rems[id].second++;
      }

      cvals = vi(pcnt);
      assert(go(0));
      goto end;
    }

    assert(false);
    end:;
  }
  return 0;
}

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <string>
#include <queue>
#include <map>
#include <set>

#define MAX 4
#define MASK 65536

using namespace std;
FILE *in; FILE *out;

int n;
int vis, goal;
int s[MAX], d[MAX];
int a[MAX * MAX];
int board[MAX][MAX];
vector <int> can[4][MASK];

int recurse(int row, int col, int csum)
{
	if (row == n - 1 && col == n - 1)
	{
		int idx = 0;
		while (vis & (1 << idx)) idx++;
		board[row][col] = a[idx];
		if (csum + a[idx] != goal) return 0;
		if (s[row] + a[idx] != goal) return 0;
		if (d[0] + a[idx] != goal) return 0;
		return 1;
	}

	if (row == n - 1)
	{
		for (int i = 0; i < n * n; i++)
		{
			if ((vis & (1 << i)) == 0 && s[col] + a[i] == goal)
			{
				board[row][col] = a[i];
				vis |= (1 << i); s[col] += a[i];
				if (row == col) d[0] += a[i];
				if (row == n - col - 1) d[1] += a[i];
				int ans = recurse(row, col + 1, csum + a[i]);
				if (row == n - col - 1) d[1] -= a[i];
				if (row == col) d[0] -= a[i];
				vis ^= (1 << i); s[col] -= a[i];
				return ans;
			}
		}
		return 0;
	}

	if (col == n - 1)
	{
		for (int i = 0; i < n * n; i++)
		{
			if ((vis & (1 << i)) == 0 && csum + a[i] == goal)
			{
				board[row][col] = a[i];
				vis |= (1 << i); s[col] += a[i];
				if (row == col) d[0] += a[i];
				if (row == n - col - 1) d[1] += a[i];
				int ans = recurse(row + 1, 0, 0);
				if (row == n - col - 1) d[1] -= a[i];
				if (row == col) d[0] -= a[i];
				vis ^= (1 << i); s[col] -= a[i];
				return ans;
			}
		}
		return 0;
	}

	for (int i = 0; i < n * n; i++) if ((vis & (1 << i)) == 0)
	{
		board[row][col] = a[i];
		vis |= (1 << i); s[col] += a[i];
		if (row == col) d[0] += a[i];
		if (row == n - col - 1) d[1] += a[i];
		int ans = recurse(row, col + 1, csum + a[i]);
		if (row == n - col - 1) d[1] -= a[i];
		if (row == col) d[0] -= a[i];
		vis ^= (1 << i); s[col] -= a[i];
		if (ans) return 1;
	}
	return 0;
}

int main(void)
{
	in = stdin; out = stdout;
//	in = fopen("D.in", "rt"); out = fopen("D.out", "wt");

	fscanf(in, "%d", &n);
	for (int i = 0; i < n * n; i++)
		fscanf(in, "%d", &a[i]);

	/*
	if (n == 1)
	{
		fprintf(out, "%d\n", a[0]);
		fprintf(out, "%d\n", a[0]);
		return 0;
	}
	*/

	goal = 0; vis = 0;
	d[0] = d[1] = 0;
	s[0] = s[1] = s[2] = s[3] = 0;
	for (int i = 0; i < n * n; i++)
		goal += a[i];
	goal /= n;
	recurse(0, 0, 0);

	fprintf(out, "%d\n", goal);
	for (int i = 0; i < n; i++)
		for (int c = 0; c < n; c++)
			fprintf(out, "%d%c", board[i][c], c + 1 == n ? '\n' : ' ');

	return 0;
}

#include <cstdio>
#include <cstdlib>
#include <string>
#include <map>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
#include <ctype.h>
#include <memory.h>
using namespace std;

int a[10];
int b[10];
int c[10];

bool judge(int c[],int n)
{
  int d[5][5] = {0};
  for(int i = 1;i <= n;i ++)
    for(int j = 1;j <= n;j ++)
      d[i][j] = c[(i - 1) * n + j];
  int val = 0;
  for(int i = 1;i <= n;i ++) val += d[1][i];
  for(int i = 1;i <= n;i ++)
    {
      int s = 0;
      for(int j = 1;j <= n;j ++) s += d[i][j];
      if(s != val) return false;
    }
  for(int i = 1;i <= n;i ++)
    {
      int s = 0;
      for(int j = 1;j <= n;j ++) s += d[j][i];
      if(s != val) return false;
    }
  int s = 0;
  for(int i = 1;i <= n;i ++) s += d[i][i];
  if(s != val) return false;
  s = 0;
  for(int i = 1;i <= n;i ++) s += d[i][n - i + 1];
  if(s != val) return false;
  return true;
}

int main()
{
  int n;
  while(scanf("%d",&n) != EOF)
    {
      for(int i = 1;i <= n * n;i ++) scanf("%d",a + i);
      for(int i = 1;i <= n * n;i ++) b[i] = i;
      do
	{
	  for(int i = 1;i <= n * n;i ++)
	    {
	      c[i] = a[b[i]];
	    }
	  if(judge(c,n))
	    {
	      int val = 0;
	      for(int i = 1;i <= n;i ++) val += a[i];
	      printf("%d\n",val);
	      for(int i = 1;i <= n;i ++)
		{
		  for(int j = 1;j <= n;j ++)
		    printf("%d ",c[(i - 1) * n + j]);
		  printf("\n");
		}
	      break;
	    }
	}while(next_permutation(b + 1,b + 1 + n * n));
    }
  return 0;
}

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <string>
#include <queue>
#include <map>
#include <set>

#define MAX 4
#define MASK 65536

using namespace std;
FILE *in; FILE *out;

int n;
int vis, goal;
int s[MAX], d[MAX];
int a[MAX * MAX];
int board[MAX][MAX];
vector <int> can[4][MASK];

int recurse(int row, int col, int csum)
{
	if (row == n - 1 && col == n - 1)
	{
		int idx = 0;
		while (vis & (1 << idx)) idx++;
		if (csum + a[idx] != goal) return 0;
		if (s[row] + a[idx] != goal) return 0;
		if (d[0] + a[idx] != goal) return 0;
		return 1;
	}

	if (row == n - 1)
	{
		for (int i = 0; i < n * n; i++)
		{
			if ((vis & (1 << i)) == 0 && s[col] + a[i] == goal)
			{
				board[row][col] = a[i];
				vis |= (1 << i); s[col] += a[i];
				if (row == col) d[0] += a[i];
				if (row == n - col - 1) d[1] += a[i];
				int ans = recurse(row, col + 1, csum + a[i]);
				if (row == n - col - 1) d[1] -= a[i];
				if (row == col) d[0] -= a[i];
				vis ^= (1 << i); s[col] -= a[i];
				return ans;
			}
		}
		return 0;
	}

	if (col == n - 1)
	{
		for (int i = 0; i < n * n; i++)
		{
			if ((vis & (1 << i)) == 0 && csum + a[i] == goal)
			{
				board[row][col] = a[i];
				vis |= (1 << i); s[col] += a[i];
				if (row == col) d[0] += a[i];
				if (row == n - col - 1) d[1] += a[i];
				int ans = recurse(row + 1, 0, 0);
				if (row == n - col - 1) d[1] -= a[i];
				if (row == col) d[0] -= a[i];
				vis ^= (1 << i); s[col] -= a[i];
				return ans;
			}
		}
		return 0;
	}

	for (int i = 0; i < n * n; i++) if ((vis & (1 << i)) == 0)
	{
		board[row][col] = a[i];
		vis |= (1 << i); s[col] += a[i];
		if (row == col) d[0] += a[i];
		if (row == n - col - 1) d[1] += a[i];
		int ans = recurse(row, col + 1, csum + a[i]);
		if (row == n - col - 1) d[1] -= a[i];
		if (row == col) d[0] -= a[i];
		vis ^= (1 << i); s[col] -= a[i];
		if (ans) return 1;
	}
	return 0;
}

int main(void)
{
	in = stdin; out = stdout;
//	in = fopen("D.in", "rt"); out = fopen("D.out", "wt");

	fscanf(in, "%d", &n);
	for (int i = 0; i < n * n; i++)
		fscanf(in, "%d", &a[i]);

	if (n == 1)
	{
		fprintf(out, "%d\n", a[0]);
		fprintf(out, "%d\n", a[0]);
		return 0;
	}

	goal = 0; vis = 0;
	s[0] = s[1] = s[2] = s[3] = 0;
	for (int i = 0; i < n * n; i++)
		goal += a[i];
	goal /= n;
	recurse(0, 0, 0);

	fprintf(out, "%d\n", goal);
	for (int i = 0; i < n; i++)
		for (int c = 0; c < n; c++)
			fprintf(out, "%d%c", board[i][c], c + 1 == n ? '\n' : ' ');

	return 0;
}

#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<sstream>
#include<deque>
#include<math.h>
#include<cstring>
#include <bitset>
#include<algorithm>
#include<stdio.h>
#include<stdlib.h>

#define all(v) v.begin(),v.end()
#define mpair make_pair

using namespace std;
typedef long double ld;
const ld epsilon = 1e-9;
typedef long long ll;

int mat[16];
void solveSmall(int n)
{
	int sz = n * n;
	sort(mat, mat + sz );
	int s = 0;
	for (int i = 0; i < sz; i++)
		s += mat[i];
	s /= n;
	vector<int> rows(n);
	vector<int> cols(n);
	do {
		for (int i = 0; i < n; i++)
		{
			rows[i] = cols[i] = 0;
		}
		for (int i = 0; i < sz; i++)
		{
			rows[i / n] += mat[i];
			cols[i % n] += mat[i];
		}
		bool does = true;
		for (int i = 0; i < n; i++)
		{
			if (rows[i] != s || cols[i] != s)
			{
				does = false;
				break;
			}

		}
		if (!does)
			continue;
		int diag = 0, diag2 = 0;
		for (int i = 0; i < n; i++)
		{
			diag += mat[i * n + i];
			diag2 += mat[n - i - 1 + i * n];
		}
		if (diag != s || diag2 != s)
			continue;
		cout << s << endl;
		for (int i = 0; i < sz; i++)
		{
			cout << mat[i];
			if (i % n != n - 1)
				cout << " ";
			else
				cout << endl;
		}
		return ;
	} while(next_permutation(mat, mat + sz));
}

int cnt[8];
bool used[16];
int te;
int S;
int SUM;
void initialize()
{
	te = S - mat[cnt[1]] - mat[cnt[2]] - mat[cnt[3]] ;
	bool found = false;
	for (int k = 0; k < 16; k++)
	{
		if (mat[k] == te && !used[k])
		{
			found = true;
		}
	}
	if (!found)
	{
		for (int i = 7; i > 3; i--)
		{
			used[cnt[i]] = false;
		}
		for (int i = 3; i >= 0; i--)
		{
			used[cnt[i]] = false;
			for (int j = cnt[i] + 1; j < 16; j++)
			{
				if (!used[j])
				{
					used[j] = true;
					if (i == 3)
					{
						te = S - mat[cnt[1]] - mat[cnt[2]] - mat[j];
						bool found = false;
						for (int k = 0; k < 16; k++)
						{
							if (mat[k] == te && !used[k])
							{
								found = true;
							}
						}
						if (!found)
						{
							used[j] = false;
							continue;
						}
					}
					cnt[i] = j;
					te = 0;

					for (int k = i + 1; k < 8; k++)
					{
						for (; te < 16; te++)
						{
							if(!used[te])
							{
								used[te] = true;
								cnt[k] = te;
								break;
							}
						}
					}
					return;
				}
			}
		}
	}
}
bool getNextCnt()
{
	int lim = 15;
	for (int i = 7; i > 5; i--)
	{
		used[cnt[i]] = false;
		for (int j = cnt[i] + 1; j < 16; j++)
		{
			if (!used[j])
			{
				cnt[i] = j;
				te = 0;
				used[j] = true;
				for (int k = i + 1; k < 8; k++)
				{
					for (; te < 16; te++)
					{
						if(!used[te])
						{
							used[te] = true;
							cnt[k] = te;
							break;
						}
					}
				}
				return true;
			}
		}
	}
	used[cnt[5]] = false;
	SUM = mat[cnt[1]] + mat[cnt[2]] + mat[cnt[4]];
	for (int j = cnt[5] + 1; j < 16; j++)
	{
		if (!used[j] && SUM + mat[j] == S)
		{
			cnt[5] = j;
			te = 0;
			used[j] = true;
			for (int k = 6; k < 8; k++)
			{
				for (; te < 16; te++)
				{
					if(!used[te])
					{
						used[te] = true;
						cnt[k] = te;
						break;
					}
				}
			}
			return true;
		}
	}
	for (int i = 4; i >= 0; i--)
	{
		used[cnt[i]] = false;
		for (int j = cnt[i] + 1; j < 16; j++)
		{
			if (!used[j])
			{
				used[j] = true;
				if (i == 3)
				{
					te = S - mat[cnt[1]] - mat[cnt[2]] - mat[j];
					bool found = false;
					for (int k = 0; k < 16; k++)
					{
						if (mat[k] == te && !used[k])
						{
							found = true;
						}
					}
					if (!found)
					{
						used[j] = false;
						continue;
					}
				}
				cnt[i] = j;
				te = 0;

				for (int k = i + 1; k < 8; k++)
				{
					for (; te < 16; te++)
					{
						if(!used[te])
						{
							used[te] = true;
							cnt[k] = te;
							break;
						}
					}
				}
				return true;
			}
		}

	}
	return false;
}
void solve4()
{

	for (int i = 0; i < 8; i++)
		cnt[i] = i;
	int n = 4;
	int sz = n * n;
	int s = 0;
	int S = s;
	for (int i = 0; i < sz; i++)
		s += mat[i];
	for (int i = 0; i <sz; i++)
		used[i] = i < 8;
	sort(mat, mat + sz);
	s /= n;
	int realMat[16];
	int a, x, y, c, z, t, q, b;
	vector<int> rows(n);
	vector<int> cols(n);
	int  tmpMat[16];
	initialize();
	do {
		a = mat[cnt[0]];
		x = mat[cnt[1]];
		y = mat[cnt[2]];
		c = mat[cnt[3]];
		z = mat[cnt[4]];
		t = mat[cnt[5]];
		q = mat[cnt[6]];
		b = mat[cnt[7]];
		realMat[2] = a;
		realMat[5] = x;
		realMat[6] = y;
		realMat[7] = c;
		realMat[9] = z;
		realMat[10] = t;
		realMat[12] = q;
		realMat[15] = b;

		realMat[3] = s - z - y - q;
		realMat[4] = s - x - y - c;
		realMat[14] = s - a - y- t;
		realMat[13] = s - q - b - realMat[14];
		realMat[1] = s - x - z - realMat[13];
		realMat[0] = s - realMat[1] - realMat[2] - realMat[3];
		realMat[8] = s - realMat[0] - realMat[4] - realMat[12];
		realMat[11] = s - realMat[10] - realMat[9] - realMat[8];

		memcpy(tmpMat, realMat, sizeof(realMat));
		bool does = true;
		sort(tmpMat, tmpMat + sz);
		for (int i = 0; i < sz; i++)
		{
			if(tmpMat[i] != mat[i])
			{
				does = false;
				break;
			}
		}
		if (!does)
			continue;
		for (int i = 0; i < n; i++)
		{
			rows[i] = cols[i] = 0;
		}
		for (int i = 0; i < sz; i++)
		{
			rows[i / n] += realMat[i];
			cols[i % n] += realMat[i];
		}
		for (int i = 0; i < n; i++)
		{
			if (rows[i] != s || cols[i] != s)
			{
				does = false;
				break;
			}

		}
		if (!does)
			continue;
		int diag = 0, diag2 = 0;
		for (int i = 0; i < n; i++)
		{
			diag += realMat[i * n + i];
			diag2 += realMat[n - i - 1 + i * n];
		}
		if (diag != s || diag2 != s)
			continue;
		cout << s << endl;
		for (int i = 0; i < sz; i++)
		{
			cout << realMat[i];
			if (i % n != n - 1)
				cout << " ";
			else
				cout << endl;
		}
		return ;
	} while(getNextCnt());

}

int main()
{
	//freopen("bobi.in", "r", stdin);
	//freopen("bobi2.in", "w", stdout);
	int n;
	cin >> n;
	for (int i = 0; i < n * n; i++)
	{
		cin >> mat[i];
	}
	if (n <= 3)
	{
		solveSmall(n);
	}
	else
		solve4();
	return 0;
}

#include<iostream>
#include<algorithm>
#include <cstdlib>

using namespace std;

int n, totalSum, size;
int res[20], a[20];
bool mark[20];
int sum;
int n2;

inline void check()
{
	for (int i = 0; i < n2; i++)
	{
		cout << res[i] << " ";
		if (i % n == n - 1)
			cout << endl;
	}
	exit(0);
}

void backTrack(int x)
{
	if(x == 3 && res[2] > res[1])
		return;
	if(x == 4 && res[3] > res[0])
		return;
	if(x == 4 || x == 8 || x == 12)
	{
		sum = 0;
		for (int i = 0; i < 4; i++)
			sum += res[x - 4 + i];
		if (sum != totalSum)
			return;
	}
	if(x >= 12)
	{
		for(int j = 0; j < 4; j++)
		{
			sum = res[j] + res[j + 4] + res[j + 8];
			bool flag = false;
			for(int i = 0; i < 16; i++)
				if(!mark[i] && a[i] == totalSum - sum)
				{
					res[j + 13] = a[i];
					mark[i] = true;
					flag = true;
					break;
				}
			if(!flag)
				return;
		}
		if(res[0] + res[5] + res[10] + res[15] != totalSum || res[3] + res[6] + res[9] + res[12] != totalSum)
			return;
		check();
	}
	for (int i = 0; i < 16; i++)
		if (!mark[i])
		{
			mark[i] = true;
			res[x] = a[i];
			backTrack(x + 1);
			mark[i] = false;
		}
}

bool check3()
{
	// ROWS
	for (int i = 0; i < n; i++)
	{
		int sum = 0;
		for (int j = 0; j < n; j++)
			sum += res[i * n + j];
		if (sum != totalSum)
			return false;
	}
	// COLUMNS
	for (int j = 0; j < n; j++)
	{
		int sum = 0;
		for (int i = 0; i < n; i++)
			sum += res[i * n + j];
		if (sum != totalSum)
			return false;
	}
	// MAIN DIAG
	int mdsum = 0;
	for (int i = 0; i < n * n; i += n + 1)
		mdsum += res[i];
	// SEC DIAG
	int sdsum = 0;
	for (int i = n - 1; i < n * n - 1; i += n - 1)
		sdsum += res[i];
	if (mdsum != totalSum || sdsum != totalSum)
		return false;
	// OUTPUT
	for (int i = 0; i < n * n; i++)
	{
		cout << res[i] << " ";
		if (i % n == n - 1)
			cout << endl;
	}
	exit(0);
}

bool backTrack3(int x)
{
	//	cout << x << endl;
	if (x == n * n)
		return check3();
	for (int i = 0; i < n * n; i++)
		if (!mark[i])
		{
			mark[i] = true;
			res[x] = a[i];
			backTrack3(x + 1);
			mark[i] = false;
		}
}

int main()
{
	ios::sync_with_stdio(false);
	cin >> n;
	n2 = n * n;
	for (int i = 0; i < n * n; i++)
	{
		cin >> a[i];
		totalSum += a[i];
	}
	random_shuffle(a, a + n);
	totalSum /= n;
	cout << totalSum << endl;
	if(n == 1)
		cout << a[0] << endl;
	else if(n < 4)
		backTrack3(0);
	else
		backTrack(0);

	return 0;
}

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#define in(x) scanf("%d",&x)
using namespace std;
const int N=100005;
int n;
int num[17];
void juge2()
{
     printf("%d %d\n",num[0],num[3]);
     printf("%d %d\n",num[2],num[1]);
 }
void juge3()
{
     printf("%d %d %d\n",num[1],num[6],num[5]);
     printf("%d %d %d\n",num[8],num[4],num[0]);
     printf("%d %d %d\n",num[3],num[2],num[7]);
 }
void juge4()
{
 }
int main()
{
    while(scanf("%d",&n)==1)
    {
       for(int i=0;i<n*n;i++)in(num[i]);
       sort(num,num+n*n);
       if(n==1)
       printf("%d\n",num[0]);
       else if(n==2)
       juge2();
       else if(n==3)
       juge3();

    }
}

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

long long S;
int n;
int a[32];

bool us[32];

int t[4][4];

bool cmag()
{
	int i, j, k, s;
			s = 0;
			for ( i = 0; i < n; i++ )
				s = s + t[0][i];

			for ( i = 0; i < n; i++ )
			{
				k = 0;
				for ( j = 0; j < n; j++ )
					k = k + t[i][j];

				if ( k != s ) return 0;

				k = 0;
				for ( j = 0; j < n; j++ )
				{
					k = k + t[j][i];
				}
				if ( k != s ) return 0;
			}

	k = 0;
	for ( i = 0; i < n; i++ )
	{
		k = k + t[i][i];
	}
	if ( k != s ) return 0;

	 k = 0;
	for ( i = 0; i < n; i++ )
	{
		k = k + t[i][n-i-1];
	}
	if ( k != s ) return 0;

	return 1;
}

inline bool mark( int val )
{
	for ( int i = 0; i < n*n; i++ )
	{
		if ( ( a[i] == val ) && ( us[i] == 0 ) )
		{
			us[i] = 1;
			return 1;
		}
	}
	return 0;
}

inline void unmark( int val )
{
	int i;
	for ( i = 0; i < n*n; i++ )
	{
		if ( ( a[i] == val ) && ( us[i] == 1 ) )
		{
			us[i] = 0;
			break;
		}
	}
//	if ( i >= n*n ) while ( 1 ); // ne vliza tuk.
}

bool make4( int st )
{
	if ( st > 7 )
	{
		return cmag();
	}
	if ( st < 3 )
	{
		for ( int i = 0; i < n*n; i++ )
		{
			if ( us[i] == 0 )
			{
				us[i] = 1;
				t[0][st] = a[i];
				if ( st == 2 )
				{
					t[0][3] = S - t[0][0] - t[0][1] - t[0][2];
					if ( mark( t[0][3] ) )
					{
						if ( make4( st+1 ) ) return 1;
						unmark( t[0][3] );
					}
				}
				else
				{
					if ( make4( st+1 ) )
						return 1;
				}
				us[i] = 0;
			}
		}
		return 0;
	}
	else if ( ( st == 3 ) || ( st == 4 ) )
	{
		for ( int i = 0; i < n*n; i++ )
		{
			if ( us[i] == 0 )
			{
				us[i] = 1;
				t[st-2][0] = a[i];
				if ( st == 4 )
				{
					t[3][0] = S - t[0][0] - t[1][0] - t[2][0];
					if ( mark( t[3][0] ) )
					{
						if ( make4( st+1 ) ) return 1;
						unmark( t[3][0] );
					}
				}
				else
				{
					if ( make4( st+1 ) )
						return 1;
				}
				us[i] = 0;
			}
		}
		return 0;
	}
	else if ( st == 5 )
	{
		for ( int i = 0; i < n*n; i++ )
		{
			if ( us[i] == 0 )
			{
				us[i] = 1;
				t[1][2] = a[i];
				t[2][1] = S - t[3][0] - t[1][2] - t[0][3];
				if ( mark( t[2][1] ) )
				{
					if ( make4( st+1 ) )
						return 1;
					unmark( t[2][1] );
				}
				us[i] = 0;
			}
		}
		return 0;
	}
	else if ( st == 6 )
	{
		for ( int i = 0; i < n*n; i++ )
		{
			if ( us[i] == 0 )
			{
				us[i] = 1;
				t[2][2] = a[i];
				t[2][3] = S - t[2][0] - t[2][1] - t[2][2];
				t[3][2] = S - t[0][2] - t[1][2] - t[2][2];
				if ( mark( t[3][2] ) )
				{
					if ( mark( t[2][3] ) )
					{
						if ( make4( st+1 ) )
							return 1;
						unmark( t[2][3] );
					}
					unmark( t[3][2] );
				}
				us[i] = 0;
			}
		}
		return 0;
	}
	else if ( st == 7 )
	{
		for ( int i = 0; i < n*n; i++ )
		{
			if ( us[i] == 0 )
			{
				us[i] = 1;
				t[1][1] = a[i];
				t[1][3] = S - t[1][0] - t[1][1] - t[1][2];
				t[3][1] = S - t[0][1] - t[1][1] - t[2][1];
				t[3][3] = S - t[0][0] - t[1][1] - t[2][2];

				if ( mark( t[3][1] ) )
				{
					if ( mark( t[1][3] ) )
					{
						if ( mark( t[3][3] ) )
						{
							if ( make4( st+1 ) )
								return 1;
							unmark( t[3][3] );
						}
						unmark( t[1][3] );
					}
					unmark( t[3][1] );
				}
				us[i] = 0;
			}
		}
		return 0;
	}
	return 0;
}

int main()
{
	int i, j;
	int s;

	scanf( "%d", &n );
	S = 0;
	for ( i = 0; i < n*n; i++ )
	{
		scanf( "%d", &a[i] );
		S = S + a[i];
	}

	S = S / n;

	memset( us, 0, sizeof( us ) );

	if ( n <= 3 )
	{
		sort( a, a + n*n );
		do
		{
			for ( i = 0; i < n; i++ )
			{
				for ( j = 0; j < n; j++ )
				{
					t[i][j] = a[ n*i + j ];
				}
			}

			if ( cmag() ) break;
		}
		while ( next_permutation( a, a + n*n ) );
	}
	else
	{
		make4( 0 );
	}

			s = 0;
			for ( i = 0; i < n; i++ )
				s = s + t[i][0];

	if ( ( S != s ) )
	{
		while ( 1 ); // vliza tuka -> ne e qko!
	}

	printf( "%d\n", s );
	for ( i = 0; i < n; i++ )
	{
		printf( "%d", t[i][0] );
		for ( j = 1; j < n; j++ )
			printf( " %d", t[i][j] );
		printf( "\n" );
	}
	return 0;
}

/*
Author: LJQ
PROG: d
DATE: 2012.4.28
*/
#include <cstdio>
#include <cstring>

using namespace std;

const int maxl = 5;
const int maxn = 17;
const int maxh = 3001;

struct Thash{
    int hash[maxh];
    int next[maxn];
    int key[maxn];
    int cnt[maxn];
    int hs;
    void Init(){
        memset(hash, -1, sizeof(hash)); hs = 0;
    }
    void Add(int x){
        int s = x % maxh;
        if (s < 0) s += maxh;
        for (int i = hash[s]; i != -1; i = next[i])
            if (key[i] == x){
                cnt[i] ++; return;
            }
        key[hs] = x; cnt[hs] = 1; next[hs] = hash[s]; hash[s] = hs ++;
    }
    int Check(int x){
        int s = x % maxh;
        if (s < 0) s += maxh;
        for (int i = hash[s]; i != -1; i = next[i])
            if (key[i] == x){
                if (cnt[i]){
                    cnt[i] --; return 1;
                }
                return 0;
            }
        return 0;
    }
} Set;

int vis[maxn];
int num[maxn];
int ans[maxl][maxl];
int n, m, s;
int sum;

void init()
{
    scanf("%d", &n); s = n * n;
    Set.Init();
    for (int i = 1; i <= s; i ++){
        scanf("%d", &num[i]);
        sum += num[i];
        Set.Add(num[i]);
    }
    sum /= n; m = n - 1;
}

bool checksum()
{
    int cnt = 0, cnt1 = 0, cnt2 = 0;
    for (int i = 1; i <= n; i ++){
        cnt += ans[i][i];
        cnt1 += ans[n][i];
        cnt2 += ans[i][n - i + 1];
    }
    if (cnt != sum || cnt1 != sum || cnt2 != sum) return 0;
    return 1;
}

bool checkit()
{
    int w[7], ws = 0;
    for (int i = 1; i <= n; i ++){
        int cnt = sum, flag = 0;
        for (int j = 1; j <= m; j ++)
            cnt -= ans[j][i];
        if (!Set.Check(cnt)){
            for (int i = 1; i <= ws; i ++) Set.Add(w[i]);
            return 0;
        }
        ans[n][i] = cnt;
        w[++ ws] = cnt;
    }
    if (!checksum()){
        for (int i = 1; i <= ws; i ++) Set.Add(w[i]);
        return 0;
    }
    return 1;
}

bool checkleft(int t)
{
    int cnt = ans[1][t] + ans[2][t];
    cnt = sum - cnt;
    for (int i = 0; i < Set.hs; i ++) if (Set.cnt[i] > 0)
        for (int j = i+1; j < Set.hs; j ++)
            if (Set.cnt[j] > 0 && Set.key[i] + Set.key[j] == cnt) return 1;
    return 0;
}

bool dfs(int x, int y, int s)
{
    if (x > m) return checkit();
    for (int i = 0; i < Set.hs; i ++)
        if (Set.cnt[i] > 0){
            Set.cnt[i] --;
            ans[x][y] = Set.key[i];
            if (n == 4 && x == 2){
                if (!checkleft(x)){
                    Set.cnt[i] ++; continue;
                }
            }
            if (y < m){
                if (dfs(x, y + 1, s + ans[x][y])) return 1;
            } else if (Set.Check(sum - s - ans[x][y])){
                ans[x][n] = sum - s - ans[x][y];
                if (dfs(x + 1, 1, 0)) return 1;
                Set.Add(sum - s - ans[x][y]);
            }
            Set.cnt[i] ++;
        }
    return 0;
}

void work()
{
    dfs(1, 1, 0);
}

void print()
{
    printf("%d\n", sum);
    for (int i = 1; i <= n; i ++){
        for (int j = 1; j < n; j ++) printf("%d ", ans[i][j]);
        printf("%d\n", ans[i][n]);
    }
}

int main()
{
    init();
    work();
    print();
    return 0;
}

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

inline int next_samebit(int mask){
    int lowbit=mask&-mask;
    int slack=lowbit+mask;
    return slack|(mask^slack)/lowbit/4;
}

int n,a[16],at,row[4],col[4],val[4][4];

bool dfs(int r, int c, int mask){
    if(r==3 && c==1 && val[0][3]+val[1][2]+val[2][1]+val[3][0]!=at) return false;
    if(c==4){
        if(r==3) return val[0][0]+val[1][1]+val[2][2]+val[3][3]==at;
        c=0,++r;
    }
    for(int s=mask;s;s&=s-1){
        int x=__builtin_ctz(s);
        if(c==3 && row[r]+a[x]!=at) continue;
        if(r==3 && col[c]+a[x]!=at) continue;
        row[r]+=a[x];
        col[c]+=a[x];
        val[r][c]=a[x];
        if(dfs(r+1,c,s^1<<x)) return true;
        row[r]-=a[x];
        col[c]-=a[x];
    }
    return false;
}

int main(){
    scanf("%d",&n);
    int m=n*n,all=0,sum=0;
    for(int i=0;i<m;all+=a[i++]) scanf("%d",a+i);
    if(n==1){
        sum=a[0];
    }else if(n==2){
        sort(a,a+m);
        do{
            sum=a[0]+a[1];
            if(sum==a[3]+a[4]
            && sum==a[0]+a[3]
            && sum==a[1]+a[4]
            && sum==a[0]+a[4]
            && sum==a[1]+a[3]) break;
        }while(next_permutation(a,a+m));
    }else if(n==3){
        sort(a,a+m);
        do{
            sum=a[0]+a[1]+a[2];
            if(sum==a[3]+a[4]+a[5]
            && sum==a[6]+a[7]+a[8]
            && sum==a[0]+a[3]+a[6]
            && sum==a[1]+a[4]+a[7]
            && sum==a[2]+a[5]+a[8]
            && sum==a[0]+a[4]+a[8]
            && sum==a[2]+a[4]+a[6]) break;
        }while(next_permutation(a,a+m));
    }else if(n==4){
        int mask=(1<<m)-1;
        for(int s=255;s<=mask;s=next_samebit(s)){
            for(int i=sum=0;i<m;i++) if(s&1<<i) sum+=a[i];
            if((all*2+sum)%10) continue;
            at=(all*2+sum)/10;
            if(dfs(0,0,mask)) break;
        }
    }
    printf("%d\n",sum);
    for(int i=0;i<n;i++){
        for(int j=0;j<n-1;j++) printf("%d ",a[i*n+j]);
        printf("%d\n",a[i*n+n-1]);
    }
}

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <cctype>
#include <numeric>
#include <queue>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <iterator>
#define FOR(i,s,e) for(int i=(s);i<(int)(e);i++)
#define FOE(i,s,e) for(int i=(s);i<=(int)(e);i++)
#define REP(i,n)   FOR(i,0,n)
#define ALL(x) (x).begin(), (x).end()
#define CLR(s) memset(s,0,sizeof(s))
#define PB push_back
#define ITER(v)      __typeof((v).begin())
#define FOREACH(i,v) for(ITER(v) i=(v).begin();i!=(v).end();i++)
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
typedef map<int,int> mii;
typedef vector<int> vi;
#define x first
#define y second

const int N = 20;
int n;
LL a[N];
LL f(int i, int j) { return a[i*n + j]; }

pair<LL,LL> p[N];
int m = 0;
LL s = 0;

LL c[N];
LL r[N];
LL d;
LL d1;

map<LL,LL> S;
LL arr[N][N];
bool sol = false;

void out() {
        if (sol) return;
        sol = true;
        cout << s << endl;
        FOR(i, 0, n) FOR(j, 0, n) {
                cout << arr[i][j] << (j==n-1?"\n":" ");
        }
}

void go(int, int);

void gao(int i, int j, LL x) {
        arr[i][j] = x;
        c[i] += x; r[j] += x;
        if (i == j) d += x;
        if (i+j==n-1) d1 += x;
}

void rec(int i, int j, int k) {
        if (sol || p[k].y <= 0) return;
        LL &x = p[k].x;
        p[k].y--;
        gao(i, j, x);
                if (i == 0) s = c[0];
                go(i, j+1);
        gao(i, j, -x);
        p[k].y++;
}

void go(int i, int j) {
        if (i == n-2 && j == n-2) {
                if (d1 != r[0]) return;
        }
//      if (i == n-1 && j == 0) {
//              LL wt = s - d;
//              if (S.find(wt) == S.end() || p[S[wt]].y <= 0) return;
//      }

        if (sol) return;
        if (i == n) {
//              if (s == d && s == d1) {
                        out();
//              }
                return;
        }

        if (j == n) return go(i+1, 0);

        if (j == n-1) {
                if (i == 0) {
                        FOR(k, 0, m)
                                if (sol) return;
                                else rec( i, j, k );
                } else {
                        LL wt = s - c[i];
                        if (S.find(wt) == S.end() || p[S[wt]].y <= 0) return;
                        int k = S[wt];
                        rec( i, j, k );
                }
        } else if (i == n-1) {
                LL wt = s - r[j];
                if (S.find(wt) == S.end() || p[S[wt]].y <= 0) return;
                int k = S[wt];
                rec( i, j, k );
        } else {
                FOR(k, 0, m)
                        rec(i, j, k);
        }
}

int main() {
        cin >> n;
        FOR(i, 0, n*n) cin >> a[i];
        sort(a, a+n*n);

        FOR(i, 0, n*n) {
                int b = a[i];
                int r = 0;
                while (i < n*n && a[i] == b) ++i, ++r;
                i--;
                S[b] = m;
                p[m++] = pii(b, r);
        }
//      FOR(i, 0, m) printf("%d %d\n", p[i].x, p[i].y);

        go(0, 0);
}

#include<iostream>
#include<algorithm>
#include <cstdlib>

using namespace std;

int n, totalSum, size;
int res[20], a[20];
int mark[20];
int sum;
int n2;

inline bool check()
{
	sum = 0;
	for (int j = 0; j < n; j++)
		sum += res[n2 - n + j];
	if (sum != totalSum)
		return false;

	sum = 0;
	for (int i = 0; i < n2; i += n + 1)
		sum += res[i];
	if (sum != totalSum)
		return false;

	for (int i = 0; i < n2; i++)
	{
		cout << res[i] << " ";
		if (i % n == n - 1)
			cout << endl;
	}
	exit(0);
}

bool backTrack(int x)
{
	if(n == 4 && x == n - 1 && res[1] == res[2])
		return false;
	if(x == n && res[n - 1] > res[0])
		return false;
	if(x > 0 && x % n == 0)
	{
		sum = 0;
		for (int i = 0; i < n; i++)
			sum += res[x - n + i];
		if (sum != totalSum)
			return false;
	}
	else if(x > n2 - n)
	{
		if(x == n2 - n + 1)
		{
			sum = 0;
			for (int i = n - 1; i < n2 - 1; i += n - 1)
				sum += res[i];
			if (sum != totalSum)
				return false;
		}

		sum = 0;
		for (int i = 0; i < n; i++)
			sum += res[x - n2 + n - 1 + i * n];
		if (sum != totalSum)
			return false;
	}
	if (x == n2)
		return check();
	for (int i = 0; i < size; i++)
		if (mark[i] != 0)
		{
			mark[i]--;
			res[x] = a[i];
			backTrack(x + 1);
			mark[i]++;
		}
}

int main()
{
	ios::sync_with_stdio(false);
	cin >> n;
	n2 = n * n;
	for (int i = 0; i < n * n; i++)
	{
		int x;
		cin >> x;
		totalSum += x;
		bool exist = false;
		for (int j = 0; j < size; j++)
			if (a[j] == x)
			{
				mark[j]++;
				exist = true;
				break;
			}
		if (!exist)
		{
			mark[size] = 1;
			a[size++] = x;
		}
	}
	totalSum /= n;
	cout << totalSum << endl;
	if(n == 1)
		cout << a[0] << endl;
	else
		backTrack(0);
	return 0;
}

#include <algorithm>
#include <iostream>
#include <cassert>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <map>
#include <set>

#define SIZE(A) ((int)A.size())
#define LENGTH(A) ((int)A.length())
#define MP(A,B) make_pair(A,B)
#define PB(A) push_back(A)

typedef long long ll;
typedef double ld;

using namespace std;

int n, m, Magic, md, pd;
int a[99], b[99], cnt[99], row[99], col[99];
int t[99][99];

int q[99];

void giveans()
{
 	printf("%d\n", Magic);
 	for (int i = 0; i < n; i++)
 		for (int j = 0; j < n; j++)
 			printf("%d%c", t[i][j], " \n"[j+1==n]);
	exit(0);
}

void dfs(int x, int y)
{
	if (x == n)
	{
//		cerr << "here\n";
//		for (int i = 0; i < n; i++)
//			for (int j = 0; j < n; j++)
//				printf("%d%c", t[i][j], " \n"[j+1==n]);
//		puts("");
	 	if (row[n-1] == Magic && md == Magic && pd == Magic) giveans();
	}
	else
	if (x == n-1)
	{
		int tmp = lower_bound(b, b+m, Magic-col[y])-b;
		if (b[tmp]!=Magic-col[y]) return;
		if (!cnt[tmp]) return;

		cnt[tmp]--;
		t[x][y] = b[tmp];

		if (y == n-1) md += b[tmp];
		if (!y) pd += b[tmp];
		row[x] += b[tmp];

//-------------------------------
		if (y == n-1) dfs(x+1, 0);
		else dfs(x, y+1);
//-------------------------------

		if (!y) pd -= b[tmp];
		if (y == n-1) md -= b[tmp];
		row[x] -= b[tmp];

		cnt[tmp]++;
	}
	else
	if (y == n-1)
	{
		int tmp = lower_bound(b, b+m, Magic-row[x])-b;
		if (b[tmp]!=Magic-row[x]) return;
		if (!cnt[tmp]) return;

		cnt[tmp]--;
		t[x][y] = b[tmp];

		if (!x) pd += b[tmp];
		col[y] += b[tmp];

//-------------------------------
		dfs(x+1, 0);
//-------------------------------

		col[y] -= b[tmp];
		if (!x) pd -= b[tmp];

		cnt[tmp]++;
	}
	else
	{
		for (int i = 0, j, k; i < m; i++)
		{
			k = q[i];

			if (cnt[k])
			{
			 	cnt[k]--;

				t[x][y] = b[k];

				row[x] += b[k];
				col[y] += b[k];
				if (x == y) md += b[k];
				if (x + y == n-1) pd += b[k];

				if (!cnt[k])
				{
					for (j = i; j+1<m && cnt[q[j+1]]; j++)
						swap(q[j], q[j+1]);
				}

				dfs(x, y+1);

				if (x + y == n-1) pd -= b[k];
				if (x == y) md -= b[k];
				col[y] -= b[k];
				row[x] -= b[k];

				if (!cnt[k])
				{
					for (; j!=i; j--)
						swap(q[j], q[j-1]);
				}

			 	cnt[k]++;
			}
			else
				break;
		}
	}
}

int main()
{
	#ifndef ONLINE_JUDGE
		freopen("input", "r", stdin);
		freopen("output", "w", stdout);
	#endif

	scanf("%d", &n);
	for (int i = 0; i < n*n; i++)
		scanf("%d", a+i),
		Magic += a[i],
		b[i] = a[i];

	if (n == 1)
	{
	 	printf("%d\n%d\n", a[0], a[0]);
	 	return 0;
	}

	sort(b, b + n*n);
	m = unique(b, b + n*n)-b;
	b[m] = 1000000000;

	for (int i = 0; i < n*n; i++)
		cnt[lower_bound(b, b + m, a[i])-b]++;
	for (int i = 0; i < m; i++)
		q[i] = i;

	Magic /= n;

/*	cnt[0]--;
	t[0][0] = b[0];
	row[0] += b[0];
	col[0] += b[0];
	md += b[0];
*/
	dfs(0, 0);

	return 0;
}

#include <bits/stdc++.h>
#define fr(a, b, c) for(int (a) = (b), __ = c; (a) < __; ++(a))
#define dbg(x) cerr << ">>>> " << x << endl;
#define _ << " , " <<

using namespace std;

const int ms = 4 * 4;

int v[ms], aux[ms];
int a[ms], vv[ms];
int ans[4][4];

int main() {
    int n;
    cin >> n;
    int sum = 0;
    for (int i = 0; i < n * n; i++) {
        scanf("%d", v + i);
        sum += v[i];
    }
    if (n == 1) {
        printf("%d\n%\n", v[0], v[0]);
        return 0;
    }
    sort(v, v + n);
    assert(sum % n == 0);
    sum /= n;
    for (int i = 0; i < (n-1) * (n-1); i++) a[i] = 1;
    sort(a, a + n * n);
    do {
        int cur = 0;
        for (int i = 0; i < n * n; i++) {
            if (a[i]) {
                vv[cur++] = v[i];
            }
        }
        int ok;
        do {
            ok = 1;
            for (int i = 0; i < cur; i++) {
                ans[i / (n-1)][i % (n-1)] = vv[i];
            }
            for (int j = 0; j + 1 < n; j++) {
                int s = 0;
                for (int i = 0; i + 1 < n; i++) {
                    s += ans[i][j];
                }
                ans[n-1][j] = sum - s;
            }
            for (int j = 0; j < n; j++) {
                int s = 0;
                for (int i = 0; i + 1 < n; i++) {
                    s += ans[j][i];
                }
                ans[j][n-1] = sum - s;
            }
            int s1 = 0, s2 = 0, s3 = 0;
            for (int i = 0; i < n; i++) {
                s1 += ans[i][i];
                s2 += ans[i][n - i - 1];
                s3 += ans[n - 1][i];
            }
            if (s1 == sum and s2 == sum and s3 == sum) {
                for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) {
                    aux[i * n + j] = ans[i][j];
                }
                sort(aux, aux + n * n);
                for (int i = 0; i < n * n; i++) if (v[i] != aux[i]) {
                    ok = 0;
                    break;
                }
            } else {
                ok = 0;
            }
            if (ok) {
                break;
            }
        } while (next_permutation(vv, vv + (n-1)*(n-1)));
        if (ok) {
            break;
        }
    } while(next_permutation(a, a + n * n));
    cout << sum << endl;
    for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) printf("%d%c", ans[i][j], " \n"[j + 1 == n]);
    return 0;
}

/**					Be name Khoda					**/
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <algorithm>
#include <bitset>
#include <limits>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <memory.h>
#include <ctime>
#include <cassert>
using namespace std;

#define ll long long
#define un unsigned
#define IT iterator
#define VAL(x) #x << " = " << x << "   "
#define SQR(a) ((a) * (a))
#define SZ(x) ((int) x.size())
#define ALL(x) x.begin(), x.end()
#define CLR(x, a) memset(x, a, sizeof x)
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define SAL cout << "Salam!\n"
#define MAXN 1000
//#define cout fout
//#define cin fin

int n, avg;

vector<vector<int> > ans;

bool check()
{
	int sum;
	for (int i = 0; i < n; i ++)
	{
		sum = 0;
		for (int j = 0; j < n; j ++)
			sum += ans[j][i];
		if (sum != avg)
			return false;
	}
	sum = 0;
	for (int i = 0; i < n; i ++)
		sum += ans[i][i];
	if (sum != avg)
		return false;
	sum = 0;
	for (int i = 0; i < n; i ++)
		sum += ans[i][(n - 1) - i];
	if (sum != avg)
		return false;
	else
		return true;
}

bool b = false;

void solve(int x)
{
	if (b)
		return ;
	if (x == n)
	{
		if (check())
		{
			b = true;
			for (int i = 0; i < n; i ++, cout << endl)
				for (int j = 0; j < n; j ++)
					cout << ans[i][j] << " ";
		}
		return ;
	}
	for (int i = 0; i < n - 1; i ++)
		for (int j = i + 1; j < n; j ++)
		{
			swap(ans[x][i], ans[x][j]);
			solve(x + 1);
			swap(ans[x][i], ans[x][j]);
		}
}

int main ()
{
	cin >> n;
	int a[10], sum = 0;
	for (int i = 0; i < SQR(n); i ++)
	{
		cin >> a[i];
		sum += a[i];
	}
	avg = sum / n;
	//cout << VAL(sum) << VAL(avg) << endl;
	cout << avg << endl;
	bool mark[10];
	CLR(mark, false);
	if (n == 1)
	{
		cout << a[0] << endl;
		return 0;
	}
	if (n == 2)
	{
		for (int i = 0; i < SQR(n) - 2; i ++)
			for (int j = 0; j < SQR(n) - 1; j ++)
				if (a[i] + a[j] == avg && !mark[i] && !mark[j])
				{
					vector<int> cur;
					cur.pb(a[i]), cur.pb(a[j]);
					ans.pb(cur);
					mark[i] = mark[j] = true;
				}
	}
	for (int i = 0; i < SQR(n) - 2; i ++)
		for (int j = 0; j < SQR(n) - 1; j ++)
			for (int k = 0; k < SQR(n); k ++)
				if (a[i] + a[j] + a[k] == avg && !mark[i] && !mark[j] && !mark[k])
				{
					vector<int> cur;
					cur.pb(a[i]), cur.pb(a[j]), cur.pb(a[k]);
					ans.pb(cur);
					mark[i] = mark[j] = mark[k] = true;
				}
	solve(0);
	if (!b)
		swap(ans[0], ans[1]);
	if (!b && n == 3)
	{
		swap(ans[0], ans[1]);
		swap(ans[1], ans[2]);
	}
	return 0;
}

#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <cstring>

using namespace std;
typedef long long LL;
#define REP(i, n) for(int i=0; i<n; i++)
#define FOR(i, x, y) for(int i=x; i<=y; i++)

int n;
int nums[32];
int a[8][8];
bool u[32];
int s;

bool check()
{
    REP(i, n)
    {
        int cur = 0;
        REP(j, n)
            cur += a[i][j];
        if (s != cur) return false;
    }

    REP(i, n)
    {
        int cur = 0;
        REP(j, n)
            cur += a[j][i];
        if (s != cur) return false;
    }

    int cur = 0;
    REP(i, n)
        cur += a[i][n-1-i];
    if (cur != s)
        return false;
    return true;
}

bool go(int x, int y)
{
    REP(i, n*n)
        if (!u[i])
        {
            u[i] = true;
            a[x][y] = nums[i];

            if (x == n - 1 && y == n - 1)
            {
                if (check())
                {
                    cout << s << endl;
                    REP(i, n)
                    {
                        REP(j, n)
                        {
                            if (j)
                                cout << " ";
                            cout << a[i][j];
                        }
                        cout << endl;
                    }
                    return true;
                }
                u[i] = false;
                continue;
            }

            if (y == n-1)
            {
                int ss = 0;
                REP(j, n)
                    ss += a[x][j];
                if (ss != s)
                {
                    u[i] = false;
                    continue;
                }
                if (go(x + 1, 0))
                    return true;
                else
                {
                    u[i] = false;
                    continue;
                }
            }

            if (go(x, y+1))
                return true;
            u[i] = false;
        }
    return false;
}

int main()
{
    cin >> n;
    REP(i, n*n)
    {
        cin >> nums[i];
        s += nums[i];
    }
    s /= n;
    go(0, 0);
}

/*
Author : Hocky Yudhiono
8/5/2020 5:00:37 PM

1. You can sort the query if offline!
2. Don't bring the dp remaining state when dfsing on DP on Tree.
3. Try to reverse (Think from the back) if you stuck.
4. Be careful when submitting Div. 2 D-F, dont waste it on stupid WAs.
5. Try to reduce a problem, think of it when you're making subtasks
   like when problemsetting.
*/

#include <algorithm>
#include <iostream>
#include <numeric>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <iomanip>
#include <cstdio>
#include <limits>
#include <string>
#include <vector>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;

typedef long long LL;
typedef long long ll;
typedef long double LD;
typedef vector<int> vi;
typedef pair<LL,LL> PLL;
typedef pair<LL,int> PLI;
typedef pair<int,int> PII;
typedef pair<int,int> pii;
typedef vector<vector<LL>> VVL;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define popf pop_front
#define pf push_front
#define popb pop_back
#define mp make_pair
#define pb push_back
#define remove erase
#define fi first
#define se second

// If the time limit is strict, try not to use long double

// Remember to undefine if the problem is interactive
#define endl '\n'
#define DEBUG(X) cout << ">>> DEBUG(" << __LINE__ << ") " << #X << " = " << (X) << endl

const double EPS = 1e-9;
const int INFMEM = 63;
const int INF = 1061109567;
const LL LINF = 4557430888798830399LL;
const double DINF = numeric_limits<double>::infinity();
const LL MOD = 1000000007;
const int dx[8] = {0,0,1,-1,1,-1,1,-1};
const int dy[8] = {1,-1,0,0,1,-1,-1,1};
// Do dir^1 to get reverse direction
const char dch[4] = {'R','L','D','U'};
// const string ds[8] = {"E","W","S","N","SE","NW","SW","NE"};
const double PI = 3.141592653589793;

inline void open(string a){
    freopen((a+".in").c_str(),"r",stdin);
    freopen((a+".out").c_str(),"w",stdout);
}

inline void fasterios(){
    // Do not use if interactive
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    // cout << fixed << setprecision(10);
}

LL n,m,allsum;
LL ans[4][4];
int cnt[20];
map<LL, int> comp;
vector<LL>revComp;
void solve(LL x, LL y){
    // cout << allsum << endl;
    // cout << x << " " << y << endl;
    if(x == n-1){
        // solve last here
        vector <int> returned;
        bool can = 1;
        rep(j,0,n){
            int cur = 0;
            rep(i,0,n-1) cur += ans[i][j];
            cur = allsum-cur;
            if(!comp.count(cur)){
                can = 0;
                break;
            }
            int curidx = comp[cur];
            if(cnt[curidx] == 0){
                can = 0;
                break;
            }
            cnt[curidx]--;
            returned.pb(curidx);
            ans[n-1][j] = cur;
        }
        if(!can){
            trav(cur, returned) cnt[cur]++;
            return;
        }
        cout << allsum << endl;
        rep(i,0,n) rep(j,0,n) cout << ans[i][j] << " \n"[j == n-1];
        exit(0);
    }else if(y == n-1){
        int returned;
        int cur = 0;
        rep(j,0,n-1) cur += ans[x][j];
        cur = allsum-cur;
        if(!comp.count(cur)) return;
        int curidx = comp[cur];
        if(cnt[curidx] == 0) return;
        cnt[curidx]--;
        returned = curidx;
        ans[x][n-1] = cur;
        solve(x+1, 0);
        cnt[returned]++;
        return;
    }

    rep(i,0,m){
        if(cnt[i]){
            ans[x][y] = revComp[i];
            cnt[i]--;
            solve(x, y+1);
            cnt[i]++;
        }
    }

    return;
}

int main(){
    cin >> n;

    vector <LL> isi(n*n);
    revComp.resize(n*n);
    rep(i,0,n*n){
        cin >> isi[i];
        allsum += isi[i];
        revComp[i] = isi[i];
    }
    sort(revComp.begin(),revComp.end());
    revComp.erase(unique(revComp.begin(),revComp.end()),revComp.end());
    m = sz(revComp);
    rep(i,0,m) comp[revComp[i]] = i;
    trav(cur, isi) cnt[comp[cur]]++;
    allsum /= n;
    solve(0,0);
    return 0;
}

//sob, 2 cze 2018, 19:06:28 CEST
//Konrad Paluszek, University of Warsaw (former XIV LO Staszic)
# include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7, MN = 1e6 + 44;
const bool local =
#ifdef LOCAL
true;
#else
false;
#endif
#define f first
#define s second
#define FOR(i, b, e) for (int i = (b); i <= (int)(e); ++i)
#define FORD(i, b, e) for (int i = (b); i >= (int)(e); --i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define bvt(s, f) int s(int x){return f(x);}int s(UI x){return f(x);}int s(LL x){return f##ll(x);}int s(ULL x){return f##ll(x);}
#define FORBITS(i,m)  for(decltype(m)lP6N4Slr=(m),i=lP6N4Slr?ctz(lP6N4Slr):0;lP6N4Slr;lP6N4Slr^=((decltype(m))1<<i),i=ctz(lP6N4Slr))
#define FORDBITS(i,m) for(decltype(m)_6Im=(m),i=_6Im?8*sizeof(m)-clz(_6Im)-1:0;_6Im;_6Im^=((decltype(m))1<<i),i=8*sizeof(m)-clz(_6Im)-1)
#define SUBS(i, mask) for(decltype(mask)i = (mask),NFR9xNQCJZTrYQ=1;NFR9xNQCJZTrYQ;NFR9xNQCJZTrYQ=i,i=(i-1)&(mask))
#define PB push_back
#define MP make_pair
#define EB emplace_back
#define ymd(t) using P##t=pair<t,t>;using V##t=vector<t>;using S##t=set<t>;using T##t=pair<P##t,t>;using Q##t=pair<P##t,P##t>;
using LL=long long;using LD=long double;using VI=vector<int>;using PII=pair<int,int>;using SI=set<int>;using MII=map<int,int>;ymd(LD)
using TII=pair<PII,int>;using QII=pair<PII,PII>;using ULL=unsigned long long;ymd(ULL)ymd(VI)ymd(LL)ymd(PII)ymd(PLL)ymd(PLD)ymd(PULL)ymd(SI)
ymd(TII)ymd(TLL)ymd(TLD)ymd(QII)ymd(QLL)ymd(QLD)ymd(QULL)ymd(TULL)using UI=unsigned int;ymd(UI)ymd(PUI)ymd(VUI)ymd(TUI)ymd(QUI)ymd(VVI)
bvt(popc,__builtin_popcount)bvt(ctz,__builtin_ctz)bvt(clz,__builtin_clz)bvt(bit_parity,__builtin_parity)ymd(SLL)ymd(VLL)ymd(VULL)ymd(SULL)
#define siz(r) ((int)r.size())
#define ALL(r) r.begin(), r.end()
#define sim template <class T
#define ros return o
sim,class N>bool mini(T&o,N h){if(o>h)ros=h,1;return 0;}sim,class N>bool maxi(T&o,N h){if(o<h)ros=h,1;return 0;}
#define oor >ostream &operator<<(ostream &o,
#define urs(r) typename pta<typename remove_reference<decltype(r)>::type>::t
#define bnt ;}sim> struct
sim,class N oor pair<T,N>u){ros<<"<"<<u.f<<", "<<u.s<<">" bnt pta{using t=T;};sim,int N>struct pta<T[N]>{using t=T*;};
sim,typename=typename enable_if<!is_same<T,string>::value,urs(T().end())>::type oor
T u){int c=0;o<<"{";for(auto e:u)o<<", "+2*!c++<<e;ros<<"}";}
int mpow(int b, int e) {int o = 1; while (e) {if (e & 1)o = o * 1ll * b % MOD;b = b * 1ll * b % MOD;e /= 2;}ros;}
void dor(){cerr<<dec<<"\033[0m"<<endl;}sim,class...N>void dor(T x,N...y){cerr<<boolalpha<<x<<"; ";dor(y...);} const bool deb =
#define acv(N,O) sim> typename enable_if<is_arithmetic<T>::value, N>::type operator O(N o, T x) {ros O N(x);} \
  sim> typename enable_if<is_arithmetic<T>::value, N>::type operator O(T o, N x) {return N(o) O x;} \
  sim> typename enable_if<is_arithmetic<T>::value || is_same<T,N>::value, N&>::type operator O##=(N &o, T x) {ros = o O N(x);}
#ifdef DEB
true;
#define debug(c...)cerr<<"\033[1;"<<30+__LINE__%8<<"m["<<__FUNCTION__<<"#"<<__LINE__<<"] \033["<<30+(__LINE__/8+__LINE__*3)%8<<"m",::dor(c)
#define rcu m);}g(+) g(-) g(*) g(/) g(%) g(<<) g(>>) g(^) g(|) g(&) g(>=) g(<=) g(>) g(<) g(!=) g(==)
#define zub(h,t,n...) class N> auto operator h(n u)->nep<urs(t)> {return ngy(t,
sim>string bte(T u){stringstream o;o<<u;ros.str()bnt nep;sim>T yyo(nep<T>);sim>nep<T>ngy(T v,string c,int m=0);sim>T yyo(T o){ros bnt
nep{T v;string c;int m; nep<T>mask(){return ngy(v,c,1);}
#define g(o) template<zub(o, v o u, N) "(" + bte(*this) + ") " #o " (" + bte(u) + ")", m);}
template<zub([],v[yyo(u)],N)c+"["+bte(u)+"]",rcu};sim>nep<T>ngy(T v,string c,int m){return nep<T>{v,c,m};}
sim>T yyo(nep<T> o){ros.v;}sim>int uhn(T){return 0;}sim>int uhn(nep<T>o){ros.m;}
#define ccy(c, y) sim> class enable_if<c is_arithmetic<T>::value,string>::type ema(T u){return bte(y);}
ccy(,bitset<8*sizeof(T)>(u))ccy(!,u)sim,class N> string ema(pair<T,N> o) {return "<" + ema(o.f) + ", " + ema(o.s) + ">";}
#define g(o) sim, zub(o, yyo(p) o u.v, T p, nep<N>) "(" + bte(p) + ") " #o " (" + bte(u) + ")", uhn(p) + u.m);}
sim>string bte(nep<T>o){ros.c+" = "+(o.m?ema(o.v):bte(o.v));}sim oor nep<T>m){ros<<bte(rcu sim>nep<T>ngy(nep<T>o,string c="",int m=0){ros;}
#undef g
#define imie(a...) ngy(a, #a)
#define imask(a...) ngy(a, #a, 1)
#define range(u, g, c) ngy(list<urs(u[0])>(&u[yyo(g)], &u[yyo(c)] + 1), #u "[" + bte(g) + "..." + bte(c) + "]")
#define arr(a, i) imie(a)[imie(i)]
#define arr2(a, i, j) imie(a)[imie(i)][imie(j)]
#define arr3(a, i, j, k) imie(a)[imie(i)][imie(j)][imie(k)]
#define arr4(a, i, j, k, q) imie(a)[imie(i)][imie(j)][imie(k)][imie(q)]
vector <stringstream> lif;struct prux {prux() {lif.EB();}~prux() {lif.pop_back();}};
#define mark_stack(c...) erf(__LINE__,c)
#define erf(l,c...) prux _4IJ1MYUxZy2p_Q##l;lif.back()<<__FUNCTION__<<"#"<<l<<": ";siw(c)
void siw(){};sim, class...N> void siw(T x, N...y) {lif.back() << x << "; ";siw(y...);}
#define print_stack() cerr << "STACK_TRACE_FROM:" << __FUNCTION__ << "#" << __LINE__<<endl,nfl()
#define ckx(o) template<zub(o, o yyo(u), nep<N>) #o "(" + bte(u) + ")", u.m);}
sim> struct tqaz{T a;int u,d,l,r;};sim> tqaz<T> zaqt(T a,int u,int d,int l,int r){return tqaz<T>{a,u,d,l,r};}
#define matr(a,u,d,l,r) ngy(zaqt(a,yyo(u),yyo(d),yyo(l),yyo(r)), #a"["+bte(u)+"..."+bte(d)+"]["+bte(l)+"..."+bte(r)+"]")
#define avc(f) sim>string f(tqaz<T> x){vector<vector<string>> t;FOR(i,x.u,x.d){t.EB();FOR(j,x.l,x.r)t.back().PB(f(x.a[i][j]));}\
stringstream o;VI M;FOR(j,x.l,x.r){int m=0;FOR(i,x.u,x.d)maxi(m, siz(t[i][j]));M.PB(m);}FOR(i,x.u,x.d){o<<"\n\t["<<i<<"] ";\
FOR(j,x.l,x.r){REP(_, M[j-x.l]-siz(t[i][j]))o<<" ";o<<t[i][j]<<((j+1)%5?", ":"; ");}}ros.str() + "\n";}
void nfl() {for (auto &s : lif)cerr << "  " << s.str() << endl;} ckx(+) ckx(-) ckx(!) ckx(~) avc(bte) avc(ema)
struct zet_p {int o;LD v;zet_p(LL x):o(x%MOD),v(x){} zet_p(LL x, LD y):o(x%MOD),v(y){} int get(){ros>0?o:o+MOD;}
  zet_p operator+(zet_p a) {return zet_p(o+a.o,v+a.v);}
  zet_p operator-(zet_p a) {return zet_p(o-a.o,v-a.v);}
  zet_p operator*(zet_p a) {return zet_p(o*1ll*a.o,v*a.v);}
  zet_p operator/(zet_p a) {assert(a.o != 0); return zet_p(o*1ll*mpow(a.o, MOD-2),v/a.v);}
  bool operator==(zet_p a) {return (o-a.o)%MOD == 0;}};
ostream&operator<<(ostream &o, zet_p u) {ros<<"["<<u.o<<" @ "<<u.v<<"]";}using frac_mod = zet_p;
#else
false;
struct zet_p {int o;int get(){ros>0?o:o+MOD;}zet_p(LL x):o(x%MOD){}zet_p(int x):o(x%MOD){}
  zet_p operator+(zet_p a) {return zet_p(o+a.o);}
  zet_p operator-(zet_p a) {return zet_p(o-a.o);}
  zet_p operator*(zet_p a) {return zet_p(o*1ll*a.o);}
  zet_p operator/(zet_p a) {return zet_p(o*1ll*mpow(a.o, MOD - 2));}
  bool operator==(zet_p a) {return (o-a.o)%MOD == 0;}};
struct frac_mod {int o,m;int get(){return (o*1ll*mpow(m,MOD-2)%MOD+MOD)%MOD;}frac_mod(int x):o(x%MOD),m(1){}frac_mod(LL x):o(x%MOD),m(1){}
  frac_mod(LL a,LL b):o(a),m(b){}
  frac_mod operator+(frac_mod a){return frac_mod(o*1ll*a.m+m*1ll*a.o,m*a.m);}
  frac_mod operator-(frac_mod a){return frac_mod(o*1ll*a.m-m*1ll*a.o,m*a.m);}
  frac_mod operator*(frac_mod a){return frac_mod(o*1ll*a.o,m*1ll*a.m);}
  frac_mod operator/(frac_mod a){return frac_mod(o*1ll*a.m,m*1ll*a.o);}
  bool operator==(frac_mod a) {return (o * 1ll * a.m - m * 1ll * a.o) % MOD == 0;}};
acv(frac_mod, +) acv(frac_mod, -) acv(frac_mod, *) acv(frac_mod, /)
#define debug(...)
#define mark_stack(...)
#define print_stack(...)
#endif
acv(zet_p, +) acv(zet_p, -) acv(zet_p, *) acv(zet_p, /)
#define nok(o,c,r,l...) auto operator o(c p, r y)-> decltype(MP(l)) {return MP(l);}
#define pcg(o) sim, class S, class N, class H> nok(o, pair<S u T>, pair<H u N>, p.first o y.first, p.second o y.second)\
sim,class S,class N,class=typename enable_if<!is_base_of<ios_base,typename remove_reference<T>::type>::value>::type>nok\
(o,T,pair<S u N>, p o y.first, p o y.second) sim, class S, class N> nok(o, pair<S u N>, T, p.first o y, p.second o y)
#define clp(r) pcg(r) sim, class S, class N, class H> \
	pair <T,S> & operator r##=(pair <T,S> &p, pair<N,H> y) {p.first r##= y.first; p.second r##= y.second; return p;}\
	sim, class S, class N> pair <T,S> &operator r##=(pair<T,S> &p, N y) {p.first r##= y; p.second r##= y; return p;}
#define u ,
#define syd(o) sim, class N> auto operator o(pair<T,N> e) -> decltype(MP(o e.f, o e.s)) {return MP(o e.f, o e.s);}
clp(+) clp(-) clp(*) clp(/) clp(%) clp(^) clp(|) clp(>>) clp(<<) clp(&) pcg(&&) pcg(||) syd(-) syd(+) syd(~) syd(!)
#undef u
sim, class N> using gyv = T;
namespace std {sim, class N> struct hash<pair<T,N> > {size_t operator()(const pair<T,N>&u) const {
      return hash<T>()(u.f) * 440624741ULL ^ hash<N>()(u.s);}};
  sim> struct hash<gyv<T, decltype(((T*)0)->end())>> {size_t operator()(const T& u) const {size_t o = 505347433;
for (auto e : u)o = hash<urs(e)>()(e) ^ o * 277953755ULL;ros;}};}
MII cou;
int sum = 0;
int n;
int board[4][4];
void go(int num, int i, int j);
void backtrack(int i, int j) {
  if (i == 3 && j == 0) {
    REP(i, n)
      REP(j, n)
        printf("%2d%c", board[i][j], j == n - 1 ? '\n' : ' ');
    exit(0);
  }
  if (j == 3) {
    int s = sum;
    REP(x, 3)
      s -= board[i][x];
    go(s, i, j);
    return;
  }
  if (i == 3) {
    int s = sum;
    REP(x, 3)
      s -= board[x][j];
    go(s, i, j);
    return;
  }
  if (i == 2 && j == 1) {
    go(board[0][0] + board[1][0] + board[2][0] - board[1][2] - board[0][3], i, j);
    return;
  }
  if (i == 2 && j == 2) {
    int o = board[0][3] + board[1][3] + sum - board[2][0] - board[2][1] - board[0][0] - board[1][1];
    if (o % 2 == 0)
    go(o / 2, i, j);
    return;
  }
  if (i == 1 && j == 0) {
    for (auto x : cou) {
      if (x.f > board[0][1])
        break;
      go(x.f, i, j);
    }
    return;
  }
  for (auto x : cou)
    go(x.f, i, j);
}
void go(int num, int i, int j) {
  auto it = cou.find(num);
  if (it == cou.end() || it -> s == 0) return;
  int under = -1;
  if (i == 2) {
    under = sum - board[0][j] - board[1][j] - num;
    if (under == num) {
      if (it -> s < 2) return;
    }
    else {
      auto it = cou.find(under);
      if (it == cou.end() || it -> s == 0) return;
      board[3][j] = under;
      it -> s--;
    }
  }
  it -> s--;
  board[i][j] = num;
  if (j == 3)
    backtrack(i + 1, 0);
  else
    backtrack(i, j + 1);
  it -> s++;
  if (i == 2) {
    cou[under]++;
  }
}
int main() {
  scanf("%d", &n);
  VI nums;
  REP(_, n * n) {
    int x;
    scanf("%d", &x);
    cou[x]++;
    sum += x;
    nums.PB(x);
  }
  assert(sum % n == 0);
  sum /= n;
  printf("%d\n", sum);
  if (n <= 3) {
    sort(ALL(nums));
    do {
      bool good = true;
      REP(i, n) {
        int s1 = 0, s2 = 0;
        REP(j, n) {
          s1 += nums[i * n + j];
          s2 += nums[j * n + i];
        }
        if (s1 != sum || s2 != sum)
          good = false;
      }
      int s1 = 0, s2 = 0;
      REP(i, n) {
        s1 += nums[i * n + i];
        s2 += nums[i * n + n - i - 1];
      }
      if (s1 != sum || s2 != sum)
        good = false;
      if (good) {
        REP(i, n * n)
          printf("%d%c", nums[i], i % n == n - 1 ? '\n' : ' ');
        return 0;
      }
    }
    while (next_permutation(ALL(nums)));
  }
  backtrack(0, 0);
}

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <bitset>
#include <complex>

using namespace std;

typedef unsigned uint;
typedef long long Int;
typedef vector<int> vint;
typedef pair<int,int> pint;
#define mp make_pair

template<class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; }
template<class T> void chmin(T &t, T f) { if (t > f) t = f; }
template<class T> void chmax(T &t, T f) { if (t < f) t = f; }
int in() { int x; scanf("%d", &x); return x; }

int N, NN;
int A[20];
int S;
int B[20][20];

int f[20] = {};
int get(int a) {
	int i = lower_bound(A, A + NN, a) - A;
	for (; i < NN && f[i]; ++i);
	return (i < NN && A[i] == a && !f[i]) ? i : -1;
}

void solve4() {
	int i00, i01, i02, i03;
	int i10, i11, i12, i13;
	int i20, i21, i22, i23;
	int i30, i31, i32, i33;
	memset(f, 0, sizeof(f));
	for (i00 = 0; i00 < NN; ++i00)
	for (i03 = i00 + 1; i03 < NN; ++i03)
	for (i30 = i03 + 1; i30 < NN; ++i30)
	{
		++f[i00]; ++f[i03]; ++f[i30];
		for (i01 = 0; i01 < NN; ++i01) if (!f[i01]) {
			++f[i01];
			i02 = get(S - A[i00] - A[i01] - A[i03]);
			if (~i02) {
				++f[i02];
				for (i10 = 0; i10 < N; ++i10) if (!f[i10]) {
					++f[i10];
					i20 = get(S - A[i00] - A[i10] - A[i30]);
					if (~i20) {
						++f[i20];
						--f[i20];
						for (i12 = 0; i12 < NN; ++i12) if (!f[i12]) {
							++f[i12];
							i21 = get(S - A[i03] - A[i12] - A[i30]);
							if (~i21) {
								++f[i21];
								for (i11 = 0; i11 < NN; ++i11) if (!f[i11]) {
									++f[i11];
									i13 = get(S - A[i10] - A[i11] - A[i12]);
									if (~i13) {
										++f[i13];
										i31 = get(S - A[i01] - A[i11] - A[i21]);
										if (~i31) {
											++f[i31];
											for (i22 = 0; i22 < NN; ++i22) if (!f[i22]) {
												++f[i22];
												i23 = get(S - A[i20] - A[i21] - A[i22]);
												if (~i23) {
													++f[i23];
													i32 = get(S - A[i02] - A[i12] - A[i22]);
													if (~i32) {
														++f[i32];
														i33 = get(S - A[i00] - A[i11] - A[i22]);
														if (~i33) {
															if (S == A[i30] + A[i31] + A[i32] + A[i33])
															if (S == A[i03] + A[i13] + A[i23] + A[i33])
															{
																B[0][0] = A[i00]; B[0][1] = A[i01]; B[0][2] = A[i02]; B[0][3] = A[i03];
																B[1][0] = A[i10]; B[1][1] = A[i11]; B[1][2] = A[i12]; B[1][3] = A[i13];
																B[2][0] = A[i20]; B[2][1] = A[i21]; B[2][2] = A[i22]; B[2][3] = A[i23];
																B[3][0] = A[i30]; B[3][1] = A[i31]; B[3][2] = A[i32]; B[3][3] = A[i33];
																return;
															}
														}
														--f[i32];
													}
													--f[i23];
												}
												--f[i22];
											}
											--f[i31];
										}
										--f[i13];
									}
									--f[i11];
								}
								--f[i21];
							}
							--f[i12];
						}
					}
					--f[i10];
				}
				--f[i02];
			}
			--f[i01];
		}
		--f[i00]; --f[i03]; --f[i30];
	}
}

int main() {
	int i;
	int x, y, z;

	for (; ~scanf("%d", &N); ) {
		NN = N * N;
		for (i = 0; i < NN; ++i) {
			A[i] = in();
		}
		sort(A, A + NN);
		S = accumulate(A, A + NN, 0) / N;
		if (N <= 3) {
			do {
				for (x = 0; x < N; ++x) for (y = 0; y < N; ++y) {
					B[x][y] = A[x * N + y];
				}
				for (x = 0; x < N; ++x) {
					int sum = 0;
					for (y = 0; y < N; ++y) sum += B[x][y];
					if (sum != S) goto failed;
				}
				for (y = 0; y < N; ++y) {
					int sum = 0;
					for (x = 0; x < N; ++x) sum += B[x][y];
					if (sum != S) goto failed;
				}
				for (z = 0; z < 2; ++z) {
					int sum = 0;
					for (x = 0; x < N; ++x) sum += B[x][z ? (N - 1 - x) : x];
					if (sum != S) goto failed;
				}
				break;
			failed:;
			} while (next_permutation(A, A + NN));
		} else {
			solve4();
		}
		printf("%d\n", S);
		for (x = 0; x < N; ++x) {
			for (y = 0; y < N; ++y) {
				if (y) printf(" ");
				printf("%d", B[x][y]);
			}
			puts("");
		}
	}

	return 0;
}

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[100],b[5][5];
bool check(int a[],int n)
{
    int i,j,k;
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            b[i][j]=a[i*n+j];
        }
    }
    int sum=0,presum=0;
    for(i=0;i<n;i++) presum+=b[0][i];
    for(i=0;i<n;i++)
    {
        sum=0;
        for(j=0;j<n;j++)
            sum+=b[i][j];
        if(sum!=presum) return false;
    }
    for(i=0;i<n;i++)
    {
        sum=0;
        for(j=0;j<n;j++)
               sum+=b[j][i];
        if(sum!=presum) return false;
    }
    sum=0;
    for(i=0;i<n;i++)
    {
        sum+=b[i][i];
    }
    if(sum!=presum) return false;
    sum=0;
    for(i=0;i<n;i++)
        sum+=b[i][n-i-1];
    if(sum!=presum) return false;
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            printf("%d ",b[i][j]);
        }
        puts("");
    }
    return true;
}
int main()
{
    int n,i;
    scanf("%d",&n);int nn=n*n;
    for(i=0;i<n*n;i++) scanf("%d",&a[i]);
    sort(a,a+nn);
    do{
        if(check(a,n))
        {
            break;
        }
    }while(next_permutation(a,a+nn));
    return 0;
}

#include <iostream>
#include <stack>
#include <algorithm>
using namespace std;

const int D=4;

struct choice
{
    bool used[D*D];

    int step; // 0, 1, 2;
    int a1, a2, a3, a4, a5, a6, /*a7,*/ a8, a9;
    int b1, b2, b3, b4, b5, b6, b7, b8;
};

int n;
int s;
int a[D*D];

int a_temp[D][D];

bool ok_trivial()
{
    int *tmp=a;
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<n; j++)
        {
            a_temp[i][j]=*tmp;
            tmp++;
        }
    }

    int ss;
    for(int i=0; i<n; i++)
    {
        ss=0;
        for(int j=0; j<n; j++) ss+=a_temp[i][j];
        if(ss!=s) return false;
    }

    for(int j=0; j<n; j++)
    {
        ss=0;
        for(int i=0; i<n; i++) ss+=a_temp[i][j];
        if(ss!=s) return false;
    }

    ss=0;
    for(int i=0; i<n; i++) ss+=a_temp[i][i];
    if(ss!=s) return false;

    ss=0;
    for(int i=0; i<n; i++) ss+=a_temp[i][n-1-i];
    if(ss!=s) return false;

    return true;
}

void solve_trivial()
{
    sort(a, a+n*n);

    if(ok_trivial())
    {
        cout << s;
        for(int i=0; i<n; i++)
        {
            cout << '\n';
            for(int j=0; j<n; j++) cout << a_temp[i][j] << ' ';
        }
        return;
    }
    while(next_permutation(a, a+n*n))
    {
        if(ok_trivial())
        {
            cout << s;
            for(int i=0; i<n; i++)
            {
                cout << '\n';
                for(int j=0; j<n; j++) cout << a_temp[i][j] << ' ';
            }
            return;
        }
    }
}

int fnd(const choice& C, int val)
{
    for(int i=0; i<n*n; i++)
    {
        if(!C.used[i] && a[i]==val) return i;
    }
    return -1;
}

bool ok(choice &C)
{
    //bool result=true;
    int ind;

    ind=fnd(C, s-(C.a1+C.a5+C.a9));
    if(ind==-1) return false;
    C.used[ind]=true;
    C.b3=a[ind];

    ind=fnd(C, s-(C.b1+C.b2+C.b3));
    if(ind==-1) return false;
    C.used[ind]=true;
    C.b4=a[ind];

    ind=fnd(C, s-(C.a8+C.a9+C.b4));
    if(ind==-1) return false;
    C.used[ind]=true;
    C.b5=a[ind];

    ind=fnd(C, s-(C.a1+C.a4+C.b5));
    if(ind==-1) return false;
    C.used[ind]=true;
    C.b6=a[ind];

    ind=fnd(C, s-(C.a2+C.a5+C.a8));
    if(ind==-1) return false;
    C.used[ind]=true;
    C.b7=a[ind];

    ind=fnd(C, s-(C.a3+C.a6+C.a9));
    if(ind==-1) return false;
    C.used[ind]=true;
    C.b8=a[ind];

    //if(result)
    {
        cout << s << '\n';
        cout << C.a1 << ' ' << C.a4 << ' ' << C.b5 << ' ' << C.b6 << '\n';
        cout << C.a2 << ' ' << C.a5 << ' ' << C.a8 << ' ' << C.b7 << '\n';
        cout << C.a3 << ' ' << C.a6 << ' ' << C.a9 << ' ' << C.b8 << '\n';
        cout << C.b1 << ' ' << C.b2 << ' ' << C.b4 << ' ' << C.b3 ;//<< '\n';
    }
    return true;
    //return result;
}

void solve_4()
{
    sort(a, a+n*n);

    choice seed;
    seed.step=0;
    for(int i=0; i<n*n; i++) seed.used[i]=false;

    stack<choice> all;
    all.push(seed);

    while(!all.empty())
    {
        seed=all.top();
        all.pop();

        if(seed.step==0)
        {
            int n2=n*n;
            for(int i1=0; i1<n2; i1++)
            {
                for(int i2=0; i2<n2; i2++)
                {
                    for(int i3=0; i3<n2; i3++)
                    {
                        if(i1==i2 || i2==i3 || i1==i3) continue;
                        choice nova;
                        nova.used[i1]=true;
                        nova.used[i2]=true;
                        nova.used[i3]=true;
                        nova.step=1;

                        nova.a1=a[i1];
                        nova.a2=a[i2];
                        nova.a3=a[i3];

                        int ind=fnd(nova, s-(nova.a1+nova.a2+nova.a3) );
                        if(ind==-1) continue;
                        nova.b1=a[ind];
                        nova.used[ind]=true;
                        all.push(nova);
                    }
                }
            }
        }
        else if(seed.step==1)
        {
            int n2=n*n;
            for(int i1=0; i1<n2; i1++)
            {
                if(seed.used[i1]) continue;
                for(int i2=0; i2<n2; i2++)
                {
                    if(seed.used[i2]) continue;
                    for(int i3=0; i3<n2; i3++)
                    {
                        if(seed.used[i3]) continue;
                        if(i1==i2 || i2==i3 || i1==i3) continue;
                        choice nova;
                        nova.used[i1]=true;
                        nova.used[i2]=true;
                        nova.used[i3]=true;
                        nova.step=2;

                        nova.a4=a[i1];
                        nova.a5=a[i2];
                        nova.a6=a[i3];

                        int ind=fnd(nova, s-(nova.a4+nova.a5+nova.a6) );
                        if(ind==-1) continue;
                        nova.b2=a[ind];
                        nova.used[ind]=true;
                        all.push(nova);
                    }
                }
            }
        }
        else // seed.step==2;
        {
            int n2=n*n;
            for(int i1=0; i1<n2; i1++)
            {
                if(seed.used[i1]) continue;
                for(int i2=0; i2<n2; i2++)
                {
                    if(seed.used[i2]) continue;
                    if(i1==i2) continue;
                    choice nova;
                    nova.used[i1]=true;
                    nova.used[i2]=true;

                    nova.a8=a[i1];
                    nova.a9=a[i2];
                    if(ok(nova))
                    {

                        return;
                    }
                }
            }
        }
    }
}

int main()
{
    cin >> n;
    s=0;
    for(int i=0; i<n*n; i++)
    {
        cin >> a[i];
        s+=a[i];
    }
    s/=n;

    if(n<=3) solve_trivial();
    else solve_4();
    return 0;
}

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>

using namespace std;

typedef long long LL;
int n, a[17], b[5][5];
LL sum;
map<LL, int> M;

bool check() {
	for (int i = 1; i <= n; ++i) {
		int t = 0;
		for (int j = 1; j <= n; ++j) {
			t += b[i][j];
		}
		if (t != sum) return false;
	}
	for (int j = 1; j <= n; ++j) {
		int t = 0;
		for (int i = 1; i <= n; ++i) {
			t += b[i][j];
		}
		if (t != sum) return false;
	}
	int t = 0;
	for (int i = 1; i <= n; ++i) {
		t += b[i][i];
	}
	if (t != sum) return false;
	t = 0;
	for (int i = 1; i <= n; ++i) {
		t += b[i][n - i + 1];
	}
	if (t != sum) return false;
	cout << sum << endl;
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) {
			cout << b[i][j];
			if (j == n) {
				cout << endl;
			} else {
				cout << ' ';
			}
		}
	}
	return true;
}

bool dfs(int i, int j, LL tsum) {
	if (j == n) {
		LL r = sum - tsum;
		if (M[r] == 0) return false;
		--M[r];
		b[i][j] = r;
		if (dfs(i + 1, 1, 0)) return true;
		++M[r];
		return false;
	}
	if (i == n + 1) {
		if (check()) return true;
		return false;
	} else {
		for (int k = 1; k <= n * n; ++k) {
			if (M[a[k]]) {
				--M[a[k]];
				b[i][j] = a[k];
				if (dfs(i, j + 1, tsum + a[k])) return true;
				++M[a[k]];
			}
		}
		return false;
	}
}

int main() {
	cin >> n;
	for (int i = 1, k = 0; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) {
			cin >> a[++k];
			M[a[k]] += 1;
		}
	}
	sort(a + 1, a + 1 + n * n);
	for (int i = 1, k = 0; i <= n; ++i)
		for (int j = 1; j <= n; ++j)
			sum += a[++k];
	sum /= n;
	dfs(1, 1, 0);
	return 0;
}

#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <memory.h>
#include <cstring>
#include <ctime>
#include <cmath>
#include <set>
#include <map>
#include <algorithm>
using namespace std;

#define forn( i,n ) for ( int i=0; i<(int)(n); i++ )
#define pb push_back
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;

int s, n, b[7][7], a[49], used[49], n2;

void rec(int i, int j, int sr) {
  if (i == n) {
    forn(q, n) {
      forn(w, n) printf("%d ", b[q][w]);
      printf("\n");
    }
    exit(0);
  }

  if (j == n-1) {
    int r1 = s - sr;
    int r2 = r1;
    int r3 = r1;
    if (i == n - 1) {
      r2 = 0, r3 = 0;
      forn(z, n-1) r2 += b[z][z], r3 += b[z][n-1];
      r2 = s - r2, r3 = s - r3;
    }
    if (r1 != r2 || r1 != r3) return;
    forn(q, n2)
      if (!used[q] && a[q] == r1) {
        used[q] = true;
        b[i][j] = a[q];
        rec(i+1, 0, 0);
        b[i][j] = 0;
        used[q] = false;
        break;
      }
    return;
  }

  if (i == n-1) {
    int sw = 0;
    forn(z, n-1) sw += b[z][j];
    int t1 = s - sw;
    int t2 = t1;
    if (j == 0) {
      int ss = 0;
      forn(z, n-1) ss += b[z][n-1-z];
      t2 = s - ss;
    }

    if (t1 != t2) return;

    forn(q, n2)
      if (!used[q] && a[q] == t1) {
        used[q] = true;
        b[i][j] = a[q];
        rec(i, j+1, sr + a[q]);
        b[i][j] = 0;
        used[q] = false;
      }
    return;
  }

  int lu = -1;
  forn(q, n2)
    if (!used[q] && a[q] != lu) {
      used[q] = true;
      b[i][j] = lu = a[q];
      rec(i, j+1, sr+lu);
      used[q] = false;
    }
}

int main()
{
  scanf("%d", &n);
  n2 = n*n;
  forn(i, n2) scanf("%d", &a[i]);
  sort(a, a+n2);
  s = 0;
  forn(i, n2) s += a[i];
  s /= n;
  printf("%d\n", s);
  rec(0, 0, 0);
  return 0;
}

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

long long S;
int n;
int a[32];

bool us[32];

int t[4][4];

bool cmag()
{
	int i, j, k, s;
			s = 0;
			for ( i = 0; i < n; i++ )
				s = s + t[0][i];

			for ( i = 0; i < n; i++ )
			{
				k = 0;
				for ( j = 0; j < n; j++ )
					k = k + t[i][j];

				if ( k != s ) return 0;

				k = 0;
				for ( j = 0; j < n; j++ )
				{
					k = k + t[j][i];
				}
				if ( k != s ) return 0;
			}

	k = 0;
	for ( i = 0; i < n; i++ )
	{
		k = k + t[i][i];
	}
	if ( k != s ) return 0;

	 k = 0;
	for ( i = 0; i < n; i++ )
	{
		k = k + t[i][n-i-1];
	}
	if ( k != s ) return 0;

	return 1;
}

inline bool mark( int val )
{
	for ( int i = 0; i < n*n; i++ )
	{
		if ( ( a[i] == val ) && ( us[i] == 0 ) )
		{
			us[i] = 1;
			return 1;
		}
	}
	return 0;
}

inline void unmark( int val )
{
	int i;
	for ( i = 0; i < n*n; i++ )
	{
		if ( ( a[i] == val ) && ( us[i] == 1 ) )
		{
			us[i] = 0;
			break;
		}
	}
//	if ( i >= n*n ) while ( 1 ); // ne vliza tuk.
}

bool make4( int st )
{
	if ( st > 7 )
	{
		return cmag();
	}
	if ( st < 3 )
	{
		for ( int i = 0; i < n*n; i++ )
		{
			if ( us[i] == 0 )
			{
				us[i] = 1;
				t[0][st] = a[i];
				if ( st == 2 )
				{
					t[0][3] = S - t[0][0] - t[0][1] - t[0][2];
					if ( mark( t[0][3] ) )
					{
						if ( make4( st+1 ) ) return 1;
						unmark( t[0][3] );
					}
				}
				else
				{
					if ( make4( st+1 ) )
						return 1;
				}
				us[i] = 0;
			}
		}
		return 0;
	}
	else if ( ( st == 3 ) || ( st == 4 ) )
	{
		for ( int i = 0; i < n*n; i++ )
		{
			if ( us[i] == 0 )
			{
				us[i] = 1;
				t[st-2][0] = a[i];
				if ( st == 4 )
				{
					t[3][0] = S - t[0][0] - t[1][0] - t[2][0];
					if ( mark( t[3][0] ) )
					{
						if ( make4( st+1 ) ) return 1;
						unmark( t[3][0] );
					}
				}
				else
				{
					if ( make4( st+1 ) )
						return 1;
				}
				us[i] = 0;
			}
		}
		return 0;
	}
	else if ( st == 5 )
	{
		for ( int i = 0; i < n*n; i++ )
		{
			if ( us[i] == 0 )
			{
				us[i] = 1;
				t[1][2] = a[i];
				t[2][1] = S - t[3][0] - t[1][2] - t[0][3];
				if ( mark( t[2][1] ) )
				{
					if ( make4( st+1 ) )
						return 1;
					unmark( t[2][1] );
				}
				us[i] = 0;
			}
		}
		return 0;
	}
	else if ( st == 6 )
	{
		for ( int i = 0; i < n*n; i++ )
		{
			if ( us[i] == 0 )
			{
				us[i] = 1;
				t[2][2] = a[i];
				t[2][3] = S - t[2][0] - t[2][1] - t[2][2];
				t[3][2] = S - t[0][2] - t[1][2] - t[2][2];
				if ( mark( t[3][2] ) )
				{
					if ( mark( t[2][3] ) )
					{
						if ( make4( st+1 ) )
							return 1;
						unmark( t[2][3] );
					}
					unmark( t[3][2] );
				}
				us[i] = 0;
			}
		}
		return 0;
	}
	else if ( st == 7 )
	{
		for ( int i = 0; i < n*n; i++ )
		{
			if ( us[i] == 0 )
			{
				us[i] = 1;
				t[1][1] = a[i];
				t[1][3] = S - t[1][0] - t[1][1] - t[1][2];
				t[3][1] = S - t[0][1] - t[1][1] - t[2][1];
				t[3][3] = S - t[0][0] - t[1][1] - t[2][2];

				if ( mark( t[3][1] ) )
				{
					if ( mark( t[1][3] ) )
					{
						if ( mark( t[3][3] ) )
						{
							if ( make4( st+1 ) )
								return 1;
							unmark( t[3][3] );
						}
						unmark( t[1][3] );
					}
					unmark( t[3][1] );
				}
				us[i] = 0;
			}
		}
		return 0;
	}
	return 0;
}

int main()
{
	int i, j;
	int s;

	scanf( "%d", &n );
	S = 0;
	for ( i = 0; i < n*n; i++ )
	{
		scanf( "%d", &a[i] );
		S = S + a[i];
	}

	S = S / n;

	memset( us, 0, sizeof( us ) );

	if ( n <= 3 )
	{
		sort( a, a + n*n );
		do
		{
			for ( i = 0; i < n; i++ )
			{
				for ( j = 0; j < n; j++ )
				{
					t[i][j] = a[ n*i + j ];
				}
			}

			if ( cmag() ) break;
		}
		while ( next_permutation( a, a + n*n ) );
	}
	else
	{
		make4( 0 );
	}

			s = 0;
			for ( i = 0; i < n; i++ )
				s = s + t[0][i];

	if ( ( S != s ) )
	{
		while ( 1 ); // vliza tuka -> ne e qko!
	}

	printf( "%d\n", s );
	for ( i = 0; i < n; i++ )
	{
		printf( "%d", t[i][0] );
		for ( j = 1; j < n; j++ )
			printf( " %d", t[i][j] );
		printf( "\n" );
	}
	return 0;
}

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <string>
#include <map>
#include <utility>
#include <vector>
#include <set>
#include <algorithm>

#define maxn 5
#define Fup(i, s, t) for (int i = s; i <= t; i ++)
#define Fdn(i, s, t) for (int i = s; i >= t; i --)
#define Path(i, s) for (int i = s; i; i = d[i].next)

using namespace std;

map <int, int>id, pos;
int ans[maxn][maxn];
int use[maxn * maxn], tt[maxn * maxn], ss[maxn];
bool flag;
int n, sum, S;

bool success()
{
    int s1 = 0, s2 = 0;
    Fup(i, 1, n){
        s1 += ans[i][i];
        s2 += ans[i][n - i + 1];
    }
    if (s1 == sum && s2 == sum)
        return true;
    return false;
}

void DFS(int x, int y, int s)
{
    if (x == n){
        if (success()){
            Fup(i, 1, n){
                Fup(j, 1, n - 1)
                    cout << ans[i][j] << ' ';
                cout << ans[i][n] << endl;
            }
            flag = true;
        }
        return;
    }
    if (y == n){
        if (pos[sum - s] && use[pos[sum - s]]){
            use[pos[sum - s]] --;
            ans[x][y] = sum - s;
            ss[y] += ans[x][y];
            if (x == n - 1){
                if (pos[sum - ss[y]] && use[pos[sum - ss[y]]]){
                    use[pos[sum - ss[y]]] --;
                    ans[n][y] = sum - ss[y];
                    DFS(x + 1, 1, 0);
                    if (flag)
                        return;
                    use[pos[sum - ss[y]]] ++;
                }
                ss[y] -= ans[x][y];
                use[pos[sum - s]] ++;
                return;
            }
            DFS(x + 1, 1, 0);
            if (flag)
                return;
            ss[y] -= ans[x][y];
            use[pos[sum - s]] ++;
        }
        return;
    }
    Fup(i, 1, S)
        if (use[i]){
            use[i] --;
            ans[x][y] = id[i];
            ss[y] += ans[x][y];
            if (x == n - 1){
                if (pos[sum - ss[y]] && use[pos[sum - ss[y]]]){
                    use[pos[sum - ss[y]]] --;
                    ans[n][y] = sum - ss[y];
                    DFS(x, y + 1, s + id[i]);
                    if (flag)
                        return;
                    use[pos[sum - ss[y]]] ++;
                }
                ss[y] -= ans[x][y];
                use[i] ++;
                continue;
            }
            DFS(x, y + 1, s + id[i]);
            ss[y] -= ans[x][y];
            use[i] ++;
            if (flag)
                return;
        }
}

void init()
{
    cin >> n;
    sum = 0;
    memset(use, 0, sizeof(use));
    Fup(i, 1, n * n){
        int x;
        cin >> x;
        if (!pos[x]){
            pos[x] = ++ S;
            id[S] = x;
        }
        use[pos[x]] ++;
        sum += x;
    }
}

void solve()
{
    sum /= n;
    cout << sum << endl;
    flag = false;
    DFS(1, 1, 0);
}

int main()
{
//    freopen("temp.in", "r", stdin);
//    freopen("temp.out", "w", stdout);
    ios::sync_with_stdio(false);
    init();
    solve();
    return 0;
}

#include <vector>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <string>
#include <bitset>
#include <complex>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
#include <numeric>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
using namespace std;

int ma[10][10];
int a[20], n, N;
map<int, int> mp;
int v[20], tot;

int getV(int x) {
    if (mp.count(x)) return mp[x];
    mp[x] = tot;
    v[tot++] = x;
    return tot - 1;
}

int orderx[20], ordery[20];
long long RS[10], CS[10], DS, DDS;
bool used[20];
long long S;

bool dfs(int dep) {
    if (dep == N) {
        return DS == S;
    }
    int x = orderx[dep], y = ordery[dep];
    for (int i = 0; i < N; ++i) if (!used[i] && (i == 0 || a[i] != a[i - 1])) {
        used[i] = true;
        RS[x] += v[a[i]];
        CS[y] += v[a[i]];
        if (x == y) DS += v[a[i]];
        if (x + y == n - 1) DDS += v[a[i]];
        bool flag = true;
        if (y == n - 1 && RS[x] != S) flag = false;
        if (x == n - 1 && CS[y] != S) flag = false;
        if (x == n - 1 && y == 0 && DDS != S) flag = false;
        ma[x][y] = v[a[i]];
        /*
        if (dep == 5 && ma[0][0] == 2 && ma[0][1] == 7 && ma[1][0] == 9 && ma[0][2] == 6 && ma[1][1] == 5 && ma[2][0] == 4) {
            cout << "!" << endl;
        }
        */
        if (flag) {
            if (dfs(dep + 1)) return true;
        }
        used[i] = false;
        RS[x] -= v[a[i]];
        CS[y] -= v[a[i]];
        if (x == y) DS -= v[a[i]];
        if (x + y == n - 1) DDS -= v[a[i]];
    }
    return false;
}

int main() {
    cin >> n;
    N = n * n;
    for (int i = 0; i < N; ++i) {
        cin >> a[i];
    }
    sort(a, a + N);
    for (int i = 0; i < N; ++i) {
        a[i] = getV(a[i]);
    }
    int t = 0;
    for (int i = 0; i <= n + n - 2; ++i) {
        for (int x = 0; x < n; ++x) {
            int y = i - x;
            if (y >= 0 && y < n) {
                orderx[t] = x;
                ordery[t] = y;
                t++;
            }
        }
    }
    S = 0;
    for (int i = 0; i < N; ++i) {
        S += v[a[i]];
    }
    S /= n;
    cout << S << endl;
    DS = DDS = 0;
    dfs(0);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n - 1; ++j) {
            cout << ma[i][j] << " ";
        }
        cout << ma[i][n - 1] << endl;
    }
}

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long i64;
#define maxn 25
int n,m,sum,avl;
int ans[maxn],pos[25];
bool vis[25];
bool pok()//main diagonal
{
    int i,s=0,j;
    for(i=1,j=0;i<=n;i++,j++)
    {
        s+=ans[i+j*n];
    }
    return s==avl;
}
bool qok()//secondary diagonal
{
    int i,s=0,j;
    for(i=n-1,j=n;i>=0;i--,j--)
    {
        s+=ans[j*n-i];
    }
    return s==avl;
}
bool qpok(int c)//column
{
    int i,j,s=0;
    for(i=0;i<n;i++)
    {
        s+=ans[c-i*n];
    }
    return s==avl;
}
bool pqok(int r)//row
{
    int i,j,s=0;
    for(i=r,j=0;j<n;j++)
    {
        s+=ans[i-j];
    }
    return s==avl;
}
bool dfs(int ln)
{
    int i,j,k,pre=-1;
    if(ln==m+1)
    {
        if(pok()&&qok())
        {
            printf("%d\n",avl);
            for(i=1;i<=m;i++)
            {
                printf("%d%c",ans[i],i%n==0?'\n':' ');
            }
            return 1;
        }
        return 0;
    }
    for(i=1;i<=m;i++)
    {
        if(pos[i]==pre) continue;
        if(vis[i]) continue;
        pre=pos[i];
        ans[ln]=pos[i];
        if(ln%n==0)
        {
            if(!pqok(ln)) continue;
        }
        if(ln>m-n)
        {
            if(!qpok(ln)) continue;
        }
        vis[i]=1;
        if(dfs(ln+1)) return 1;
        vis[i]=0;
    }
    return 0;
}
int main()
{
    int i,j,k;
    while(scanf("%d",&n)!=EOF)
    {
        sum=0;
        m=n*n;
        for(i=1;i<=m;i++)
        {
            scanf("%d",&pos[i]);
            sum+=pos[i];
        }
        avl=sum/n;
        memset(vis,0,sizeof(vis));
        dfs(1);
    }
    return 0;
}

# include <stdio.h>
# include <stdlib.h>
int a,sum,sum1,sum2;
int c[20];
int d[9][9];
int v[20];
void btr(int x)
{
if(x>=a*2 && x%a==0)
{
sum=0;
for(int j=0;j<a;j++)
sum=sum+d[x/a-2][j];
for(int j=0;j<a;j++)
sum=sum-d[x/a-1][j];
if(sum!=0)
return;
}
if(a*a==x)
{
sum=0;
sum1=0;
sum2=0;
for(int i=0;i<a;i++)
{
sum=sum+d[i][i]-d[i][a-1-i];
sum1=sum1+d[0][i];
}
for(int i=1;i<a;i++)
{
sum2=0;
for(int j=0;j<a;j++)
sum2=sum2+d[j][i];
if(sum2!=sum1)
return;
}
if(sum!=0)
return;
printf("%d\n",sum1);
for(int i=0;i<a;i++)
{
for(int j=0;j<a-1;j++)
printf("%d ",d[i][j]);
printf("%d\n",d[i][a-1]);
}
exit(0);
}
for(int i=0;i<a*a;i++)
if(v[i]==0)
{
v[i]=1;
d[x/a][x%a]=c[i];
btr(x+1);
v[i]=0;
}
}
int main()
{
scanf("%d",&a);
for(int i=0;i<a*a;i++)
scanf("%d",&c[i]);
if(a==1)
{
printf("%d\n",c[0]);
return 0;
}
btr(0);
}

#include<string>
#include<iostream>
#include<algorithm>
#include<vector>
//#include<list>
//#include<stack>
//#include<queue>
//#include<map>
//#include<set>
//#include<bitset>
//#include<unordered_set>
//#include<unordered_map>

using namespace std;

int main()
{
	int n;
	int sum = 0;
	bool flag = true;
	cin >> n;
	vector<int> v(n*n);
	for (int i = 0; i < n*n; ++i){
		cin >> v[i];
		sum += v[i];
	}
	if (n == 1){
		cout << v[0] << endl;
		cout << v[0] << endl;
	}
	else if (n == 2){
		cout << v[0] * 2 << endl;
		cout << v[0] << " " << v[0] << endl;
		cout << v[0] << " " << v[0] << endl;
	}
	else{
		sum /= 3;
		cout << sum << endl;
		do{
			int s = 0, t = 0;
			flag = true;
			for (int i = 0; i < n; ++i){
				s = 0;
				t = 0;
				for (int j = 0; j < n; ++j){
					s += v[j + i*n];
					t += v[j*n + i];
				}
				if (s != sum || t != sum){
					flag = false;
					break;
				}

			}
			if (flag){
				s = 0;
				t = 0;
				for (int i = 0; i < n; ++i){
					s += v[i*n + i];
					t += v[(n - 1 - i)*n + i];
				}
				if (s != sum || t != sum){
					flag = false;
				}
			}
			if (flag){
				cout << v[0] << " " << v[1] << " " << v[2] << endl;
				cout << v[3] << " " << v[4] << " " << v[5] << endl;
				cout << v[6] << " " << v[7] << " " << v[8] << endl;
				break;
			}

		} while (next_permutation(v.begin(), v.end()));
	}

	return 0;
}

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

inline bool check(ll b[4][4], ll sum){
	for(int i = 0; i < 4; i++){
		ll tmp = 0;
		for(int j = 0; j < 4; j++){
			tmp += b[i][j];
		}
		if(tmp != sum)return false;
	}
	for(int i = 0; i < 4; i++){
		ll tmp = 0;
		for(int j = 0; j < 4; j++){
			tmp += b[j][i];
		}
		if(tmp != sum)return false;
	}
	ll tmp = 0;
	for(int i = 0; i < 4; i++){
		tmp += b[i][i];
	}
	return tmp == sum;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cout<<setprecision(32);

	int n;
	cin>>n;
	int m = n*n;
	vector<ll> a(m);
	ll sum = 0;
	for(ll &x : a){
		cin>>x;
		sum += x;
	}
	assert(sum%n == 0);
	sum /= n;
	if(n <= 3){
		sort(a.begin(), a.end());
		while(true){
			ll b[n][n];
			for(int i = 0; i < n; i++){
				for(int j = 0; j < n; j++){
					b[i][j] = a[n*i + j];
				}
			}
			bool flg = true;
			for(int i = 0; i < n; i++){
				ll tmp = 0;
				for(int j = 0; j < n; j++){
					tmp += b[i][j];
				}
				if(tmp != sum)flg = false;
				tmp = 0;
				for(int j = 0; j < n; j++){
					tmp += b[j][i];
				}
				if(tmp != sum)flg = false;
			}
			ll tmp = 0;
			for(int i = 0; i < n; i++){
				tmp += b[i][i];
			}
			if(tmp != sum)flg = false;
			if(flg){
				cout<<sum<<'\n';
				for(int i = 0; i < n; i++){
					for(int j = 0; j < n; j++){
						cout<<b[i][j]<<" ";
					}
					cout<<'\n';
				}
				exit(0);
			}
			if(!next_permutation(a.begin(), a.end()))break;
		}
		assert(false);
	}
	vector<int> pos;
	for(int i = 0; i < 16; i++){
		for(int j = i + 1; j < 16; j++){
			for(int k = j + 1; k < 16; k++){
				for(int l = j + 1; l < 16; l++){
					if(a[i] + a[j] + a[k] + a[l] == sum){
						int mask = (1<<i)|(1<<j)|(1<<k)|(1<<l);
						pos.push_back(mask);
					}
				}
			}
		}
	}
	for(int i = 0; i < pos.size(); i++){
		for(int j = i + 1; j < pos.size(); j++){
			if(pos[i]&pos[j])continue;
			for(int k = j + 1; k < pos.size(); k++){
				if(pos[k]&(pos[i]|pos[j]))continue;
				for(int l = k + 1; l < pos.size(); l++){
					if(pos[l]&(pos[i]|pos[j]|pos[k]))continue;
					ll b[4][4];
					vector<int> r1, r2, r3, r4;
					for(int ind = 0; ind < 16; ind++){
						if(pos[i]&(1<<ind))r1.push_back(a[ind]);
						if(pos[j]&(1<<ind))r2.push_back(a[ind]);
						if(pos[k]&(1<<ind))r3.push_back(a[ind]);
						if(pos[l]&(1<<ind))r4.push_back(a[ind]);
					}
					sort(r1.begin(), r1.end());
					for(int ind = 0; ind < 4; ind++){
						b[0][ind] = r1[ind];
					}
					sort(r2.begin(), r2.end());
					while(true){
						for(int ind = 0; ind < 4; ind++){
							b[1][ind] = r2[ind];
						}
						sort(r3.begin(), r3.end());
						while(true){
							for(int ind = 0; ind < 4; ind++){
								b[2][ind] = r3[ind];
							}
							sort(r4.begin(), r4.end());
							while(true){
								for(int ind = 0; ind < 4; ind++){
									b[3][ind] = r4[ind];
								}
								if(check(b, sum)){
									for(int i = 0; i < 4; i++){
										for(int j = 0; j < 4; j++){
											cout<<b[i][j]<<" ";
										}
										cout<<'\n';
									}
									exit(0);
								}
								if(!next_permutation(r4.begin(), r4.end()))break;
							}
							if(!next_permutation(r3.begin(), r3.end()))break;
						}
						if(!next_permutation(r2.begin(), r2.end()))break;
					}
				}
			}
		}
	}

	return 0;
}

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string>
#include <cstdlib>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <ctime>
#include <cassert>
#include <list>
#include <bitset>
using namespace std;
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define forit(v,it) for(typeof(v.begin()) it = v.begin(); it != v.end(); ++it)
typedef long long ll;
typedef pair <int, int> pii;
typedef vector <int> vi;

int n;
ll a[100],s,b[20][20],r[20],c[20];

void solve() {
	sort(a + 1,a + n*n + 1);
	do {
		int v = 0;
		for(int i=1;i<=n*n;++i) {
			if(i%n == 1) ++v;
			int x = i%n;
			if(x == 0) x = n;
			b[v][x] = a[i];
		}
		for(int i=1;i<=n;++i) {
			r[i] = 0;
			c[i] = 0;
		}
		ll l = 0,x = 0;
		for(int i=1;i<=n;++i) {
			for(int j=1;j<=n;++j) {
				r[i] += b[i][j];
				c[j] += b[i][j];
				if(i == j) l += b[i][j];
				if(i + j == n + 1) x += b[i][j];
			}
		}
		bool ok = true;
		if(x != s  || l != s) ok = false;
		for(int i=1;i<=n  &&  ok;++i) {
			if(r[i] != s  ||  c[i] != s) ok = false;
		}
		if(ok) {
			printf("%I64d\n",s);
			for(int i=1;i<=n;++i) {
				for(int j=1;j<=n;++j) {
					printf("%I64d ",b[i][j]);
				}
				puts("");
			}
			exit(0);
		}

	} while(next_permutation(a + 1,a + n*n + 1));
}

int main () {
	#ifdef LOCAL
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	#endif
	scanf("%d",&n);
	for(int i=1;i<=n*n;++i) {
		scanf("%I64d",&a[i]);
		s += a[i];
	}
	if(n == 1) {
		printf("%I64d \n",a[1]);
		return 0;
	}
	s /= n;
	solve();
	return 0;
}

#include<stdio.h>
#include<iostream>
#include<vector>
#include<cmath>
#include<algorithm>
#include<memory.h>
#include<map>
#include<set>
#include<queue>
#include<list>
#include<sstream>
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define SS stringstream
#define sqr(x) ((x)*(x))
#define m0(x) memset(x,0,sizeof(x))
#define m1(x) memset(x,63,sizeof(x))
#define CC(x) cout << (x) << endl
#define AL(x) x.begin(),x.end()
#define pw(x) (1ull<<(x))
#define M 1000000007
using namespace std;
typedef pair<int,int> pt;
typedef vector<int> vt;

int n,a[55],e[5][5],q,s,f[55],A[5],B[5];

void rec(int x,int y){
	if (y==n){
		if (A[x]*n!=s)return;
		y=0,x++;
	}
	if (x==n){
		for (int i=0;i<n;i++)if (A[i]*n!=s)return;
		for (int i=0;i<n;i++)if (B[i]*n!=s)return;
		q=0;
		for (int i=0;i<n;i++)q+=e[i][i];
		if (q*n!=s)return;
		q=0;
		for (int i=0;i<n;i++)q+=e[i][n-1-i];
		if (q*n!=s)return;
		for (int i=0;i<n;i++){
			for (int j=0;j<n;j++)cout << e[i][j] << " ";
			puts("");
		}
		exit(0);
	}
	for (int i=0;i<n*n;i++)if (!f[i]){
		if (i>0&&f[i-1]==0&&a[i]==a[i-1])continue;
		e[x][y]=a[i];
		A[x]+=a[i];
		B[y]+=a[i];
		f[i]=1;
		rec(x,y+1);
		f[i]=0;
		A[x]-=a[i];
		B[y]-=a[i];
	}
}

int main(){
//	freopen("1.in","r",stdin);
//	freopen("1.out","w",stdout);
	cin >> n;
	for (int i=0;i<n*n;i++)cin >> a[i],s+=a[i];
	sort(a,a+n*n);
	rec(0,0);
	return 0;
}

#include<iostream>
#include<algorithm>
#include <cstdlib>

using namespace std;

int n, totalSum, size;
int res[20], a[20], g[20];
bool mark[20];
int sum;
int n2;

inline void check()
{
	for (int i = 0; i < n2; i++)
	{
		cout << res[i] << " ";
		if (i % n == n - 1)
			cout << endl;
	}
	exit(0);
}

void backTrack(int x)
{
	if(x == 3 && res[2] > res[1])
		return;
	if(x == 3)
	{
		sum = res[0] + res[1] + res[2];
		bool flag = false;
		int h;
		for(int i = 0; i < 16; i++)
			if(!mark[i] && totalSum == sum + a[i])
			{
				mark[i] = true;
				h = i;
				res[3] = a[i];
				flag = true;
				break;
			}
		if(!flag || res[3] < res[0])
			return;
		else
		{
			backTrack(x + 1);
			mark[h] = false;
			return;
		}
	}
	else if(x == 7)
	{
		sum = res[4] + res[5] + res[6];
		bool flag = false;
		int h;
		for(int i = 0; i < 16; i++)
			if(!mark[i] && totalSum == sum + a[i])
			{
				mark[i] = true;
				h = i;
				res[7] = a[i];
				flag = true;
				break;
			}
		if(!flag)
			return;
		else
		{
			backTrack(x + 1);
			mark[h] = false;
			return;
		}
	}
	else if(x > 0 && x % 4 == 0)
	{
		sum = 0;
		for (int i = 0; i < 4; i++)
			sum += res[x - 4 + i];
		if (sum != totalSum)
			return;
	}
	if(x == 12)
	{
		int in = 0;
		for(int j = 0; j < 4; j++)
		{
			sum = res[j] + res[j + 4] + res[j + 8];
			bool flag = false;
			for(int i = 0; i < 16; i++)
				if(!mark[i] && totalSum == sum + a[i])
				{
					mark[i] = true;
					g[in++] = i;
					res[j + 12] = a[i];
					flag = true;
					break;
				}
			if(!flag)
			{
				for(int i = 0; i < in; i++)
					mark[g[i]] = false;
				return;
			}
		}
		sum = 0;
		for (int i = 3; i < 15; i += 3)
			sum += res[i];
		if (sum != totalSum)
		{
			for(int i = 0; i < in; i++)
				mark[g[i]] = false;
			return;
		}
		sum = 0;
		for (int i = 0; i < n2; i += n + 1)
			sum += res[i];
		if (sum != totalSum)
		{
			for(int i = 0; i < in; i++)
				mark[g[i]] = false;
			return;
		}
		check();
	}
	for (int i = 0; i < 16; i++)
		if (!mark[i])
		{
			mark[i] = true;
			res[x] = a[i];
			backTrack(x + 1);
			mark[i] = false;
		}
}

bool check3()
{
	// ROWS
	for (int i = 0; i < n; i++)
	{
		int sum = 0;
		for (int j = 0; j < n; j++)
			sum += res[i * n + j];
		if (sum != totalSum)
			return false;
	}
	// COLUMNS
	for (int j = 0; j < n; j++)
	{
		int sum = 0;
		for (int i = 0; i < n; i++)
			sum += res[i * n + j];
		if (sum != totalSum)
			return false;
	}
	// MAIN DIAG
	int mdsum = 0;
	for (int i = 0; i < n * n; i += n + 1)
		mdsum += res[i];
	// SEC DIAG
	int sdsum = 0;
	for (int i = n - 1; i < n * n - 1; i += n - 1)
		sdsum += res[i];
	if (mdsum != totalSum || sdsum != totalSum)
		return false;
	// OUTPUT
	for (int i = 0; i < n * n; i++)
	{
		cout << res[i] << " ";
		if (i % n == n - 1)
			cout << endl;
	}
	exit(0);
}

bool backTrack3(int x)
{
	//  cout << x << endl;
	if (x == n * n)
		return check3();
	for (int i = 0; i < n * n; i++)
		if (!mark[i])
		{
			mark[i] = true;
			res[x] = a[i];
			backTrack3(x + 1);
			mark[i] = false;
		}
}

int main()
{
	ios::sync_with_stdio(false);
	cin >> n;
	n2 = n * n;
	for (int i = 0; i < n * n; i++)
	{
		cin >> a[i];
		totalSum += a[i];
	}
	random_shuffle(a, a + n);
	totalSum /= n;
	cout << totalSum << endl;
	if(n == 1)
		cout << a[0] << endl;
	else if(n < 4)
		backTrack3(0);
	else
		backTrack(0);

	return 0;
}

#include <algorithm>
#include <cstdlib>
#include <iostream>
#define INF (1 << 30)

using namespace std;

int n, n2;
int a[16];
int s = 0;

bool check() {
    int t = 0;
    for (int i = 0; i < n; i++) {
        t = 0;
        for (int j = 0; j < n; j++)
            t += a[i * n + j];
        if (t != s)
            return false;

        t = 0;
        for (int j = 0; j < n; j++)
            t += a[j * n + i];
        if (t != s)
            return false;
    }

    t = 0;
    for (int i = 0; i < n; i++)
        t += a[i * n + i];
    if (t != s)
        return false;

    t = 0;
    for (int i = 0; i < n; i++)
        t += a[i * n + (n - i - 1)];
    if (t != s)
        return false;

    return true;
}

void solve_small() {
    do {
        if (check()) {
            cout << s << endl;
            for (int i = 0; i < n2; i++)
                cout << a[i] << endl;
            break;
        }
    } while (next_permutation(a, a + n2));
}

int b[4][4];
bool used[16];

void solve(int i, int j) {
    if (i == 4) { // done
        if (b[0][0] + b[1][1] + b[2][2] + b[3][3] != s)
            return;
        for (int k = 0; k < 4; k++) {
            for (int l = 0; l < 4; l++)
                cout << b[k][l] << " ";
            cout << endl;
        }
        exit(0);
    }
    if (i == 3 && j == 1) { // check diagonal
        if (b[0][3] + b[1][2] + b[2][1] + b[3][0] != s)
            return;
    }
    if (i == 3 && j > 0) { // check column
        if (b[0][j - 1] + b[1][j - 1] + b[2][j - 1] + b[3][j - 1] != s)
            return;
    }
    if (j == 4) { // check row
        if (b[i][0] + b[i][1] + b[i][2] + b[i][3] != s)
            return;
        solve(i + 1, 0);
        return;
    }

    int prev = INF;
    for (int k = 0; k < 16; k++)
        if (!used[k] && a[k] != prev) {
            prev = a[k];
            used[k] = true;
            b[i][j] = a[k];
            solve(i, j + 1);
            used[k] = false;
        }
}

int main() {
    cin >> n;
    n2 = n * n;
    for (int i = 0; i < n2; i++) {
        cin >> a[i];
        s += a[i];
    }
    s /= n;
    sort(a, a + n2);

    if (n <= 3)
        solve_small();
    else
        solve(0, 0);
}

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <ctime>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <numeric>
#include <cassert>
using namespace std;
static const double EPS = 1e-10;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)
#define rev(i,n) for(int i=n-1;i>=0;i--)
#define all(a) a.begin(),a.end()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define SS stringstream
#define DBG1(a) rep(_X,sz(a)){printf("%d ",a[_X]);}puts("");
#define DBG2(a) rep(_X,sz(a)){rep(_Y,sz(a[_X]))printf("%d ",a[_X][_Y]);puts("");}
#define bitcount(b) __builtin_popcount(b)
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)

#define delete(a,n) a.erase(remove(all(a),n),a.end())
template<typename T, typename S> vector<T>& operator<<(vector<T>& a, S b) { a.push_back(b); return a; }
template<typename T> void operator>>(vector<T>& a, int b) {while(b--)if(!a.empty())a.pop_back();}
bool isprime(int n){ if(n<2)return false;  for(int i=2;i*i<=n;i++)if(n%i==0)return false;  return true;}
ll b_pow(ll x,ll n){return n ? b_pow(x*x,n/2)*(n%2?x:1) : 1ll;}
string itos(int n){stringstream ss;ss << n;return ss.str();}

map<int,int> use;
int A[16] , B[16];
int n;

int table[] = {0,5,10,15, 3,6,9,12, 1,2, 13,14, 4,8, 7,11};

int ans[16];
int tate_sum[4];
int yoko_sum[4];
int naname_sum;

int get = 0;
int sum = 0;

inline int tate_check(int x){
    return tate_sum[x];
}

inline int yoko_check(int x){
    return yoko_sum[x];
}
//　／　2

int sz = 0;

inline int naname_check2(){
    return naname_sum;
}

int flipflop = 0;

int dfs(int pos){
    if(pos == n*n ){
        if( 1
        ){
            cout << get << endl;
            for(int i = 0 ; i < 4 ; i++){
                for(int j= 0 ; j < 4 ; j++) cout << ans[i*4+j] << (j==3?"\n":" ");
            }
            exit(0);
        }
    }
    flipflop ^= 1;
    int p = table[pos];

    for(int i = 0 ; i < sz ; i++){
        if(B[i]){
            B[i]--;
            sum += A[i];
            ans[p] = A[i];
            if( pos == 0){
                yoko_sum[0] += A[i];
                tate_sum[0] += A[i];
                dfs(pos+1);
                yoko_sum[0] -= A[i];
                tate_sum[0] -= A[i];
            }else if( pos == 1){
                yoko_sum[1] += A[i];
                tate_sum[1] += A[i];
                dfs(pos+1);
                yoko_sum[1] -= A[i];
                tate_sum[1] -= A[i];
            }else if( pos == 2){
                yoko_sum[2] += A[i];
                tate_sum[2] += A[i];
                dfs(pos+1);
                yoko_sum[2] -= A[i];
                tate_sum[2] -= A[i];
            }else if( pos == 3){
                get = sum;
                yoko_sum[3] += A[i];
                tate_sum[3] += A[i];
                dfs(pos+1);
                yoko_sum[3] -= A[i];
                tate_sum[3] -= A[i];
            }else if(pos == 4){
                yoko_sum[0] += A[i];
                tate_sum[3] += A[i];
                naname_sum += A[i];
                dfs(pos+1);
                naname_sum -= A[i];
                yoko_sum[0] -= A[i];
                tate_sum[3] -= A[i];
            }else if(pos == 5){
                yoko_sum[1] += A[i];
                tate_sum[2] += A[i];
                naname_sum += A[i];
                dfs(pos+1);
                naname_sum -= A[i];
                yoko_sum[1] -= A[i];
                tate_sum[2] -= A[i];
            }else if(pos == 6){
                yoko_sum[2] += A[i];
                tate_sum[1] += A[i];
                naname_sum += A[i];
                dfs(pos+1);
                naname_sum -= A[i];
                yoko_sum[2] -= A[i];
                tate_sum[1] -= A[i];
            }else if(pos == 7 ){
                yoko_sum[3] += A[i];
                tate_sum[0] += A[i];
                naname_sum += A[i];
                if( naname_check2() == get){
                    dfs(pos+1);
                }
                naname_sum -= A[i];
                yoko_sum[3] -= A[i];
                tate_sum[0] -= A[i];
                //int table[] = {0,5,10,15, 3,6,9,12, 1,2, 13,14, 4,8, 7,11};
            }else if(pos == 8){
                yoko_sum[0] += A[i];
                tate_sum[1] += A[i];
                dfs(pos+1);
                yoko_sum[0] -= A[i];
                tate_sum[1] -= A[i];
            }else if(pos == 9){
                yoko_sum[0] += A[i];
                tate_sum[2] += A[i];
                if( yoko_check(0) == get){
                    dfs(pos+1);
                }
                yoko_sum[0] -= A[i];
                tate_sum[2] -= A[i];
            }else if(pos == 10){
                yoko_sum[3] += A[i];
                tate_sum[1] += A[i];
                if( tate_check(1) == get){
                    dfs(pos+1);
                }
                yoko_sum[3] -= A[i];
                tate_sum[1] -= A[i];
            }else if(pos == 11){
                yoko_sum[3] += A[i];
                tate_sum[2] += A[i];
                if( tate_check(2) == get && yoko_check(3) == get){
                    dfs(pos+1);
                }
                yoko_sum[3] -= A[i];
                tate_sum[2] -= A[i];
            }else if(pos == 12){
                yoko_sum[1] += A[i];
                tate_sum[0] += A[i];
                dfs(pos+1);
                yoko_sum[1] -= A[i];
                tate_sum[0] -= A[i];
            }else if(pos == 13){
                yoko_sum[2] += A[i];
                tate_sum[0] += A[i];
                if( tate_check(0) == get){
                    dfs(pos+1);
                }
                yoko_sum[2] -= A[i];
                tate_sum[0] -= A[i];
            }else if(pos == 14){
                yoko_sum[1] += A[i];
                tate_sum[3] += A[i];
                if( yoko_check(1) == get){
                    dfs(pos+1);
                }
                yoko_sum[1] -= A[i];
                tate_sum[3] -= A[i];
            }else if(pos == 15){
                yoko_sum[2] += A[i];
                tate_sum[3] += A[i];
                if( yoko_check(2) == get && tate_check(3) == get){
                    dfs(pos+1);
                }
                yoko_sum[2] -= A[i];
                tate_sum[3] -= A[i];
            }else{
                while(1){cerr << "BAD" << endl;}
            }
            sum -= A[i];
            B[i]++;
        }
    }
}

int solve(int n){
    int a[16] = {};
    for(int i = 0 ; i < n*n ; i++)
        cin >> a[i];
        sort(a,a+n*n);
    do{
        int s = 0 ,f =1;
        for(int i = 0 ; i < n ; i++) s += a[i];

        for(int i = 0 ; i < n ; i++){
            int t = 0;
            for(int j = 0 ; j < n ; j++)
                t += a[i*n+j];
            if(t != s) f = 0;
        }
        for(int i = 0 ; i < n ; i++){
            int t = 0;
            for(int j = 0 ; j < n ; j++)
                t += a[j*n+i];
            if(t != s) f = 0;
        }
        int x = 0;
        for(int i = 0 ; i < n ; i++) x += a[i*n+i];
        if( x != s) f=0;

        x = 0;
        for(int i = 0 ; i < n ; i++) x += a[i*n+(n-i-1)];
        if( x != s) f=0;

        if(f){
            cout << s << endl;
            for(int i = 0 ; i < n ; i++){
                for(int j = 0 ; j < n ; j++) cout << a[i*n+j] << (j==n-1?"\n":" ");
            }
            return 0;
        }
    }while(next_permutation(a,a+n*n));
}

int main(){
    ios_base::sync_with_stdio(false);
    cin >> n;
    if( n <= 3){
        solve(n);
        return 0;
    }else{
        map<int,int> use;
        for(int i = 0 ; i < n*n ; i++){
            int t;
            cin >> t;
            A[i] = t;
            B[i] = t;
        }
        sz = n*n;
        srand(time(NULL));
        random_shuffle(A,A+sz);
        dfs(0);
    }
    while(1){cout << "UEAAA" << endl;}

}

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<map>
#include<cmath>
using namespace std;
int a[1006],b[106],c[100],v[300];
long long sum;
int cmp(int a,int b)
{
    return a<b;
}
int n;

int main()
{
    int n;
    while(~scanf("%d",&n))
    {
     int i,j;
     sum=0;
     for(i=0;i<n*n;i++)
      {
       scanf("%d",&a[i]);
       sum+=a[i];
      }
      sum=sum/n;
     sort(a,a+n*n,cmp);
     memset(b,0,sizeof(b));
     memset(c,0,sizeof(c));
     if(n==1)
     printf("%d\n",a[0]);
     else if(n==2)
     {
     printf("%d %d\n",a[0],a[3]);
     printf("%d %d\n",a[1],a[2]);
     }
     else if(n==3)
     {
     if(a[1]+a[8]+a[2]==sum&&a[0]+a[6]+a[7]==sum)
     {
     printf("%d %d %d\n",a[1],a[5],a[6]);
     printf("%d %d %d\n",a[8],a[4],a[0]);
     printf("%d %d %d\n",a[2],a[3],a[7]);
     }
     else
     {
     printf("%d %d %d\n",a[1],a[6],a[5]);
     printf("%d %d %d\n",a[8],a[4],a[0]);
     printf("%d %d %d\n",a[3],a[2],a[7]);
     }
  }
}
}

#include <iostream>
#include <vector>
#include <set>
#include <memory.h>
#include <map>
#include <algorithm>
using namespace std;

int n;
vector <int> x;
#define X(i,j) x[i*n+j]

void view() {
    for (int i=0;i<n;i++) {
        for (int j=0;j<n-1;j++) cout << X(i,j) << " ";
        cout << x[i*n+n-1] << endl;
    }
}

void D() {
    int s = 0;
    for (int i=0;i<9;i++) s+=x[i];
    s/=3;
    sort(x.begin(),x.end());
    do {
        try {
            int r = 0, c = 0;
            for (int i=0;i<n;i++) {

                for (int j=0;j<n;j++) {
                    r+=X(i,j);
                    c+=X(j,i);
                }
                if (r!=s || c!=s) throw 1;
            }
            r = c = 0;
            for (int i=0;i<n;i++) {
                r+=X(i,i);
                c+=X(i,n-i-1);
            }
            if (r!=s || c!=s) throw 1;
            break;
        } catch(int) {}
    } while (next_permutation(x.begin(),x.end()));

}

int main() {

    cin >> n;
    x.resize(n*n);
    for (int i=0;i<n*n;i++) cin >> x[i];

    if (n>2) D();

    view();
}

#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_DEPRECATE

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#pragma comment(linker, "/STACK:64000000")

template<class T> inline T sqr (T x) {return x * x;}

typedef long long int64;
typedef unsigned long long uint64;
typedef pair<int, int> pii;
typedef pair<int, pii> pip;
typedef pair<pii, int> ppi;
typedef pair<int64, int> pii64;
typedef pair<double, double> pdd;
typedef pair<double, int> pdi;
typedef pair<string, int> psi;
typedef pair<int, string> pis;
#define FAIL ++*(int*)0
#define eps  1e-9
#define inf  0x7f7f7f7f
#define MP make_pair
#define sz(C) (int)((C).size())
#define all(C) (C).begin(), (C).end()
#define TASK "codeforces"
#define RR 300000001

int n;
int a[16];
int b[4][4];

void bf () {
    sort(a, a + n * n);
    do {
        for (int i = 0; i < n; ++i)
            for (int j = 0; j < n; ++j)
                b[i][j] = a[i * n + j];
        bool ok = true;
        int sum = accumulate(b[0], b[0] + n, 0);
        for (int i = 0; i < n && ok; ++i)
            ok &= accumulate(b[i], b[i] + n, 0) == sum;
        for (int j = 0; j < n && ok; ++j) {
            int cur = 0;
            for (int i = 0; i < n; ++i)
                cur += b[i][j];
            ok &= sum == cur;
        }
        int c1 = 0, c2 = 0;
        for (int i = 0; i < n; ++i) {
            c1 += b[i][i];
            c2 += b[i][n - i - 1];
        }
        ok &= sum == c1 && sum == c2;
        if (ok) {
            printf("%d\n", sum);
            for (int i = 0; i < n; ++i)
                for (int j = 0; j < n; ++j)
                    printf("%d%c", b[i][j], " \n"[j == n - 1]);
            return;
        }
    } while (next_permutation(a, a + n * n));
}

int sum;
int m;
int val[16];
int cnt[16];
char used[16] = {0};
int cntrow[4], cntcol[4];
int row[4], col[4];
int d1cnt = 0, d2cnt = 0;
int d1 = 0, d2 = 0;
pii to[16];
int MAX = 6 * 1000 * 1000;
int ITER = 0;

bool rec (int v) {
    if (v == n * n) return true;
    if (++ITER >= MAX) return false;
    int x = to[v].first;
    int y = to[v].second;
    for (int i = 0; i < m; ++i) {
        if (!cnt[i]) continue;
        if (cntrow[x] == n - 1 && row[x] + val[i] != sum) continue;
        if (cntcol[y] == n - 1 && col[y] + val[i] != sum) continue;
        if (x == y && d1cnt == n - 1 && d1 + val[i] != sum) continue;
        if (x + y == n - 1 && d2cnt == n - 1 && d2 + val[i] != sum) continue;
        --cnt[i];
        b[x][y] = val[i];
        ++cntrow[x], row[x] += val[i];
        ++cntcol[y], col[y] += val[i];
        if (x == y) ++d1cnt, d1 += val[i];
        if (x + y == n - 1) ++d2cnt, d2 += val[i];
        if (rec(v + 1)) return true;
        ++cnt[i];
        --cntrow[x], row[x] -= val[i];
        --cntcol[y], col[y] -= val[i];
        if (x == y) --d1cnt, d1 -= val[i];
        if (x + y == n - 1) --d2cnt, d2 -= val[i];
    }
    return false;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    scanf("%d", &n);
    for (int i = 0; i < n * n; ++i)
        scanf("%d", &a[i]);
    if (n < 4) {
        bf();
        return 0;
    }
    map<int, int> tmp;
    for (int i = 0; i < n * n; ++i)
        ++tmp[a[i]];
    int k = 0;
    for (map<int,int>::iterator it = tmp.begin(); it != tmp.end(); ++it, ++k)
        val[k] = it->first, cnt[k] = it->second;
    m = sz(tmp);
    sum = accumulate(a, a + n * n, 0) / n;
    for (int i = 0; i < n * n; ++i)
        to[i] = MP(i / n, i % n);
    srand(192942347u);
    clock_t begin = clock();
    while (double(clock() - begin) / CLK_TCK < 0.9) {
        random_shuffle(to, to + n * n);
        ITER = 0;
        if (rec(0)) break;
    }
    printf("%d\n", sum);
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < n; ++j)
            printf("%d%c", b[i][j], " \n"[j == n - 1]);

    return 0;
}

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <string>
#include <queue>
#include <map>
#include <set>

#define MAX 8
#define MASK 65536

using namespace std;
FILE *in; FILE *out;

int n;
int vis, goal;
int s[MAX], d[MAX];
int a[MAX * MAX];
int board[MAX][MAX];
vector <int> sums[MASK];

int find(int num)
{
	int left = 0, right = (int)sums[vis].size() - 1;
	while (left <= right)
	{
		int mid = (left + right) / 2;
		if (sums[vis][mid] == num) return 1;
		if (sums[vis][mid] < num) left = mid + 1;
		else right = mid - 1;
	}
	return 0;
}

int recurse(int row, int col, int csum)
{
	if (col > 0 && row >= n - 1)
		if (d[1] != goal) return 0;

	if (row == n - 1 && col == n - 1)
	{
		int idx = 0;
		while (vis & (1 << idx)) idx++;
		board[row][col] = a[idx];
		if (csum + a[idx] != goal) return 0;
		if (s[col] + a[idx] != goal) return 0;
		if (d[0] + a[idx] != goal) return 0;
		return 1;
	}

	if (row == n - 1)
	{
		for (int i = 0; i < n * n; i++)
		{
			if ((vis & (1 << i)) == 0 && s[col] + a[i] == goal)
			{
				board[row][col] = a[i];
				vis |= (1 << i); s[col] += a[i];
				if (row == col) d[0] += a[i];
				if (row == n - col - 1) d[1] += a[i];
				int ans = recurse(row, col + 1, csum + a[i]);
				if (row == n - col - 1) d[1] -= a[i];
				if (row == col) d[0] -= a[i];
				vis ^= (1 << i); s[col] -= a[i];
				return ans;
			}
		}
		return 0;
	}

	if (col == n - 1)
	{
		for (int i = 0; i < n * n; i++)
		{
			if ((vis & (1 << i)) == 0 && csum + a[i] == goal)
			{
				board[row][col] = a[i];
				vis |= (1 << i); s[col] += a[i];
				if (row == col) d[0] += a[i];
				if (row == n - col - 1) d[1] += a[i];
				int ans = recurse(row + 1, 0, 0);
				if (row == n - col - 1) d[1] -= a[i];
				if (row == col) d[0] -= a[i];
				vis ^= (1 << i); s[col] -= a[i];
				return ans;
			}
		}
		return 0;
	}

	for (int i = 0; i < n * n; i++) if ((vis & (1 << i)) == 0)
	{
		int flag = 1;
		if (flag && n - row == 3 && !find(goal - s[col] - a[i])) flag = 0;
		if (flag && n - col == 3 && !find(goal - csum - a[i])) flag = 0;
		if (flag && row == col && !find(goal - d[0] - a[i])) flag = 0;
		if (flag && row == n - col - 1 && !find(goal - d[1] - a[i])) flag = 0;
		if (flag)
		{
			board[row][col] = a[i];
			vis |= (1 << i); s[col] += a[i];
			if (row == col) d[0] += a[i];
			if (row == n - col - 1) d[1] += a[i];
			int ans = recurse(row, col + 1, csum + a[i]);
			if (row == n - col - 1) d[1] -= a[i];
			if (row == col) d[0] -= a[i];
			vis ^= (1 << i); s[col] -= a[i];
			if (ans) return 1;
		}
	}
	return 0;
}

void precalc()
{
	for (int mask = 0; mask < (1 << (n * n)); mask++)
	{
		for (int i = 0; i < n * n; i++) if ((mask & (1 << i)) == 0)
			for (int c = i + 1; c < n * n; c++) if ((mask & (1 << c)) == 0)
				sums[mask].push_back(a[i] + a[c]);
		sort(sums[mask].begin(), sums[mask].end());
		sums[mask].resize(unique(sums[mask].begin(), sums[mask].end()) - sums[mask].begin());
	}
}

int main(void)
{
	in = stdin; out = stdout;
//	in = fopen("D.in", "rt"); out = fopen("D.out", "wt");

	fscanf(in, "%d", &n);
	for (int i = 0; i < n * n; i++)
		fscanf(in, "%d", &a[i]);

	if (n == 1)
	{
		fprintf(out, "%d\n", a[0]);
		fprintf(out, "%d\n", a[0]);
		return 0;
	}

	precalc();

	goal = 0; vis = 0;
	d[0] = d[1] = 0;
	s[0] = s[1] = s[2] = s[3] = 0;
	for (int i = 0; i < n * n; i++)
		goal += a[i];
	goal /= n;
	recurse(0, 0, 0);

	fprintf(out, "%d\n", goal);
	for (int i = 0; i < n; i++)
		for (int c = 0; c < n; c++)
			fprintf(out, "%d%c", board[i][c], c + 1 == n ? '\n' : ' ');

	return 0;
}

#include <iostream>
#include <algorithm>
using namespace std;

int a[20], n, s, x, y, i, j, d3[4] = {2, 4, 6}, d4[5] = {3, 6, 9, 12};
int t[5][5], ch = 100;

int main() {
	ios_base::sync_with_stdio(0);
	cin >> n;
	for(int i = 0; i < n * n; i++)
        cin >> a[i];
    sort(a, a + n);
    bool f;
    while(next_permutation(a, a + n * n) || ch) {
        x = 0;
        ch--;
        f = true;
        for(i = 0; i < n; i++)
            for(j = 0; j < n; j++)
                t[i][j] = a[x++];
        s = 0;
        for(i = 0; i < n; i++) s += a[i];
        for(i = 0; i < n && f; i++) {
            x = y = 0;
            for(j = 0; j < n && f; j++)
                x += t[i][j], y += t[j][i];
            if (x != s || y != s) f = false;
        }
        if (!f) continue;
        for(i = x = y = 0; i < n; i++)
            x += t[i][i], y += t[i][n - 1 - i];
        if (x != s || y != s) continue;
        for(i = 0; i < n; i++) {
            for(j = 0; j < n; j++)
                cout << t[i][j] << ' ';
            cout << endl;
        }
        //cout << endl << endl;
        return 0;
    }
    return 0;
}

#include <bits/stdc++.h>
using  namespace std;
typedef long long int ll;
#define PB push_back
#define MP make_pair
const int N=5e4+20;
int n , a , num;
vector<int> ve;
int ans[4][4];
bool in[12];
int sum[9];
bool ch()
{
     int last = 0 ;
     memset(sum ,0,sizeof(sum));
     for(int i=0 ;i<n;++i){
     for(int j=0 ;j<n;++j)
     sum[last]+=ans[i][j];
     ++last;
     }

      for(int i=0 ;i<n;++i){
     for(int j=0 ;j<n;++j)
     sum[last]+=ans[j][i];
     ++last;
      }

     for(int i=0 ;i<n;++i)
     sum[last]+=ans[i][i];
     int x = n-1;
     ++last;
     for(int i=0 ;i<n;++i)
     sum[last]+=ans[i][x-i];

     for(int i=1 ; i <=last;++i)
     if(sum[i] != sum[i-1])
    return false;

     return true;
}
void dis()
{
     for(int i=0 ;i<n;++i)
     {
          for(int j=0 ;j<n;++j)
          printf("%d ",ans[i][j]);
          printf("\n");
     }
}
void solve(int x , int y)
{
     if(x == n-1 && y == n)
     {
         if(ch()){
         cout<<sum[0]<<endl;
         dis();
         exit(0);
         }
          return;
     }
     if(y == n)
     {y = 0; ++x;}
     for(int i=0 ;i<n*n;++i)
     {
          if(in[i]) continue;
          ans[x][y] = ve[i];
          in[i] = true;
          solve(x , y+1);
          in[i] = false;
     }

}
int main() {
     scanf("%d",&n);
     for(int i=0 ;i<n*n;++i)
     scanf("%d",&a) , ve.PB(a);
     if(n == 1)
     return printf("%d",ve[0]) , 0;
     solve(0,0);
   return 0;
}

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
	int n,a[9];
	long long s=0;
	scanf("%d",&n);
	for(int i=0;i<n*n;++i)
	{
		scanf("%d",&a[i]);
		s+=a[i];
	}
	if(n==1)printf("%d\n%d\n",a[0],a[0]);
	else if(n==2)
	{
		cout<<"test"<<endl;
		for(int i=0;i<2;++i)printf("%d %d\n",a[0],a[1]);
	}
	else
	{
		int ans[9];
		s/=3;
		ans[4]=s/3;
		long long ts=s-ans[4];
		int ck[9];
		bool flag;
	//	printf("%I64d\n%I64d\n",s,ts);
		for(int i=0;i<9;++i)
		{
			if(a[i]==ans[4])continue;
			for(int j=0;j<9;++j)
			{
				if(a[j]==ans[4])continue;
				ans[0]=a[i];ans[2]=a[j];
				ans[8]=ts-ans[0];ans[6]=ts-ans[2];
				ans[1]=s-ans[0]-ans[2];
				ans[3]=s-ans[0]-ans[6];
				ans[5]=s-ans[2]-ans[8];
				ans[7]=s-ans[6]-ans[8];
				if(ans[1]+ans[7]!=ts||ans[3]+ans[5]!=ts)continue;
				memcpy(ck,ans,sizeof(ans));
				sort(ck,ck+9);
				sort(a,a+9);
				flag=true;
				for(int i=0;i<9;++i)
					if(a[i]!=ck[i])
					{
						flag=false;break;
					}
				if(!flag)continue;
				cout<<s<<endl;
				for(int k=0;k<9;k+=3)
					printf("%d %d %d\n",ans[k],ans[k+1],ans[k+2]);
				return 0;
			}
		}
	}
	return 0;
}

#include <iostream>
#include <vector>

int n, sum = 0;
int a[16];
int mat[4][4];
int rows[4], cols[4];

bool dfs(int state, int mask) {
	//std::cout << "on state " << state << ", mask " << mask << std::endl;
	if(state == 0) {
		if(n == 1) {
			mat[0][0] = a[0];
			return true;
		} else if(n == 2) {
			for(int i = 0; i < n * n; i++) {
				for(int j = i + 1; j < n * n; j++) {
					if(a[i] + a[j] == sum) {
						int nxtMask = mask;
						mat[0][0] = a[i];
						nxtMask ^= 1 << i;
						mat[1][1] = a[j];
						nxtMask ^= 1 << j;
						rows[0] = cols[0] = a[i];
						rows[1] = cols[1] = a[j];
						if(dfs(state + 1, nxtMask)) {
							return true;
						}
					}
				}
			}
		} else if(n == 3) {
			for(int i = 0; i < n * n; i++) {
				for(int j = i + 1; j < n * n; j++) {
					for(int k = j + 1; k < n * n; k++) {
						if(a[i] + a[j] + a[k] == sum) {
							int nxtMask = mask;
							mat[0][0] = a[i];
							nxtMask ^= 1 << i;
							mat[1][1] = a[j];
							nxtMask ^= 1 << j;
							mat[2][2] = a[k];
							nxtMask ^= 1 << k;
							rows[0] = cols[0] = a[i];
							rows[1] = cols[1] = a[j];
							rows[2] = cols[2] = a[k];
							//std::cout << "choosing diagonal (" << i << ", " << j << ", " << k << ")\n";
							if(dfs(state + 1, nxtMask)) {
								return true;
							}
						}
					}
				}
			}
		} else {
			for(int i = 0; i < n * n; i++) {
				for(int j = i + 1; j < n * n; j++) {
					for(int k = j + 1; k < n * n; k++) {
						for(int l = k + 1; l < n * n; l++) {
							if(a[i] + a[j] + a[k] + a[l] == sum) {
								int nxtMask = mask;
								mat[0][0] = a[i];
								nxtMask ^= 1 << i;
								mat[1][1] = a[j];
								nxtMask ^= 1 << j;
								mat[2][2] = a[k];
								nxtMask ^= 1 << k;
								mat[3][3] = a[l];
								nxtMask ^= 1 << l;
								rows[0] = cols[0] = a[i];
								rows[1] = cols[1] = a[j];
								rows[2] = cols[2] = a[k];
								rows[3] = cols[3] = a[l];
								if(dfs(state + 1, nxtMask)) {
									return true;
								}
							}
						}
					}
				}
			}
		}
	} else if((state - 1) / (n - 1) == n) {
		//std::cout << "ended " << (state - 1) / (n - 1) << ", " << n << std::endl;
		return true;
	} else {
		int row = (state - 1) / (n - 1);
		int col = (state - 1) % (n - 1);
		if(col >= row) {
			col++;
		}
		for(int i = 0; i < n * n; i++) {
			if((mask & (1 << i)) != 0 && rows[row] + a[i] <= sum && cols[col] + a[i] <= sum) {
				rows[row] += a[i];
				cols[col] += a[i];
				if(dfs(state + 1, mask ^ (1 << i))) {
					mat[row][col] = a[i];
					return true;
				}
				rows[row] -= a[i];
				cols[col] -= a[i];
			}
		}
	}
	return false;
}

int main() {
	std::ios_base::sync_with_stdio(false); std::cin.tie(NULL);
	std::cin >> n;
	int mn = 1e9;
	long long ans = 0;
	for(int i = 0; i < n * n; i++) {
		std::cin >> a[i];
		mn = std::min(mn, a[i]);
		ans += a[i];
	}
	std::cout << ans / n << std::endl;
	for(int i = 0; i < n * n; i++) {
		a[i] -= mn;
		sum += a[i];
		//std::cout << a[i] << (i + 1 == n * n ? '\n' : ' ');
	}
	sum /= n;
	dfs(0, (1 << (n * n)) - 1);
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < n; j++) {
			std::cout << mat[i][j] + mn << (j + 1 == n ? '\n' : ' ');
		}
	}
}

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
const ll INF=ll(1e15);
const double alpha=0.99988;
const double eps=1e-6;
int a[20],r[20],n,m,ans[20][20],fir[20];
void fill(int c[])
{
    for(int i=0;i<n;i++)
        ans[i/m][i%m]=c[i];
}
ll check(int &s)
{
    int s1=0,s2=0;
    ll f=0;
    for(int i=0;i<m;i++)
        s1+=ans[i][i],s2+=ans[i][m-i-1];
    f+=abs(s1-s2);
    for(int i=0;i<m;i++)
    {
        int s3=0,s4=0;
        for(int j=0;j<m;j++)
            s3+=ans[i][j],s4+=ans[j][i];
       f+=abs(s3-s1)+abs(s4-s1)+abs(s3-s2)+abs(s4-s2)+abs(s3-s4);
    }
    s=s1;
    return f;
}
void annealing(unsigned int seed)
{
    srand(seed);
    for(int i=0;i<n;i++)
        r[i]=a[i]=fir[i];
    ll maxi=INF;
    double t=178.0;
    int best;
    while (t>eps && maxi)
    {
        for(int i=0;i<5;i++)
        {
            swap(r[rand()%n],r[rand()%n]);
            fill(r);
            ll cur=check(best);
            if (cur<maxi || (rand()%1001)/1000.0<exp(maxi-cur/t))
            {
                maxi=cur;
                memcpy(a,r,n*sizeof r[0]);
                if (!maxi)
                    break;
            } else memcpy(r,a,n*sizeof a[0]);
        }
        t*=alpha;
    }
    fill(a);
    maxi=check(best);
    if (!maxi)
    {
        cout<<best<<endl;
        for(int i=0;i<m;i++,cout<<endl)
            for(int j=0;j<m;j++)
                cout<<ans[i][j]<<" ";
        exit(0);
    }
}
int main()
{
    cin>>n;
    m=n;
    n*=n;
    for(int i=0;i<n;i++)
        cin>>fir[i];
    annealing(473571609);
    annealing(732801432);
    annealing(1840348433);
    annealing(3298343312u);
    return 0;
}
// xxxx

#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <cstring>

using namespace std;
typedef long long LL;
#define REP(i, n) for(int i=0; i<n; i++)
#define FOR(i, x, y) for(int i=x; i<=y; i++)

int n;
int nums[32];
int a[8][8];
bool u[32];
int s;

bool check()
{
    REP(i, n)
    {
        int cur = 0;
        REP(j, n)
            cur += a[i][j];
        if (s != cur) return false;
    }

    REP(i, n)
    {
        int cur = 0;
        REP(j, n)
            cur += a[j][i];
        if (s != cur) return false;
    }

    int cur = 0;
    REP(i, n)
        cur += a[i][n-1-i];
    if (cur != s)
        return false;
    return true;
}

bool go(int x, int y)
{
    REP(i, n*n)
        if (!u[i])
        {
            u[i] = true;
            a[x][y] = nums[i];

            if (x == n - 1 && y == n - 1)
            {
                if (check())
                {
                    cout << s << endl;
                    REP(i, n)
                    {
                        REP(j, n)
                        {
                            if (j)
                                cout << " ";
                            cout << a[i][j];
                        }
                        cout << endl;
                    }
                    return true;
                }
                u[i] = false;
                continue;
            }

            if (y == n-1)
            {
                int ss = 0;
                REP(j, n)
                    ss += a[x][j];
                if (ss != s)
                {
                    u[i] = false;
                    continue;
                }
                if (go(x + 1, 0))
                    return true;
                else
                {
                    u[i] = false;
                    continue;
                }
            }

            if (go(x, y+1))
                return true;
            u[i] = false;
        }
    return false;
}

int main()
{
    cin >> n;
    REP(i, n*n)
    {
        cin >> nums[i];
        s += nums[i];
    }
    s /= n;
    go(0, 0);
}