#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:64000000")
using namespace std;
const int inf = (int)1e9;
const int mod = inf + 7;
const double eps = 1e-9;
const double pi = acos(-1.0);
string second, tmp;
int n, m;
string name[5050];
vector<string> v[5050];
vector<int> g[5050];
map<string, int> id;
int val[5050];
int get_operand(string &second) {
  if (second == "OR") return 0;
  if (second == "AND") return 1;
  if (second == "XOR") return 2;
  return -1;
}
int bin_op(int a, int op, int b) {
  if (op == 0) return (a | b);
  if (op == 1) return (a & b);
  if (op == 2) return (a ^ b);
  assert(false);
  return -1;
}
bool is_name(string &second) {
  if (id.count(second)) return true;
  return false;
}
int get_id(string &second) {
  if (id.count(second)) return id[second];
  return -1;
}
int get(int ind, int bit) {
  int ans = 0;
  for (int i = 0; i < n; i++) {
    val[i] = 0;
    int op = 0;
    for (int j = 0; j < v[i].size(); j++) {
      int nop = g[i][j];
      if (nop == -2)
        nop = 0;
      else if (nop == -3)
        nop = 1;
      else if (nop == -4)
        nop = 2;
      else
        nop = -1;
      if (nop != -1)
        op = nop;
      else {
        if (g[i][j] == -1)
          val[i] = bin_op(val[i], op, bit);
        else if (g[i][j] >= 0 && g[i][j] < n) {
          int p = g[i][j];
          val[i] = bin_op(val[i], op, val[p]);
        } else {
          int x = v[i][j][ind] - '0';
          val[i] = bin_op(val[i], op, x);
        }
      }
    }
    ans += val[i];
  }
  return ans;
}
int main() {
  scanf("%d %d\n", &n, &m);
  for (int i = 0; i < n; i++) {
    getline(cin, second);
    int cnt = 0;
    string cur = "";
    for (int j = 0; j < second.size(); j++) {
      if (second[j] == ' ') {
        if (cur.size() > 0) {
          if (cnt == 0)
            name[i] = cur;
          else if (cnt == 1)
            tmp = cur;
          else
            v[i].push_back(cur);
          cnt++;
        }
        cur = "";
      } else
        cur += second[j];
    }
    if (cur.size() > 0) v[i].push_back(cur);
    id[name[i]] = i;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < v[i].size(); j++) {
      if (v[i][j] == "?")
        g[i].push_back(-1);
      else if (v[i][j] == "OR")
        g[i].push_back(-2);
      else if (v[i][j] == "AND")
        g[i].push_back(-3);
      else if (v[i][j] == "XOR")
        g[i].push_back(-4);
      else if (is_name(v[i][j]))
        g[i].push_back(get_id(v[i][j]));
      else
        g[i].push_back(-5);
    }
  }
  string mn = "", mx = "";
  for (int i = 0; i < m; i++) {
    int ans0 = get(i, 0);
    int ans1 = get(i, 1);
    if (ans0 == ans1) {
      mn += "0";
      mx += "0";
    } else if (ans0 < ans1) {
      mn += "0";
      mx += "1";
    } else {
      mn += "1";
      mx += "0";
    }
  }
  cout << mn << endl << mx << endl;
  return 0;
}
