#include <bits/stdc++.h>
using namespace std;
const int NMax = 5e3 + 5;
const int MMax = 1e3 + 5;
int N, M;
char input[NMax][MMax + 101];
short bit[NMax][MMax][2];
char solMin[MMax], solMax[MMax];
map<string, int> ID;
struct elem {
  bool known;
  int op1, op2;
  char tor;
} v[NMax];
int compute(int, int, int);
int main() {
  cin >> N >> M;
  cin.getline(input[0], MMax + 100);
  for (int i = 1; i <= N; ++i) {
    cin.getline(input[i] + 1, MMax + 100);
    string name = "";
    int j = 1;
    while ('a' <= input[i][j] && input[i][j] <= 'z') {
      name += input[i][j++];
    }
    ID[name] = i;
  }
  for (int i = 1; i <= N; ++i) {
    int j = 1;
    while (input[i][j] != '=') {
      ++j;
    }
    j += 2;
    if (input[i][j] == '0' || input[i][j] == '1') {
      v[i].known = true;
      for (int k = 1; k <= M; ++k) {
        bit[i][k][0] = bit[i][k][1] = input[i][j] - '0' + 1;
        ++j;
      }
    } else {
      string str;
      if (input[i][j] == '?') {
        v[i].op1 = -1;
        ++j;
      } else {
        str = "";
        while ('a' <= input[i][j] && input[i][j] <= 'z') {
          str += input[i][j];
          ++j;
        }
        v[i].op1 = ID[str];
      }
      ++j;
      if (input[i][j] == 'A') {
        v[i].tor = '&';
        j += 3;
      } else if (input[i][j] == 'O') {
        v[i].tor = '|';
        j += 2;
      } else {
        v[i].tor = '^';
        j += 3;
      }
      ++j;
      if (input[i][j] == '?') {
        v[i].op2 = -1;
      } else {
        str = "";
        while ('a' <= input[i][j] && input[i][j] <= 'z') {
          str += input[i][j];
          ++j;
        }
        v[i].op2 = ID[str];
      }
    }
  }
  for (int b = 1; b <= M; ++b) {
    int nr1By1 = 0, nr1By0 = 0;
    for (int i = 1; i <= N; ++i) {
      nr1By1 += compute(i, b, 1);
      nr1By0 += compute(i, b, 0);
    }
    if (nr1By1 > nr1By0) {
      solMax[b] = '1';
    } else {
      solMax[b] = '0';
    }
    if (nr1By1 < nr1By0) {
      solMin[b] = '1';
    } else {
      solMin[b] = '0';
    }
  }
  cout << (solMin + 1) << '\n' << (solMax + 1);
  return 0;
}
int compute(int id, int b, int val) {
  if (bit[id][b][val] != 0) {
    return bit[id][b][val] - 1;
  }
  int f1, f2;
  if (v[id].op1 == -1) {
    f1 = val;
  } else {
    f1 = compute(v[id].op1, b, val);
  }
  if (v[id].op2 == -1) {
    f2 = val;
  } else {
    f2 = compute(v[id].op2, b, val);
  }
  int res;
  if (v[id].tor == '&') {
    res = f1 & f2;
  } else if (v[id].tor == '|') {
    res = f1 | f2;
  } else {
    res = f1 ^ f2;
  }
  bit[id][b][val] = res + 1;
  return res;
}
