#include <bits/stdc++.h>
using namespace std;
const int N = 5e3 + 3;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
int n, m;
map<string, int> mp;
struct node {
  string s;
  int num, a, b, op;
} p[N];
long long cul(int pos, int num) {
  p[0].num = num;
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    if (p[i].op == -1)
      p[i].num = p[i].s[pos];
    else if (p[i].op == 1)
      p[i].num = p[p[i].a].num & p[p[i].b].num;
    else if (p[i].op == 2)
      p[i].num = p[p[i].a].num | p[p[i].b].num;
    else if (p[i].op == 3)
      p[i].num = p[p[i].a].num ^ p[p[i].b].num;
    ans += p[i].num;
  }
  return ans;
}
int main(void) {
  string s, s1, s2, s3, s4, ansmin = "", ansmax = "";
  cin >> n >> m;
  mp["?"] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> s >> s1 >> s2;
    mp[s] = i;
    if (s2[0] >= '0' && s2[0] <= '1') {
      p[i].s = s2;
      p[i].op = -1;
    } else {
      cin >> s3 >> s4;
      if (s3[0] == 'A')
        p[i].op = 1, p[i].a = mp[s2], p[i].b = mp[s4];
      else if (s3[0] == 'O')
        p[i].op = 2, p[i].a = mp[s2], p[i].b = mp[s4];
      else if (s3[0] == 'X')
        p[i].op = 3, p[i].a = mp[s2], p[i].b = mp[s4];
    }
  }
  for (int i = 0; i < m; i++) {
    long long ans0 = cul(i, 0);
    long long ans1 = cul(i, 1);
    if (ans0 < ans1)
      ansmin += '0', ansmax += '1';
    else if (ans0 == ans1)
      ansmin += '0', ansmax += '0';
    else
      ansmin += '1', ansmax += '0';
  }
  cout << ansmin << endl;
  cout << ansmax << endl;
  return 0;
}
void bef0re_submit() {
  puts("Make sure the algorithm is right!");
  puts("LONG LONG!!!");
  puts("Check n and m are misuesd???");
  puts("Make sure output format is right/// Yes??YES  (%.20LF)???");
  puts("if all meet,run with special situation!!!");
  puts("I confirm that I have done all above");
}
