#include <bits/stdc++.h>
#define int long long
using namespace std;
vector<vector<long long>>mapa;
queue<vector<long long>>q;
void bfs(vector<string>&mapa2){
    while(!q.empty()){
        auto p=q.front();
        q.pop();
        int t=p[2];
        int x=p[1];
        int y=p[0];
        if(y-1>=0 and mapa2[y-1][x]=='1'){
            mapa2[y-1][x]='0';
            mapa[y-1][x]=t+1;
            q.push({x,y-1,t+1});
        }
        if(y+1<mapa.size() and mapa2[y+1][x]=='1'){
            mapa2[y+1][x]='0';
            mapa[y+1][x]=t+1;
            q.push({x,y+1,t+1});
        }
        if(x-1>=0 and mapa2[y][x-1]=='1'){
            mapa2[y][x-1]='0';
            mapa[y][x-1]=t+1;
            q.push({x-1,y,t+1});
        }
        if(x+1<mapa2.size() and mapa2[y][x+1]=='1'){
            mapa2[y][x+1]='0';
            mapa[y][x+1]=t+1;
            q.push({x+1,y,t+1});
        }
    }
}

signed main()
{
    int n;
    cin>>n;
    vector<string>mapa1(n),mapa2(n);
    mapa.assign(n,vector<int>(n));
    vector<vector<int>>val(n,vector<int>(n));
    vector<vector<pair<long long,long long>>>dp(n+2,vector<pair<long long,long long>>(n+2,{2000000000000000,2000000000000000})),dp1(n+2,vector<pair<long long,long long>>(n+2,{2000000000000000,2000000000000000}));
    for(int i=0;i<n;i++)cin>>mapa1[i],mapa2[i]=mapa1[i];
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(mapa1[i][j]=='0'){
                q.push({i,j,0});
            }
        }
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            cin>>val[i][j];
        }
    }
    bfs(mapa2);
    dp[1][1]={0,0};
    dp1[1][1]={0,0};
    if(mapa1[0][0]=='0')dp[1][1].first=val[0][0],dp1[1][1].first=val[0][0];
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){

            bool stize = mapa[i-1][j-1]>dp[i-1][j].second;
            int tprethodno=dp[i-1][j].second;
            int uzimamo=dp[i-1][j].first+val[i-1][j-1];
            int neuzimamo=dp[i-1][j].first;
            if(!stize and (uzimamo<dp[i][j].first or(uzimamo==dp[i][j].first and tprethodno+1<dp[i][j].second)))dp[i][j].first=uzimamo,dp[i][j].second=tprethodno+1;//ne stizemo
            else if(stize){
                if(uzimamo<dp[i][j].first or(uzimamo==dp[i][j].first and mapa[i-1][j-1]<dp1[i][j].second)){ //stizemo i uzimamo
                    dp[i][j].first=uzimamo;
                    dp[i][j].second=mapa[i-1][j-1];
                }
                if(neuzimamo<dp1[i][j].first or (neuzimamo==dp1[i][j].first and tprethodno+1<dp1[i][j].second)){//stizemo i ne uzimamo
                    dp1[i][j].first=neuzimamo;
                    dp1[i][j].second=tprethodno+1;
                }
            }
            stize=mapa[i-1][j-1]>dp1[i-1][j].second;
            tprethodno=dp1[i-1][j].second;
            uzimamo=dp1[i-1][j].first+val[i-1][j-1];
            neuzimamo=dp1[i-1][j].first;

            if(!stize and (uzimamo<dp[i][j].first or(uzimamo==dp[i][j].first and tprethodno+1<dp[i][j].second)))dp[i][j].first=uzimamo,dp[i][j].second=tprethodno+1;//ne stizemo
            else if(stize){
                if(uzimamo<dp[i][j].first or(uzimamo==dp[i][j].first and mapa[i-1][j-1]<dp1[i][j].second)){//stizemo i uzimamo
                    dp[i][j].first=uzimamo;
                    dp[i][j].second=mapa[i-1][j-1];
                }
                if(neuzimamo<dp1[i][j].first or (neuzimamo==dp1[i][j].first and tprethodno+1<dp1[i][j].second)){//stizemo i ne uzimamo
                    dp1[i][j].first=neuzimamo;
                    dp1[i][j].second=tprethodno+1;
                }
            }


            //j-1

            stize = mapa[i-1][j-1]>dp[i][j-1].second;
            tprethodno=dp[i][j-1].second;
            uzimamo=dp[i][j-1].first+val[i-1][j-1];
            neuzimamo=dp[i][j-1].first;
            if(!stize and (uzimamo<dp[i][j].first or(uzimamo==dp[i][j].first and tprethodno+1<dp[i][j].second)))dp[i][j].first=uzimamo,dp[i][j].second=tprethodno+1;//ne stizemo
            else if(stize){
                if(uzimamo<dp[i][j].first or(uzimamo==dp[i][j].first and mapa[i-1][j-1]<dp1[i][j].second)){ //stizemo i uzimamo
                    dp[i][j].first=uzimamo;
                    dp[i][j].second=mapa[i-1][j-1];
                }
                if(neuzimamo<dp1[i][j].first or (neuzimamo==dp1[i][j].first and tprethodno+1<dp1[i][j].second)){//stizemo i ne uzimamo
                    dp1[i][j].first=neuzimamo;
                    dp1[i][j].second=tprethodno+1;
                }
            }

            stize=mapa[i-1][j-1]>dp1[i][j-1].second;
            tprethodno=dp1[i][j-1].second;
            uzimamo=dp1[i][j-1].first+val[i-1][j-1];
            neuzimamo=dp1[i][j-1].first;
            if(!stize and (uzimamo<dp[i][j].first or(uzimamo==dp[i][j].first and tprethodno+1<dp[i][j].second)))dp[i][j].first=uzimamo,dp[i][j].second=tprethodno+1;//ne stizemo
            else if(stize){
                if(uzimamo<dp[i][j].first or(uzimamo==dp[i][j].first and mapa[i-1][j-1]<dp1[i][j].second)){//stizemo i uzimamo
                    dp[i][j].first=uzimamo;
                    dp[i][j].second=mapa[i-1][j-1];
                }
                if(neuzimamo<dp1[i][j].first or (neuzimamo==dp1[i][j].first and tprethodno+1<dp1[i][j].second)){//stizemo i ne uzimamo
                    dp1[i][j].first=neuzimamo;
                    dp1[i][j].second=tprethodno+1;
                }
            }
           // cout<<dp[i][j].first<<" "<<dp[i][j].second<<" "<<dp1[i][j].first<<" "<<dp1[i][j].second<<endl;
        }

    }
    cout<<min(dp[n][n].first,dp1[n][n].first)<<endl;
    return 0;
}