#include <bits/stdc++.h>
using namespace std;
const int AND = 1;
const int OR = 2;
const int XOR = 3;
int opToInt(string op) {
  if (op == "AND") {
    return AND;
  }
  if (op == "OR") {
    return OR;
  }
  return XOR;
}
int n, m;
vector<vector<int>> a;
string solve(vector<string> vals, bool mn) {
  for (int bt = 0; bt < m; bt++) {
    string tries = "01?";
    int best = mn ? (1 << 30) : -1;
    for (int i = 0; i < tries.size(); i++) {
      vals[n][bt] = tries[i];
      int s = 0;
      for (auto row : a) {
        int l = vals[row[1]][bt] - '0';
        int r = vals[row[3]][bt] - '0';
        int res = 0;
        if (row[2] == AND) {
          res = l & r;
        } else if (row[2] == OR) {
          res = l | r;
        } else if (row[2] == XOR) {
          res = l ^ r;
        }
        s += res;
        vals[row[0]][bt] = res + '0';
      }
      if (mn ? (s < best) : (s > best)) {
        best = s;
        tries[2] = tries[i];
      }
    }
  }
  return vals[n];
}
int main() {
  cin >> n >> m;
  vector<string> vals(n + 1);
  map<string, int> mp;
  mp["?"] = n;
  int mx = 0;
  auto getIndex = [&](string v) {
    if (mp.count(v) == 0) {
      mp[v] = mx++;
    }
    return mp[v];
  };
  for (int i = 0; i < n; i++) {
    string v, eq, opL, op, opR;
    cin >> v >> eq >> opL;
    int vi = getIndex(v);
    if (opL[0] == '0' || opL[0] == '1') {
      vals[vi] = opL;
    } else {
      cin >> op >> opR;
      a.push_back({vi, getIndex(opL), opToInt(op), getIndex(opR)});
      vals[vi] = string(m, '0');
    }
  }
  vals[n] = string(m, '0');
  cout << solve(vals, true) << endl << solve(vals, false) << endl;
}
