#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]};
}


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

    vector <vector <int>> ans(n, vector <int> (n));
    vector <vector <array <int, 2>>> cnt(n + 1, vector <array <int, 2>> (n + 1));
    for (int col1 = 1; col1 <= 10; ++col1) {
        for (int col2 = 1; col2 <= 10; ++col2) {
            if (col1 == col2) continue;
            array <int, 2> col = {col1, col2};
            
            for (int i = 0; i < n; ++i) {
                for (int j = 0; j < n; ++j) {
                    cnt[i + 1][j + 1] = cnt[i][j + 1] + cnt[i + 1][j] - cnt[i][j];

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

                    ans[i][j] = max(ans[i][j], cnt[i + 1][j + 1][0] + cnt[i + 1][j + 1][1]);
                }
            }
        }
    }

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