#include <iostream>
#include <vector>
#include <algorithm>
#include <array>

using namespace std;

struct TwoMax {
    int opt1 = -1;
    int cnt1 = 0;
    int opt2 = -1;
    int cnt2 = 0;

    void add(int a, int x) {
        if (opt1 == a) {
            cnt1 = x;
        }
        if (opt2 == a) {
            cnt2 = x;
        }

        if (opt1 == a || opt2 == a) {
            if (cnt1 < cnt2) {
                swap(opt1, opt2);
                swap(cnt1, cnt2);
            }
            return;
        }

        if (cnt1 < x) {
            opt2 = opt1;
            cnt2 = cnt1;
            opt1 = a;
            cnt1 = x;
        } else if (cnt2 < x) {
            opt2 = a;
            cnt2 = x;
        }
    }
};

inline void solve() {
    int n;
    cin >> n;
    vector <vector <int>> a(n, vector <int> (n));
    vector <int> all;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            cin >> a[i][j];
            all.push_back(a[i][j]);
        }
    }

    sort(all.begin(), all.end());
    all.resize(unique(all.begin(), all.end()) - all.begin());
    int cnt_diff = all.size();

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            a[i][j] = lower_bound(all.begin(), all.end(), a[i][j]) - all.begin();
        }
    }

    vector <vector <int>> ans(n, vector <int> (n));

    for (int i = 0; i < n; ++i) {
        vector <array <int, 2>> cnt(cnt_diff, {0, 0});
        array <TwoMax, 2> flex;

        for (int j = 0; j < n; ++j) {
            for (int k = 0; k <= i; ++k) {
                flex[(j + k) & 1].add(a[k][j], ++cnt[a[k][j]][(j + k) & 1]);
            }

            if (flex[0].opt1 != flex[1].opt1) ans[i][j] = max(ans[i][j], flex[0].cnt1 + flex[1].cnt1);
            if (flex[0].opt2 != flex[1].opt1) ans[i][j] = max(ans[i][j], flex[0].cnt2 + flex[1].cnt1);
            if (flex[0].opt1 != flex[1].opt2) ans[i][j] = max(ans[i][j], flex[0].cnt1 + flex[1].cnt2);
        }
    }

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            cout << (i + 1) * (j + 1) - ans[i][j] << " ";
        }
        cout << "\n";
    }
};

signed main() {
    if (1) {
        ios_base::sync_with_stdio(false);
        cin.tie(nullptr);
    }

    solve();
}