#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;cin>>n;

    bool case2 = true;

    vector<vector<bool>> vec(n, vector<bool>(n));
    vector<vector<int>> tox(n, vector<int>(n));

    for (int i = 0; i < n; ++i){
        for (int j = 0; j < n; ++j){
            char c; cin>>c;
            vec[i][j] = c == '1';

            if (vec[i][j]){
                case2 = false;
            }
        }
    }

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

    if (case2){
        vector<vector<int>> dp(n, vector<int>(n, INT_MAX));
        dp[0][0] = tox[0][0];

        for (int i = 0; i < n; ++i){
            for (int j = 0; j < n; ++j){
                if (i == 0 && j == 0)
                    continue;

                dp[i][j] = min(i > 0 ? (dp[i - 1][j] + tox[i][j]) : INT_MAX, j > 0 ? (dp[i][j - 1] + tox[i][j]) : INT_MAX);
                //cout<<i<<' '<<j<<' '<<dp[i][j]<<endl;
            }
        }

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

    /*if (n <= 200){
        vector<vector<vector<int>>> dp(n, vector<vector<int>>(n, vector<int>(n, INT_MAX)));



        for (int i = 0; i < n; ++i){
            for (int j = 0; j < n; ++j){
                for (int k = 0; k < n; ++k){
                    int moves = i + j + k;

                }
                int add = )
            }
        }
    }*/

    return 0;
}