#include <bits/stdc++.h>
using namespace std;
template <typename A>
ostream &operator<<(ostream &cout, vector<A> const &v);
template <typename A, typename B>
ostream &operator<<(ostream &cout, pair<A, B> const &p) {
  return cout << "(" << p.first << ", " << p.second << ")";
}
template <typename A>
ostream &operator<<(ostream &cout, vector<A> const &v) {
  cout << "[";
  for (int i = 0; i < v.size(); i++) {
    if (i) cout << ", ";
    cout << v[i];
  }
  return cout << "]";
}
template <typename A, typename B>
istream &operator>>(istream &cin, pair<A, B> &p) {
  cin >> p.first;
  return cin >> p.second;
}
mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
void usaco(string filename) {
  freopen((filename + ".in").c_str(), "r", stdin);
  freopen((filename + ".out").c_str(), "w", stdout);
}
const long double pi = 3.14159265358979323846;
long long n, m, q, k, l, r, x, y, z;
const long long template_array_size = 1e6 + 16318;
long long a[template_array_size];
long long b[template_array_size];
long long c[template_array_size];
string second, t;
long long ans = 0;
string op[5005];
string val[5005];
pair<long long, long long> dep[5005];
map<string, long long> rev;
long long dp[5005];
long long check(long long x, long long p, long long t) {
  if (x == -1) return t;
  if (dp[x] != -1) return dp[x];
  if (op[x] == "=")
    return dp[x] = val[x][p] - '0';
  else if (op[x] == "OR")
    return dp[x] = check(dep[x].first, p, t) | check(dep[x].second, p, t);
  else if (op[x] == "AND")
    return dp[x] = check(dep[x].first, p, t) & check(dep[x].second, p, t);
  else
    return dp[x] = check(dep[x].first, p, t) ^ check(dep[x].second, p, t);
}
void solve(int tc = 0) {
  cin >> n >> m;
  string v;
  getline(cin, v);
  rev["?"] = -1;
  for (long long i = 0; i < n; i++) {
    vector<string> x;
    getline(cin, v);
    istringstream is(v);
    while (is >> v) {
      x.push_back(v);
    }
    rev[x[0]] = i;
    if (x.size() == 3) {
      op[i] = "=";
      val[i] = x[2];
    } else {
      op[i] = x[3];
      dep[i] = {rev[x[2]], rev[x[4]]};
    }
  }
  string rmin = "", rmax = "";
  for (long long i = 0; i < m; i++) {
    long long c0 = 0, c1 = 0;
    memset(dp, -1, sizeof(dp));
    for (long long j = 0; j < n; j++) {
      c0 += check(j, i, 0);
    }
    memset(dp, -1, sizeof(dp));
    for (long long j = 0; j < n; j++) {
      c1 += check(j, i, 1);
    }
    if (c0 == c1) {
      rmin += "0";
      rmax += "0";
    } else if (c0 < c1) {
      rmin += "0";
      rmax += "1";
    } else {
      rmin += "1";
      rmax += "0";
    }
  }
  cout << rmin << '\n' << rmax << '\n';
}
int main() {
  { ios_base::sync_with_stdio(false); }
  { cin.tie(NULL); }
  cout << setprecision(15) << fixed;
  int tc = 1;
  for (int t = 0; t < tc; t++) solve(t);
}
