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

using namespace std;
#define ll long long

array <int, 2> operator+(array <int, 2> a, array <int, 2> b) {
    return {a[0] + b[0], a[1] + b[1]};
}

array <int, 2> operator-(array <int, 2> a, array <int, 2> b) {
    return {a[0] - b[0], a[1] - b[1]};
}

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());

    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 <array <TwoMax, 2>>> flex(n, vector <array <TwoMax, 2>> (n));
    vector <vector <array <int, 2>>> cnt(n + 1, vector <array <int, 2>> (n + 1));
    for (int col = 0; col < all.size(); ++col) {
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                cnt[i + 1][j + 1] = cnt[i + 1][j] + cnt[i][j + 1] - cnt[i][j];

                if (a[i][j] == col) cnt[i + 1][j + 1][(i + j) & 1]++;

                flex[i][j][0].add(col, cnt[i + 1][j + 1][0]);
                flex[i][j][1].add(col, cnt[i + 1][j + 1][1]);
            }
        }
    }

    vector <vector <int>> ans(n, vector <int> (n));
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            if (flex[i][j][0].opt1 != flex[i][j][1].opt1) ans[i][j] = max(ans[i][j], flex[i][j][0].cnt1 + flex[i][j][1].cnt1);
            ans[i][j] = max(ans[i][j], flex[i][j][0].cnt1 + flex[i][j][1].cnt2);
            ans[i][j] = max(ans[i][j], flex[i][j][0].cnt2 + flex[i][j][1].cnt1);
        }
    }
    

    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();
}