#include //ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair pii; typedef pair ppii; typedef pair pipi; typedef pair pll; typedef pair ppll; typedef pair plpl; typedef tuple tl; ll mod=1000000007; ll mod2=998244353; ll mod3=1000003; ll mod4=998244853; ll inf=1000000000; double pi=2*acos(0); #define rep(i,m,n) for(ll i=m;i=m;i--) int dh[4]={1,-1,0,0}; int dw[4]={0,0,1,-1}; int ddh[8]={-1,-1,-1,0,0,1,1,1}; int ddw[8]={-1,0,1,-1,1,-1,0,1}; ll lmax(ll a,ll b){ if(a0){ if(k&1)ret*=now; now*=now; k/=2; } return ret; } ll beki(ll n,ll k,ll md){ ll ret=1; ll now=n; while(k>0){ if(k%2==1){ ret*=now; ret%=md; } now*=now; now%=md; k/=2; } return ret; } ll gyaku(ll n,ll md){ return beki(n,md-2,md); } int main(){ ios::sync_with_stdio(false);cin.tie(0); int n;cin>>n; vector v[n]; ll su[n]; fill(su,su+n,0); rep(i,0,n){ rep(j,0,n){ int x;cin>>x; if(x==1){ v[j].push_back(i); su[i]++; } } } ll a[n];rep(i,0,n)cin>>a[i]; ll ans=inf; rep(i,0,1< q; ll u=i; rep(j,0,n){ if(u%2==1){ q.push(j); used[j]=true; sum+=a[j]; } u/=2; } rep(j,0,n){ if(used[j]==false&&su[j]==0){ used[j]=true; q.push(j); } } ll cnt[n]; fill(cnt,cnt+n,0); while(q.size()>0){ ll now=q.front();q.pop(); rep(j,0,v[now].size()){ ll ne=v[now][j]; if(used[ne])continue; cnt[ne]++; if(cnt[ne]==su[ne]){ q.push(ne); used[ne]=true; } } } int r=0; rep(j,0,n){ if(used[j]==false){ r=1; break; } } if(r==0)ans=min(ans,sum); } cout<