#include <bits/stdc++.h>
using namespace std;
int n, m, ans0, ans1, opt[5010], t1[5010], t2[5010], v[5010][2], ans[5010][2];
char name[5010][20];
bool fl;
char s1[5010][1010], s2[5010][1010], s[1010];
bool check(char *a, char *b) {
  int l1 = strlen(a + 1), l2 = strlen(b + 1);
  if (l1 != l2) return 0;
  for (int i = 1; i <= l1; i++)
    if (a[i] != b[i]) return 0;
  return 1;
}
int main() {
  scanf("%d%d", &m, &n);
  for (int i = 1; i <= m; i++) {
    if (!fl) scanf("%s", name[i] + 1);
    scanf("%s", s);
    scanf("%s", s1[i] + 1);
    s[1] = 'a';
    scanf("%s", s + 1);
    if (s[1] < 'a' || s[1] > 'z') {
      fl = 0;
      if (s[1] == 'X')
        opt[i] = 1;
      else if (s[1] == 'O')
        opt[i] = 2;
      else
        opt[i] = 3;
      scanf("%s", s2[i] + 1);
    } else {
      fl = 1;
      int len = strlen(s + 1);
      for (int k = 1; k <= len; k++) name[i + 1][k] = s[k];
    }
  }
  for (int i = 1; i <= m; i++) {
    if (s1[i][1] == '?')
      t1[i] = 0;
    else if (s1[i][1] >= '0' && s1[i][1] <= '1')
      t1[i] = -1;
    else {
      for (int j = 1; j <= i - 1; j++)
        if (check(name[j], s1[i])) t1[i] = j;
    }
    if (!opt[i]) continue;
    if (s2[i][1] == '?')
      t2[i] = 0;
    else if (s2[i][1] >= '0' && s2[i][1] <= '1')
      t2[i] = -1;
    else
      for (int j = 1; j <= i - 1; j++)
        if (check(name[j], s2[i])) t2[i] = j;
  }
  v[0][0] = 0;
  v[0][1] = 1;
  for (int i = 1; i <= n; i++) {
    ans0 = ans1 = 0;
    for (int j = 1; j <= m; j++) {
      if (!opt[j]) {
        if (t1[j] == -1) v[j][0] = v[j][1] = s1[j][i] - '0';
        if (t1[j] >= 0) v[j][0] = v[t1[j]][0], v[j][1] = v[t1[j]][1];
      } else {
        int ll[2], rr[2];
        if (t1[j] == -1) ll[0] = ll[1] = s1[j][i] - '0';
        if (t1[j] >= 0) ll[0] = v[t1[j]][0], ll[1] = v[t1[j]][1];
        if (t2[j] == -1) rr[0] = rr[1] = s2[j][i] - '0';
        if (t2[j] >= 0) rr[0] = v[t2[j]][0], rr[1] = v[t2[j]][1];
        if (opt[j] == 1) v[j][0] = ll[0] ^ rr[0], v[j][1] = ll[1] ^ rr[1];
        if (opt[j] == 2) v[j][0] = ll[0] | rr[0], v[j][1] = ll[1] | rr[1];
        if (opt[j] == 3) v[j][0] = ll[0] & rr[0], v[j][1] = ll[1] & rr[1];
      }
      ans0 += v[j][0];
      ans1 += v[j][1];
    }
    if (ans0 < ans1) {
      ans[i][0] = 0;
      ans[i][1] = 1;
    } else if (ans0 > ans1) {
      ans[i][0] = 1;
      ans[i][1] = 0;
    } else {
      ans[i][0] = ans[i][1] = 0;
    }
  }
  for (int i = 1; i <= n; i++) printf("%d", ans[i][0]);
  printf("\n");
  for (int i = 1; i <= n; i++) printf("%d", ans[i][1]);
  printf("\n");
  return 0;
}
