#include <bits/stdc++.h>
using namespace std;
int n, matt[1000][1000], brP, suma;
string s;

int main(){
    cin >> n;



    for(int i = 1; i <= n; i++){
        cin >> s;
        for(int j = 0; j < n; j++)
            if(s[j] == 0)
                brP++;
    }

    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
            cin >> matt[i][j];

    int i = 1;
    int j = 1;

    while(i < n+n && j < n+n){
        suma += matt[i][j];
        matt[i][j] = 0;

        if(matt[i][j+1] < matt[i+1][j] && matt[i][j+1]){
            j+=1;
        }

        else{
            i+=1;
        }

        if(i == n && j == n){
            suma+=matt[i][j];
            break;
        }
    }

    cout <<suma <<'\n';

    return 0;
}