#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define int ll
void dbg_out() { cerr << endl; }
template<typename H, typename... T>
void dbg_out(H h, T... t) { cerr << ' ' << h; dbg_out(t...); }
#define dbg(...) { cerr << #__VA_ARGS__ << ':'; dbg_out(__VA_ARGS__); }
bool rec(string s, int&i, bool token){
if(token){
if(s[i] == '0'){
i++;
return 0;
}
if(s[i] == '1'){
i++;
return 1;
}
if(s[i] == '!'){
i++;
return 1^rec(s,i,token);
}
if(s[i] == '('){
i++;
int u = rec(s,i,0);
i++;
return u;
}
assert(0);
}else{
int ant = 0;
int op = 0;
//0 = or, 1 = xor, 2 = &, 3 = equal
while(true){
bool at = rec(s,i,1);
if(op == 0){
ant = ant|at;
}else if(op == 1){
ant = ant^at;
}else if(op == 2){
ant = ant&at;
}else{
ant = ant == at;
}
int sz = s.size();
if(i == sz) break;
if(s[i] == '|'){
op = 0;
}else if(s[i] == '&'){
op = 2;
}else if(s[i] == '^'){
op = 1;
}else if(s[i] == '='){
op = 3;
}else{
break;
}
i++;
}
return ant;
}
assert(0);
return false;
}
string transform(string s, int x, int y){
int sz = s.size();
for(int i = 0; i < sz; i++){
if(s[i] == 'x'){
s[i] = char('0'+x);
}
if(s[i] == 'y'){
s[i] = char('0'+y);
}
}
return s;
}
array<int,4> truetable(string s){
array<int,4> retorno;
for(int x = 0; x < 2; x++){
for(int y = 0; y < 2; y++){
string t = transform(s,x,y);
int po = 0;
bool ret = rec(t,po,0);
retorno[x*2 + y] = ret;
}
}
return retorno;
}
int mapeia[11][11][2][2];
int tempo[11][11][2][2];
using bit = bitset<1002>;
void solve() {
int n,k;
cin >> n >> k;
for(int i = 0; i < k; i++){
for(int j = 0; j < k; j++){
for(int w = 0; w < 2; w++){
for(int z = 0; z < 2; z++){
mapeia[i][j][w][z] = -1;
}
}
}
}
for(int i = 0; i < n; i++){
int A,B;
cin >> A >> B;
A--;
B--;
string s;
cin >> s;
array<int,4> at = truetable(s);
int retval;
cin >> retval;
for(int x = 0; x < 2; x++){
for(int y = 0; y < 2; y++){
if(mapeia[A][B][x][y] != -1) continue;
int id = x*2+y;
if(at[id]){
mapeia[A][B][x][y] = retval;
tempo[A][B][x][y] = i;
}
}
}
}
int base;
cin >> base;
for(int i = 0; i < k; i++){
for(int j = 0; j < k; j++){
for(int w = 0; w < 2; w++){
for(int z = 0; z < 2; z++){
if(mapeia[i][j][w][z] == -1){
mapeia[i][j][w][z] = base;
tempo[i][j][w][z] = n;
}
}
}
}
}
vector<bit> g((1<<k));
for(int mask1 = 0; mask1 < (1<<k); mask1++){
for(int mask2 = 0; mask2 < (1<<k); mask2++){
int val = base;
int tem = n;
for(int i = 0; i < k; i++){
for(int j = 0; j < k; j++){
int x = (mask1>>i)&1;
int y = (mask2>>j)&1;
if(tempo[i][j][x][y] < tem){
tem = tempo[i][j][x][y];
val = mapeia[i][j][x][y];
}
}
}
g[mask1][mask2] = val;
}
}
int refle = 0;
for(int mask = 0; mask < (1<<k); mask++){
refle += g[mask][mask];
}
int sime = 0;
for(int mask1 = 0; mask1 < (1<<k); mask1++){
for(int mask2 = 0; mask2 < (1<<k); mask2++){
if(g[mask1][mask2] == 1){
sime += g[mask2][mask1];
}
}
}
int trans = 0;
for(int mask1 = 0; mask1 < (1<<k); mask1++){
bit neg = g[mask1];
for(int mask2 = 0; mask2 < (1<<k); mask2++){
if(neg[mask2]) neg[mask2] = 0;
else neg[mask2] = 1;
}
for(int mask2 = 0; mask2 < (1<<k); mask2++){
if(g[mask1][mask2]){
bit at = neg&g[mask2];
trans += at.count();
}
}
}
cout << refle << ' ' << sime << ' ' << trans <<'\n';
}
signed main() {
ios::sync_with_stdio(false); cin.tie(0);
solve();
}