#include <bits/stdc++.h>
using namespace std;
const int N = 5001;
int n, m, st[N], sn[N], val[N];
char str[N];
string a[N], b[N], c[N], ans1, ans2;
map<string, int> p;
inline void fet(string &a) {
  scanf("%s", str);
  a = str;
}
inline int sum(int k, int l) {
  for (int i = 1, x, y; i <= n; i++) {
    if (!sn[i]) {
      val[i] = b[i][l] - '0';
    } else {
      if (b[i] == "?")
        x = k;
      else
        x = val[p[b[i]]];
      if (c[i] == "?")
        y = k;
      else
        y = val[p[c[i]]];
      if (st[i] == 0) {
        val[i] = x | y;
      }
      if (st[i] == 1) {
        val[i] = x ^ y;
      }
      if (st[i] == 2) {
        val[i] = x & y;
      }
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) ans += val[i];
  return ans;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    fet(a[i]);
    fet(b[i]);
    fet(b[i]);
    p[a[i]] = i;
    if (b[i][0] == '0' || b[i][0] == '1') {
      sn[i] = 0;
      continue;
    } else {
      fet(c[i]);
      sn[i] = 1;
      if (c[i].size() == 2)
        st[i] = 0;
      else if (c[i][0] == 'X')
        st[i] = 1;
      else
        st[i] = 2;
      fet(c[i]);
    }
  }
  for (int i = 0, x, y; i < m; i++) {
    x = sum(0, i);
    y = sum(1, i);
    if (x <= y)
      ans1.push_back('0');
    else
      ans1.push_back('1');
    if (x >= y)
      ans2.push_back('0');
    else
      ans2.push_back('1');
  }
  cout << ans1 << "\n" << ans2;
  return 0;
}
