#include <bits/stdc++.h>
using namespace std;
const int N = 5e3 + 2;
const int M = 1e3 + 2;
int num[N][M];
int flag[N];
int id[N][2];
int res[M][2];
int op[N];
int n, m;
map<string, int> has;
int ress[M][2];
int ans1[M];
int ans2[M];
void check(int val) {
  for (int i = 0; i < m; i++) num[0][i] = val;
  for (int i = 1; i <= n; i++)
    if (flag[i]) {
      for (int j = 0; j < m; j++)
        if (op[i] == 0)
          num[i][j] = num[id[i][0]][j] ^ num[id[i][1]][j];
        else if (op[i] == 1)
          num[i][j] = num[id[i][0]][j] | num[id[i][1]][j];
        else
          num[i][j] = num[id[i][0]][j] & num[id[i][1]][j];
    }
  for (int i = 1; i <= n; i++)
    for (int j = 0; j < m; j++) res[j][val] += num[i][j];
}
int main() {
  cin >> n >> m;
  string a, b, c, d, e;
  has["?"] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a >> b >> c;
    if (c[0] == '0' || c[0] == '1') {
      for (int j = 0; j < m; j++) num[i][j] = c[j] - '0';
      has[a] = i;
    } else {
      flag[i] = 1;
      cin >> d >> e;
      id[i][0] = has[c];
      id[i][1] = has[e];
      if (d[0] == 'X')
        op[i] = 0;
      else if (d[0] == 'O')
        op[i] = 1;
      else
        op[i] = 2;
      has[a] = i;
    }
  }
  check(0);
  check(1);
  for (int i = 0; i < m; i++) {
    if (res[i][0] > res[i][1]) ans1[i] = 1;
    if (res[i][0] < res[i][1]) ans2[i] = 1;
  }
  for (int i = 0; i < m; i++) cout << ans1[i];
  cout << endl;
  for (int i = 0; i < m; i++) cout << ans2[i];
  cout << endl;
  return 0;
}
