#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  if (0) return;
  cout << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  if (0) return;
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
const int N = 3e6 + 7, M = 1000000007, inf = INT_MAX;
const long long INF = LLONG_MAX;
int n, Q, m;
int arr[N + 2];
class val {
 public:
  string var, op1, op2, opr;
};
vector<val> vec;
string getstr(string& str, int& j) {
  int sz = str.size();
  string res = "";
  while (j < sz and str[j] != ' ') res.push_back(str[j]), j++;
  j++;
  if (res.size() == 0) res = "!";
  return res;
}
val getvalue(string str) {
  int sz = str.size();
  val res;
  int j = 0;
  res.var = getstr(str, j);
  getstr(str, j);
  res.op1 = getstr(str, j);
  res.opr = getstr(str, j);
  res.op2 = getstr(str, j);
  return res;
}
char perform(char a, char b, char op) {
  int x = a - '0';
  int y = b - '0';
  if (op == 'X') return (x ^ y) + '0';
  if (op == 'A') return (x & y) + '0';
  return (x | y) + '0';
}
int get_ans(char bt, int k) {
  int sum = 0;
  unordered_map<string, char> mp;
  mp["?"] = bt;
  for (int i = 0; i < (int)n; i++) {
    char ch;
    if (vec[i].opr == "!")
      ch = vec[i].op1[k];
    else {
      string s1 = vec[i].op1, s2 = vec[i].op2;
      ch = perform(mp[s1], mp[s2], vec[i].opr[0]);
    }
    if (ch == '1') sum++;
    mp[vec[i].var] = ch;
  }
  return sum;
}
void solve() {
  cin >> n >> m;
  vec.resize(n);
  string s;
  cin.ignore();
  for (int i = 0; i < (int)n; i++) {
    getline(cin, s, '\n');
    val v1 = getvalue(s);
    vec[i] = v1;
  }
  string ans1(m, '0');
  string ans2(m, '0');
  for (int i = 0; i < (int)m; i++) {
    int g0 = get_ans('0', i);
    int g1 = get_ans('1', i);
    if (g0 < g1) {
      ans1[i] = '0';
      ans2[i] = '1';
    } else if (g1 < g0) {
      ans1[i] = '1';
      ans2[i] = '0';
    }
  }
  cout << ans1 << '\n';
  cout << ans2 << '\n';
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int test = 1;
  while (test--) solve();
}
