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

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

    int n;
    cin >> n;
    vector<vector<int>> dp(n, vector<int>(n,0)), tmp(n, vector<int>(n,0));
    vector<vector<char>> mp(n, vector<char>(n,0));
    for(int i = 0;i < n;i++){
        string s;
        cin >> s;
        for(int j = 0;j < n;j++)
            mp[i][j] = s[j];
    }

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

    dp[0][0] = tmp[0][0];
    for(int i = 1;i < n;i++) dp[i][0] = tmp[i][0] + dp[i-1][0];
    for(int j = 1;j < n;j++) dp[0][j] = tmp[0][j] + dp[0][j-1];

    for(int i = 1;i < n;i++)
        for(int j = 1;j < n;j++){
            dp[i][j] = min(dp[i-1][j], dp[i][j-1]);
            if(tmp[i][j] != '0')  dp[i][j] += tmp[i][j];
        }

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

    return 0;
}