#include <bits/stdc++.h>
using namespace std;
int u, v, j, cnt = 0, n, m, ans0[1005], ans1[1005], a[5005][1005], i,
             link[5005][3], tot = 0, pre[5005];
string s, s1, s2;
map<string, int> mp;
map<string, int>::iterator it;
void calc(int p) {
  int i;
  for (i = 1; i <= n; i++) {
    if (link[i][0] == -1)
      pre[i] = a[i][p];
    else
      pre[i] = (link[i][0] == 0
                    ? (pre[link[i][1]] ^ pre[link[i][2]])
                    : (link[i][0] == 1 ? (pre[link[i][1]] | pre[link[i][2]])
                                       : (pre[link[i][1]] & pre[link[i][2]])));
    if (pre[i] == 1) tot++;
  }
}
int main() {
  memset(link, -1, sizeof(link));
  cin >> n >> m;
  mp["?"] = 0;
  for (i = 1; i <= n; i++) {
    cin >> s;
    mp[s] = i;
    cin >> s;
    cin >> s;
    if (s[0] == '0' || s[0] == '1') {
      for (j = 1; j <= m; j++) a[i][j] = s[j - 1] - '0';
    } else {
      cin >> s1 >> s2;
      if (s1[0] == 'X') link[i][0] = 0;
      if (s1[0] == 'O') link[i][0] = 1;
      if (s1[0] == 'A') link[i][0] = 2;
      link[i][1] = mp[s];
      link[i][2] = mp[s2];
    }
  }
  for (i = 1; i <= m; i++) {
    tot = pre[0] = 0;
    calc(i);
    u = tot;
    tot = 0;
    pre[0] = 1;
    calc(i);
    v = tot;
    ans0[i] = 0, ans1[i] = 1;
    if (u > v) swap(ans0[i], ans1[i]);
    if (u == v) ans1[i] = 0;
  }
  for (i = 1; i <= m; i++) cout << ans0[i];
  cout << endl;
  for (i = 1; i <= m; i++) cout << ans1[i];
  return 0;
}
