#include <bits/stdc++.h>
using namespace std;
const int maxn = 5000 + 23;
int n, m, len, cur[maxn], num[maxn], mini[maxn], maxi[maxn], x[maxn], y[maxn],
    op[maxn];
bool mark[maxn], a[maxn][maxn], ox, oy;
string s, name;
map<string, int> M;
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> name;
    M[name] = i;
    cin >> s;
    cin >> s;
    if ((s[0] != '1') && (s[0] != '0')) {
      if (s[0] == '?')
        x[i] = 0;
      else
        x[i] = M[s];
      cin >> s;
      if (s[0] == 'A') op[i] = 1;
      if (s[0] == 'O') op[i] = 2;
      if (s[0] == 'X') op[i] = 3;
      cin >> s;
      if (s[0] == '?')
        y[i] = 0;
      else
        y[i] = M[s];
    } else {
      len = s.length(), mark[i] = 1;
      for (int j = 0; j <= len - 1; j++) a[i][j + 1] = (bool)(s[j] - '0');
    }
  }
  for (int i = 1; i <= m; i++) {
    memset(cur, 0, sizeof(cur));
    num[0] = num[1] = 0;
    for (int j = 0; j <= 1; j++) {
      for (int l = 1; l <= n; l++) {
        if (mark[l]) {
          cur[l] = a[l][i];
        } else {
          if (x[l] == 0)
            ox = j;
          else
            ox = cur[x[l]];
          if (y[l] == 0)
            oy = j;
          else
            oy = cur[y[l]];
          if (op[l] == 1) cur[l] = ox & oy;
          if (op[l] == 2) cur[l] = ox | oy;
          if (op[l] == 3) cur[l] = ox ^ oy;
        }
      }
      for (int l = 1; l <= n; l++)
        if (cur[l]) num[j]++;
    }
    if (num[0] > num[1]) mini[i] = 1, maxi[i] = 0;
    if (num[0] == num[1]) mini[i] = 0, maxi[i] = 0;
    if (num[0] < num[1]) mini[i] = 0, maxi[i] = 1;
  }
  for (int i = 1; i <= m; i++) cout << mini[i];
  cout << endl;
  for (int i = 1; i <= m; i++) cout << maxi[i];
  cout << endl;
  return 0;
}
