#include <bits/stdc++.h>
using namespace std;
int n, m;
struct st {
  string name, val, first, second, op;
};
vector<string> parse(string &s) {
  vector<string> ans;
  string vr = "";
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == ' ') {
      ans.push_back(vr);
      vr = "";
    } else
      vr += s[i];
  }
  ans.push_back(vr);
  return ans;
}
int calk(int a, int b, string op) {
  if (op == "AND") {
    return a * b;
  }
  if (op == "OR") {
    return min(1, a + b);
  }
  if (a == b)
    return 0;
  else
    return 1;
}
int main() {
  cin >> n >> m;
  string k;
  getline(cin, k);
  vector<st> v(n);
  for (int i = 0; i < n; i++) {
    string s;
    getline(cin, s);
    vector<string> p = parse(s);
    if (p.size() == 3) {
      v[i].name = p[0];
      v[i].val = p[2];
    } else {
      v[i].name = p[0];
      v[i].first = p[2];
      v[i].second = p[4];
      v[i].op = p[3];
    }
  }
  string ans_min, ans_max;
  ans_min.resize(m);
  ans_max.resize(m);
  for (int pos = 0; pos < m; pos++) {
    unordered_map<string, int> mp;
    mp["?"] = 0;
    int ans_0, ans_1;
    int sum = 0;
    for (int i = 0; i < v.size(); i++) {
      if (v[i].val != "") {
        mp[v[i].name] = v[i].val[pos] - '0';
      } else {
        int first, second;
        if (v[i].first[0] == '0' || v[i].first[0] == '1')
          first = v[i].first[pos] - '0';
        else
          first = mp[v[i].first];
        if (v[i].second[0] == '0' || v[i].second[0] == '1')
          second = v[i].second[pos] - '0';
        else
          second = mp[v[i].second];
        mp[v[i].name] = calk(first, second, v[i].op);
      }
      sum += mp[v[i].name];
    }
    ans_0 = sum;
    mp.clear();
    mp["?"] = 1;
    sum = 0;
    for (int i = 0; i < v.size(); i++) {
      if (v[i].val != "") {
        mp[v[i].name] = v[i].val[pos] - '0';
      } else {
        int first, second;
        if (v[i].first[0] == '0' || v[i].first[0] == '1')
          first = v[i].first[pos] - '0';
        else
          first = mp[v[i].first];
        if (v[i].second[0] == '0' || v[i].second[0] == '1')
          second = v[i].second[pos] - '0';
        else
          second = mp[v[i].second];
        mp[v[i].name] = calk(first, second, v[i].op);
      }
      sum += mp[v[i].name];
    }
    ans_1 = sum;
    if (ans_1 < ans_0) {
      ans_max[pos] = '0';
      ans_min[pos] = '1';
    }
    if (ans_1 > ans_0) {
      ans_max[pos] = '1';
      ans_min[pos] = '0';
    }
    if (ans_1 == ans_0) {
      ans_min[pos] = '0';
      ans_max[pos] = '0';
    }
  }
  cout << ans_min << endl << ans_max;
  return 0;
}
