#include <bits/stdc++.h>
#define ford(i, n) for(int i = (int)(n) - 1; i >= 0; i--)
#define forn(i, n) for(int i = 0; i < (int)(n); i++)
#define for1(i, n) for(int i = 1; i <= (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define clr(x) memset(x, 0, sizeof(x))
#define pb push_back
#define mp make_pair
#define prev asdfsdf
#define fi first
#define se second
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long long i64;
int nxt() {
int x;
scanf("%d", &x);
return x;
}
ull q[35][1 << 16];
ull cur[35][1 << 16];
#define set asfkjskdfj
void set(int v, int p) {
q[v][p >> 6] |= 1ull << (p & 63);
}
struct node {
node *ch[2];
int mul;
node() {
clr(ch);
}
};
node* root;
int m;
void add(ull mask, long long x) {
node * cur = root;
for (int i = m - 1; i >= 0; --i) {
int bit = (mask >> i) & 1ull;
if (!cur->ch[bit]) {
cur->ch[bit] = new node();
}
cur = cur->ch[bit];
}
cur->mul = x;
}
long long val[100];
long long ans = 0;
int SI;
int r;
void go(node *root, int d, long long v) {
if (!root->ch[0] && !root->ch[1]) {
ans += v * root->mul;
return;
}
if (root->ch[0]) {
memcpy(cur[d + 1], cur[d], SI);
go(root->ch[0], d + 1, v);
}
if (root->ch[1]) {
int cnt = 0;
int i = m - 1 - d;
for (int j = 0; j < r; ++j) {
cur[d + 1][j] = q[i][j] | cur[d][j];
cnt += __builtin_popcountll(cur[d + 1][j] ^ cur[d][j]);
}
go(root->ch[1], d + 1, v + val[i] * cnt);
}
}
void solve() {
int n = nxt();
m = nxt();
string msk[m];
for (int i = 0; i < m; ++i) {
cin >> msk[i] >> val[i];
}
int o[n];
iota(o, o + n, 0);
random_shuffle(o, o + n);
for (int i = 0; i < m; ++i) {
string q(n, '0');
for (int j = 0; j < n; ++j) {
q[o[j]] = msk[i][j];
}
msk[i] = q;
}
int k = min(n, 16);
int v = n - k;
map <ull, int> cnt;
for (int t = 0; t < m; ++t) {
for (int i = 0; i < (1 << k); ++i) {
int ok = 1;
for (int T = 0; T < k; ++T) {
int bit = (i >> T) & 1;
if (msk[t][T + v] != '?' && msk[t][T + v] != '0' + bit) {
ok = 0;
break;
}
}
if (ok) set(t, i);
}
}
for (int i = 0; i < (1 << v); ++i) {
ull mask = (1ull << m) - 1;
for (int j = 0; j < v; ++j) {
int bit = (i >> j) & 1;
for (int k = 0; k < m; ++k) {
if (msk[k][j] != '?' && msk[k][j] != '0' + bit) {
mask &= ~(1ull << k);
}
}
}
cnt[mask] += 1;
}
r = ((1 << k) + 63) / 64;
SI = r * sizeof(ull);
root = new node();
for (auto p : cnt) {
ull msk = p.fi;
add(msk, p.se);
}
go(root, 0, 0);
cout << ans << "\n";
}
int main()
{
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
int t = 1;
while (t--) solve();
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CgojZGVmaW5lIGZvcmQoaSwgbikgZm9yKGludCBpID0gKGludCkobikgLSAxOyBpID49IDA7IGktLSkKI2RlZmluZSBmb3JuKGksIG4pIGZvcihpbnQgaSA9IDA7IGkgPCAoaW50KShuKTsgaSsrKQojZGVmaW5lIGZvcjEoaSwgbikgZm9yKGludCBpID0gMTsgaSA8PSAoaW50KShuKTsgaSsrKQojZGVmaW5lIGFsbCh4KSAoeCkuYmVnaW4oKSwgKHgpLmVuZCgpCiNkZWZpbmUgY2xyKHgpIG1lbXNldCh4LCAwLCBzaXplb2YoeCkpCiNkZWZpbmUgcGIgcHVzaF9iYWNrCiNkZWZpbmUgbXAgbWFrZV9wYWlyCiNkZWZpbmUgcHJldiBhc2Rmc2RmCiNkZWZpbmUgZmkgZmlyc3QKI2RlZmluZSBzZSBzZWNvbmQKCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnR5cGVkZWYgbG9uZyBkb3VibGUgbGQ7CnR5cGVkZWYgbG9uZyBsb25nIGxsOwp0eXBlZGVmIHVuc2lnbmVk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