#include <bits/stdc++.h>
using namespace std;
int n, m, b[2][5005];
bool a[5005];
map<string, int> pos;
vector<string> v[5005];
string mx, mn;
int solve(int x, int y) {
  int cnt = 0;
  a[n] = y;
  for (int i = 0; i < n; ++i) {
    if (v[i].size() == 3)
      a[i] = v[i][2][x] - '0';
    else {
      if (v[i][3] == "OR")
        a[i] = a[b[0][i]] | a[b[1][i]];
      else if (v[i][3] == "XOR")
        a[i] = a[b[0][i]] ^ a[b[1][i]];
      else
        a[i] = a[b[0][i]] & a[b[1][i]];
    }
  }
  for (int i = 0; i < n; ++i) cnt += (a[i] == 1);
  return cnt;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  if (fopen("a.txt", "r")) {
    freopen("a.txt", "r", stdin);
  }
  cin >> n >> m;
  cin.ignore();
  pos["?"] = n;
  for (int i = 0; i < n; ++i) {
    string s, t;
    getline(cin, s);
    stringstream ss(s);
    while (ss >> t) v[i].push_back(t);
    pos[v[i][0]] = i;
    if (v[i].size() > 3) b[0][i] = pos[v[i][2]], b[1][i] = pos[v[i][4]];
  }
  for (int i = 0; i < m; ++i) {
    int v0 = solve(i, 0), v1 = solve(i, 1);
    if (v0 >= v1)
      mx.push_back('0');
    else
      mx.push_back('1');
    if (v0 <= v1)
      mn.push_back('0');
    else
      mn.push_back('1');
  }
  cout << mn << '\n' << mx;
}
