1 #include<bits/stdc++.h>
2 #define B bitset<105>
3 using namespace std;
4 typedef long long ll ;
5 const int N = 105 ;
6 B G[N] ;
7 ll a[N] ;
8 typedef struct Node {
9 B mask;
10 ll val;
11 Node() {}
12 Node( B b,ll v):mask(b),val(v){}
13 bool operator < (const Node & rhs )const {
14 return val > rhs.val ;
15 }
16 }Node ;
17 int main()
18 {
19 int n,k;
20 while(~scanf("%d%d",&n,&k)){
21 for(int i=1;i<=n;i++){
22 scanf("%lld",&a[i]);
23 }
24 for(int i=1;i<=n;i++){
25 G[i].reset();
26 for(int j=1,x;j<=n;j++){
27 scanf("%1d",&x);
28 G[i][j] = x ;
29 }
30 }
31 priority_queue < Node > Q ;
32 B tmp ;
33 tmp.reset();
34 Q.push( Node(tmp,0) );
35 while( !Q.empty() ){
36 Node cur = Q.top() ;
37 Q.pop();
38 if( !(--k) ){
39 return 0*printf("%lld\n",cur.val );
40 }
41 int pos = 1 ;
42 for(int i=1;i<=n;i++){
43 if(cur.mask[i]){
44 pos = i+1 ;
45 }
46 }
47 for(int i=pos;i<=n;i++){
48 if( ( G[i] & cur.mask ) == cur.mask ){
49 cur.mask[i] = 1 ;
50 Q.push( Node(cur.mask,cur.val +a[i]) );
51 cur.mask[i] = 0 ;
52 }
53 }
54 }
55 puts("-1");
56 }
57 return 0;
58 }