#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5 + 10;
const int MAX1 = 1e3 + 10;
const int MAX2 = 1e2 + 10;
const int INF = 1e9 + 10;
const long long mod = 1e18;
inline int bpow(int t, int n) {
  int ans = 1;
  while (n > 0) {
    if (n & 1) ans = (ans * t) % mod;
    t = (t * t) % mod, n >>= 1;
  }
  return ans;
}
int a[9];
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n * n; i++) cin >> a[i];
  if (n == 1)
    cout << a[0] << "\n" << a[0];
  else if (n == 2) {
    cout << 2 * a[0] << "\n";
    cout << a[0] << " " << a[0] << endl;
    cout << a[0] << " " << a[0] << endl;
  } else if (n == 3) {
    vector<int> vec;
    for (int i = 0; i < n * n; i++) vec.push_back(i);
    do {
      int t[] = {
          a[vec[0]] + a[vec[1]] + a[vec[2]], a[vec[3]] + a[vec[4]] + a[vec[5]],
          a[vec[6]] + a[vec[7]] + a[vec[8]], a[vec[0]] + a[vec[3]] + a[vec[6]],
          a[vec[1]] + a[vec[4]] + a[vec[7]], a[vec[2]] + a[vec[5]] + a[vec[8]],
          a[vec[0]] + a[vec[4]] + a[vec[8]], a[vec[2]] + a[vec[4]] + a[vec[6]]};
      bool ok = true;
      for (int i = 1; i < 8 && ok; i++)
        if (t[0] != t[i]) ok = false;
      if (ok) {
        cout << t[0];
        for (int i = 0; i < n * n; i++) {
          if (i % n == 0) cout << endl;
          cout << a[vec[i]] << " ";
        }
        return 0;
      }
    } while (next_permutation(vec.begin(), vec.end()));
  } else {
    int sum = 0;
    for (int i = 0; i < n * n; i++) sum += a[i];
    sum /= n;
    int h[16];
    bool mark[100] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    for (h[0] = 0; h[0] < n * n; h[0]++) {
      mark[h[0]] = true;
      for (h[5] = 0; h[5] < n * n; h[5]++) {
        if (!mark[h[5]]) {
          mark[h[5]] = true;
          for (h[10] = 0; h[10] < n * n; h[10]++) {
            if (!mark[h[10]]) {
              mark[h[10]] = true;
              int wh15 = sum - a[h[10]] - a[h[5]] - a[h[0]];
              for (h[15] = 0; h[15] < n * n; h[15]++) {
                if (a[h[15]] == wh15 && !mark[h[15]]) {
                  mark[h[15]] = true;
                  for (h[12] = 0; h[12] < n * n; h[12]++) {
                    if (!mark[h[12]]) {
                      mark[h[12]] = true;
                      for (h[9] = 0; h[9] < n * n; h[9]++) {
                        if (!mark[h[9]]) {
                          mark[h[9]] = true;
                          for (h[6] = 0; h[6] < n * n; h[6]++) {
                            if (!mark[h[6]]) {
                              mark[h[6]] = true;
                              int wh3 = sum - a[h[6]] - a[h[9]] - a[h[12]];
                              for (h[3] = 0; h[3] < n * n; h[3]++) {
                                if (a[h[3]] == wh3 && !mark[h[3]]) {
                                  mark[h[3]] = true;
                                  for (h[1] = 0; h[1] < n * n; h[1]++) {
                                    if (!mark[h[1]]) {
                                      mark[h[1]] = true;
                                      int wh13 =
                                          sum - a[h[1]] - a[h[5]] - a[h[9]];
                                      for (h[13] = 0; h[13] < n * n; h[13]++) {
                                        if (a[h[13]] == wh13 && !mark[h[13]]) {
                                          mark[h[13]] = true;
                                          int wh2 =
                                              sum - a[h[0]] - a[h[1]] - a[h[3]];
                                          for (h[2] = 0; h[2] < n * n; h[2]++) {
                                            if (a[h[2]] == wh2 && !mark[h[2]]) {
                                              mark[h[2]] = true;
                                              int wh14 = sum - a[h[12]] -
                                                         a[h[13]] - a[h[15]];
                                              for (h[14] = 0; h[14] < n * n;
                                                   h[14]++) {
                                                if (a[h[14]] == wh14 &&
                                                    !mark[h[14]]) {
                                                  mark[h[14]] = true;
                                                  for (h[8] = 0; h[8] < n * n;
                                                       h[8]++) {
                                                    if (!mark[h[8]]) {
                                                      mark[h[8]] = true;
                                                      int wh11 = sum - a[h[8]] -
                                                                 a[h[9]] -
                                                                 a[h[10]];
                                                      for (h[11] = 0;
                                                           h[11] < n * n;
                                                           h[11]++) {
                                                        if (a[h[11]] == wh11 &&
                                                            !mark[h[11]]) {
                                                          mark[h[11]] = true;
                                                          int wh7 = sum -
                                                                    a[h[11]] -
                                                                    a[h[15]] -
                                                                    a[h[3]];
                                                          for (h[7] = 0;
                                                               h[7] < n * n;
                                                               h[7]++) {
                                                            if (a[h[7]] ==
                                                                    wh7 &&
                                                                !mark[h[7]]) {
                                                              mark[h[7]] = true;
                                                              int wh4 =
                                                                  sum -
                                                                  a[h[0]] -
                                                                  a[h[8]] -
                                                                  a[h[12]];
                                                              for (h[4] = 0;
                                                                   h[4] < n * n;
                                                                   h[4]++) {
                                                                if (a[h[4]] ==
                                                                        wh4 &&
                                                                    !mark
                                                                        [h[4]]) {
                                                                  mark[h[4]] =
                                                                      true;
                                                                  cout << sum;
                                                                  for (int i =
                                                                           0;
                                                                       i <
                                                                       n * n;
                                                                       i++) {
                                                                    if (i % n ==
                                                                        0)
                                                                      cout
                                                                          << endl;
                                                                    cout
                                                                        << a[h[i]]
                                                                        << " ";
                                                                  }
                                                                  return 0;
                                                                }
                                                              }
                                                              mark[h[7]] =
                                                                  false;
                                                            }
                                                          }
                                                          mark[h[11]] = false;
                                                        }
                                                      }
                                                      mark[h[8]] = false;
                                                    }
                                                  }
                                                  mark[h[14]] = false;
                                                }
                                              }
                                              mark[h[2]] = false;
                                            }
                                          }
                                          mark[h[13]] = false;
                                          break;
                                        }
                                      }
                                      mark[h[1]] = false;
                                    }
                                  }
                                  mark[h[3]] = false;
                                  break;
                                }
                              }
                              mark[h[6]] = false;
                            }
                          }
                          mark[h[9]] = false;
                        }
                      }
                      mark[h[12]] = false;
                    }
                  }
                  mark[h[15]] = false;
                  break;
                }
              }
              mark[h[10]] = false;
            }
          }
          mark[h[5]] = false;
        }
      }
      mark[h[0]] = false;
    }
  }
}
