#include <bits/stdc++.h>
using namespace std;
const long long MX = 5e5 + 9;
const int MAXN = 10100;
const long double pi = acos(-1);
const long long mod = 1e9 + 7;
long long vl;
long long po(long long a, long long b) {
  if (b == 0) {
    return 1;
  }
  long long x = po(a, b / 2);
  x *= x;
  if (b % 2) {
    x *= a;
  }
  return x;
}
int power(int p) {
  if (p == 0) return 1;
  if (p == 1) return vl;
  if (p % 2) return (vl * power(p - 1)) % mod;
  long long x = power(p / 2);
  return (x * x) % mod;
}
int mod_inv(long long a, long long b) {
  vl = b;
  return (a * power(mod - 2)) % mod;
}
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  else {
    return gcd(b, a % b);
  }
}
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
int n, m;
vector<string> pp[5005];
map<string, int> ma;
char ans1[MX], ans2[MX];
int va[5006];
int pos = 0;
int we[5555];
int ww[5555][6];
void get(string &t) {
  vector<string> v;
  string q;
  int l = t.length();
  for (int i = 0; i < l; i++) {
    if (t[i] != ' ') {
      q += t[i];
    } else {
      v.push_back(q);
      q.clear();
    }
  }
  v.push_back(q);
  ma[v[0]] = pos;
  we[pos] = ma[v[0]];
  pp[pos] = v;
  if (pp[pos].size() > 3) {
    ww[pos][0] = ma[v[2]];
    ww[pos][1] = ma[v[4]];
  }
  pos++;
}
int solve(int bit, int id) {
  long long su = 0;
  for (int i = 0; i <= n; i++) {
    va[i] = 0;
  }
  for (int i = 0; i < pos; i++) {
    if (pp[i].size() == 3) {
      va[ma[pp[i][0]]] = pp[i][2][id];
    } else {
      int x, y;
      if (pp[i][2][0] == '?') {
        x = bit;
      } else {
        x = va[ww[i][0]];
      }
      if (pp[i][4][0] == '?') {
        y = bit;
      } else {
        y = va[ww[i][1]];
      }
      if (pp[i][3][0] == 'O') {
        va[we[i]] = x | y;
      } else if (pp[i][3][0] == 'X') {
        va[we[i]] = x ^ y;
      } else if (pp[i][3][0] == 'A') {
        va[we[i]] = x & y;
      }
    }
    su += va[we[i]];
  }
  return su;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    string s;
    while (s.empty()) {
      getline(cin, s);
    }
    get(s);
  }
  for (int i = 0; i < m; i++) {
    long long t1 = solve(0, i);
    long long t2 = solve(1, i);
    if (t1 == t2) {
      ans1[i] = '0';
      ans2[i] = '0';
    } else if (t1 > t2) {
      ans1[i] = '1';
      ans2[i] = '0';
    } else {
      ans1[i] = '0';
      ans2[i] = '1';
    }
  }
  for (int i = 0; i < m; i++) {
    cout << ans1[i];
  }
  cout << endl;
  for (int i = 0; i < m; i++) {
    cout << ans2[i];
  }
  return 0;
}
