#include <bits/stdc++.h>
using namespace std;
int n, m;
string var[5010], op[5010], Left[5010], Right[5010];
int curval[5010][1010][2];
map<string, int> mapperino;
string ans, ans2;
void addLine(string s, int index) {
  int i = 0;
  for (; i < s.length(); i++) {
    if (s[i] == ' ') break;
    var[index] += s[i];
  }
  mapperino[var[index]] = index;
  i++;
  for (; i < s.length(); i++) {
    if (s[i] == ' ') break;
  }
  i++;
  if (s.find(' ', i) == string::npos) {
    op[index] = "N";
    int j = 0;
    for (; i < s.length(); i++, j++)
      curval[index][j][0] = curval[index][j][1] = (s[i] - '0');
  } else {
    for (; i < s.length(); i++) {
      if (s[i] == ' ') break;
      Left[index] += s[i];
    }
    i++;
    for (; i < s.length(); i++) {
      if (s[i] == ' ') break;
      op[index] += s[i];
    }
    i++;
    for (; i < s.length(); i++) {
      Right[index] += s[i];
    }
    int t1 = n + 1, t2 = n + 1;
    if (mapperino[Left[index]]) t1 = mapperino[Left[index]];
    if (mapperino[Right[index]]) t2 = mapperino[Right[index]];
    if (op[index] == "XOR")
      for (int j = 0; j < m; j++) {
        curval[index][j][0] = curval[t1][j][0] ^ curval[t2][j][0];
        curval[index][j][1] = curval[t1][j][1] ^ curval[t2][j][1];
      }
    if (op[index] == "OR")
      for (int j = 0; j < m; j++) {
        curval[index][j][0] = curval[t1][j][0] | curval[t2][j][0];
        curval[index][j][1] = curval[t1][j][1] | curval[t2][j][1];
      }
    if (op[index] == "AND")
      for (int j = 0; j < m; j++) {
        curval[index][j][0] = curval[t1][j][0] & curval[t2][j][0];
        curval[index][j][1] = curval[t1][j][1] & curval[t2][j][1];
      }
  }
}
void read_data() {
  cin >> n >> m;
  cin.ignore();
  for (int i = 0; i < m; i++) curval[n + 1][i][0] = 0, curval[n + 1][i][1] = 1;
  for (int i = 0; i < n; i++) {
    string line;
    getline(cin, line);
    addLine(line, i + 1);
  }
}
void solve() {
  for (int i = 0; i < m; i++) ans += "-", ans2 += "-";
  for (int i = 0; i < m; i++) {
    int s1 = 0, s2 = 0;
    for (int k = 1; k <= n; k++) {
      s1 += curval[k][i][0];
      s2 += curval[k][i][1];
    }
    if (s1 > s2)
      ans[i] = '0', ans2[i] = '1';
    else if (s2 > s1)
      ans[i] = '1', ans2[i] = '0';
    else
      ans[i] = '0', ans2[i] = '0';
  }
  cout << ans2 << endl;
  cout << ans << endl;
}
int main() {
  std::ios::sync_with_stdio(false);
  read_data();
  solve();
  return 0;
}
