#include <bits/stdc++.h>
using namespace std;
using ll = long long;

void solve()
{
    ll n,q;
    cin >> n;
    vector<string> a(n);
    for(int i = 0; i < n; i++)cin >> a[i];

    vector<vector<ll>> t(n,vector<ll>(n));
    for(int i =0 ; i < n; i++)for(int j =0 ; j < n; j++)cin>>t[i][j];

    vector<vector<ll>> d(n,vector<ll>(n,1e18));
    bool check=false;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            if(a[i][j] == '1')check = true;
        }
    }
    vector<vector<ll>> dp(n,vector<ll>(n,1e18));


    if(!check){
        dp[0][0] = t[0][0];
        for(int i =0; i < n; i++){
            for(int j = 0; j < n; j++){
                if(i == 0 && j==0)continue;
                if(j >0)dp[i][j] = t[i][j] + min(dp[i][j], dp[i][j-1]);
                if(i >0)dp[i][j] = t[i][j] + min(dp[i][j], dp[i-1][j]);

            }
        }

        cout << dp[n-1][n-1] << "\n";


    }





}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    t = 1;
    //cin >> t;
    while(t--){
        solve();
    }
}