#include <iostream>
#include <bits/stdc++.h>

using namespace std;

#define endl "\n"
#define ll long long

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(NULL);

    int n;
    cin >> n;
    char t;
    int mat[n][n];
    int pos[n][n];
    int dp[n][n] = {0};
    vector<int> v;

    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            cin >> t;
            pos[i][j] = t;
            if(t=='0'){
                v.push_back((i-1)*1000+(j-1));
            }
        }
    }

    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            cin >> mat[i][j];
        }
    }

    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            if(pos[i][j]=='1'){
                bool tmp = false;
                for(auto x:v){
                    if(abs(i-(x/1000+1))+abs(j-(x%1000+1)) < i+j){
                        tmp = true;
                        break;
                    }
                }
                if(!tmp){
                    mat[i][j] = 0;
                }
            }
        }
    }

    /*
    cout << endl;
    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            cout << mat[i][j] << " ";;
        }
        cout << endl;
    }
    cout << endl;
    */

    dp[0][0] = mat[0][0];

    for(int j=1; j<n; j++){
        dp[j][0] = dp[j-1][0] + mat[j][0];
        dp[0][j] = dp[0][j-1] + mat[0][j];
    }

    for(int i=1; i<n; i++){
        for(int j=1; j<n; j++){
            dp[j][i] = min(dp[j-1][i], dp[j][i-1]) + mat[j][i];
            dp[i][j] = min(dp[i][j-1], dp[i-1][j]) + mat[i][j];
        }
    }

    /*
    cout << endl;
    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            cout << dp[i][j] << " ";;
        }
        cout << endl;
    }
    */

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

}