#include<bits/stdc++.h>#define ll long longusingnamespace std;template<typename T>voidread(T &x){
x =0;int f =1;char c =getchar();while(!isdigit(c)){if(c =='-') f =-1; c =getchar();}while(isdigit(c)) x = x *10+ c -'0', c =getchar(); x *= f;}constint N =1<<20;int cnt[N], val[N], s[N][2];intmain(){int n, q;read(n),read(q);int m =(1<< n)-1, t = n /3;for(int i =1; i <= m; i++) cnt[i]= cnt[i -(i &-i)]+1;for(int i =0; i <= m; i++){char c =getchar();while(!isdigit(c)) c =getchar();
s[i][0]= s[i][1]= val[i]= c -'0';}for(int j =0; j < n; j++)for(int i =0; i <= m; i++)if((i >> j)&1) s[i][0]+= s[i ^(1<< j)][0], s[i ^(1<< j)][1]+= s[i][1];while(q--){int s0 =0, s1 =0, s2 =0, ans =0;char st[30];scanf(" %s", st);for(int i =0, j = n -1; i < n; i++, j--)if(st[i]=='0') s0 |=(1<< j);elseif(st[i]=='1') s1 |=(1<< j);else s2 |=(1<< j);if(cnt[s0]<= t){for(int ts = s0;; ts = s0 &(ts -1)){(cnt[ts]&1)? ans -= s[s1 | ts][1]: ans += s[s1 | ts][1];if(!ts)break;}}elseif(cnt[s1]<= t)for(int ts = s1;; ts = s1 &(ts -1)){(cnt[ts ^ s1]&1)? ans -= s[s2 | ts][0]: ans += s[s2 | ts][0];if(!ts)break;}elsefor(int ts = s2;; ts = s2 &(ts -1)){
ans += val[s1 | ts];if(!ts)break;}
cout << ans <<'\n';}return0;}