#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll inf = 2000000000;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    ll n;
    cin >> n;
    string s;
    vector<vector<ll>>pust(n, vector<ll>(n));
    vector<vector<ll>>toks(n, vector<ll>(n));
    queue<pair<ll, ll>>q;
    vector<vector<bool>>poseceni(n, vector<bool>(n, false));
    vector<vector<ll>>uda(n, vector<ll>(n));
    ll mini = inf;
    for(ll i = 0; i < n; i++){
        cin >> s;
        for(ll j = 0; j < n; j++){
            if(s[j] == '1'){
                pust[i][j] = 1;
            }
            else{
                q.push(make_pair(i, j));
                poseceni[i][j] = true;
                uda[i][j] = 0;
                pust[i][j] = 0;
                if(i + j < mini){
                    mini = i + j;
                }
            }
        }
    }
    for(ll i = 0; i < n; i++){
        for(ll j = 0; j < n; j++){
            cin >> toks[i][j];
        }
    }
    if(mini == inf){
        cout << 0 << endl;
    }
    else{
        vector<vector<ll>>dp(n, vector<ll>(n, 0));
        dp[n - 1][n - 1] = toks[n - 1][n - 1];
        for(ll i = n - 2; i >= 0; i--){
            dp[n - 1][i] = dp[n - 1][i + 1] + toks[n - 1][i];
        }
        for(ll i = n - 2; i >= 0; i--){
            dp[i][n - 1] = dp[i + 1][n - 1] + toks[i][n - 1];
        }
        for(ll i = n - 2; i >= 0; i--){
            for(ll j = n - 2; j >= 0; j--){
                dp[i][j] = min(dp[i + 1][j], dp[i][j + 1]) + toks[i][j];
            }
        }
        ll res = dp[0][0];
        while(!q.empty()){
            pair<ll, ll> kord = q.front();
            q.pop();
            ll i = kord.first, j = kord.second;
            if(i > 0 and !poseceni[i - 1][j]){
                poseceni[i - 1][j] = true;
                q.push(make_pair(i - 1, j));
                uda[i - 1][j] = uda[i][j] + 1;
            }
            if(j > 0 and !poseceni[i][j - 1]){
                poseceni[i][j + 1] = true;
                q.push(make_pair(i, j - 1));
                uda[i][j - 1] = uda[i][j] + 1;
            }
            if(i < n - 1 and !poseceni[i + 1][j]){
                poseceni[i + 1][j] = true;
                q.push(make_pair(i + 1, j));
                uda[i + 1][j] = uda[i][j] + 1;
            }
            if(j < n - 1 and !poseceni[i][j + 1]){
                poseceni[i][j + 1] = true;
                q.push(make_pair(i, j + 1));
                uda[i][j + 1] = uda[i][j] + 1;
            }
        }
        for(ll i = 0; i < n; i++){
            for(ll j = 0; j < n; j++){
                if(i + j <= uda[i][j]){
                    res = min(res, dp[i][j]);
                }
            }
        }
        cout << res << endl;

    }
}