#include <bits/stdc++.h>
using namespace std;
const int maxn = 5050;
vector<string> lines[maxn];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  unordered_map<string, int> values;
  values.reserve(8192);
  values.max_load_factor(0.10);
  string now, ad;
  getline(cin, now);
  for (int e = 0; e < n; e++) {
    getline(cin, now);
    stringstream cc(now);
    while (cc >> ad) lines[e].push_back(ad);
  }
  string M, Mi, zero;
  for (int e = 0; e < m; e++) zero.push_back('0');
  for (int e = 0; e < m; e++) {
    vector<int> ss;
    for (int f = '0'; f < '2'; f++) {
      values.clear();
      int qz = 0;
      for (int g = 0; g < n; g++) {
        if (lines[g].size() == 3) {
          values[lines[g][0]] = lines[g][2][e];
          if (lines[g][2][e] == '0') qz++;
          continue;
        }
        if (lines[g][2] == "?" && lines[g][4] == "?") {
          string play, next;
          int value = '0';
          if (lines[g][3] == "OR") {
            if (f == '0')
              value = '0', qz++;
            else
              value = '1';
          } else if (lines[g][3] == "AND") {
            if (f == '0')
              value = '0', qz++;
            else
              value = '1';
          } else {
            value = '0', qz++;
          }
          values[lines[g][0]] = value;
        } else if (lines[g][2] == "?" || lines[g][4] == "?") {
          string play, next;
          int value;
          if (lines[g][2] == "?")
            play = lines[g][4];
          else
            play = lines[g][2];
          value = values[play];
          if (lines[g][3] == "OR") {
            if (value == '0' && f == '0')
              qz++, value = '0';
            else
              value = '1';
          } else if (lines[g][3] == "AND") {
            if (value == '0' || f == '0')
              qz++, value = '0';
            else
              value = '1';
          } else {
            if (value == f)
              qz++, value = '0';
            else
              value = '1';
          }
          values[lines[g][0]] = value;
        } else {
          int a1, a2, ans;
          a1 = values[lines[g][2]];
          a2 = values[lines[g][4]];
          ans = a1;
          if (lines[g][3] == "AND") {
            if (a1 == '0' || a2 == '0')
              ans = '0', qz++;
            else
              ans = '1';
          } else if (lines[g][3] == "OR") {
            if (a1 == '0' && a2 == '0')
              ans = '0', qz++;
            else
              ans = '1';
          } else {
            if (a1 == a2)
              ans = '0', qz++;
            else
              ans = '1';
          }
          values[lines[g][0]] = ans;
        }
      }
      ss.push_back(qz);
    }
    if (ss[1] < ss[0])
      M.push_back('1'), Mi.push_back('0');
    else if (ss[1] == ss[0])
      M.push_back('0'), Mi.push_back('0');
    else
      M.push_back('0'), Mi.push_back('1');
  }
  cout << Mi << endl;
  cout << M << endl;
  return 0;
}
