#include <bits/stdc++.h>
using std::map;
using std::pair;
using std::string;
using std::vector;
map<string, vector<pair<bool, bool>>> SampleInput();
map<string, vector<pair<bool, bool>>> ReadInput(
    std::istream& in_stream = std::cin);
void WriteAnswer(const vector<bool>& min, const vector<bool>& max,
                 std::ostream& out_stream = std::cout);
pair<vector<bool>, vector<bool>> BestSums(
    const map<string, vector<pair<bool, bool>>>& variables);
int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  auto input = ReadInput();
  auto result = BestSums(input);
  WriteAnswer(result.first, result.second);
  return 0;
}
pair<vector<bool>, vector<bool>> BestSums(
    const map<string, vector<pair<bool, bool>>>& variables) {
  size_t bit_size = variables.at("?").size();
  vector<pair<size_t, size_t>> count_sum(bit_size);
  for (auto var : variables) {
    if (var.first != "?") {
      const auto& variants = var.second;
      for (size_t pos = 0; pos < bit_size; ++pos) {
        count_sum[pos].first += variants[pos].first;
        count_sum[pos].second += variants[pos].second;
      }
    }
  }
  vector<bool> min, max;
  for (auto bit_count : count_sum) {
    min.push_back(bit_count.second < bit_count.first);
    max.push_back(bit_count.second > bit_count.first);
  }
  return std::make_pair(min, max);
}
vector<pair<bool, bool>> Pure(const string& bit_mask);
vector<pair<bool, bool>> Pure(const vector<bool>& bit_mask);
vector<pair<bool, bool>> And(const vector<pair<bool, bool>>& fst,
                             const vector<pair<bool, bool>>& snd);
vector<pair<bool, bool>> Or(const vector<pair<bool, bool>>& fst,
                            const vector<pair<bool, bool>>& snd);
vector<pair<bool, bool>> Xor(const vector<pair<bool, bool>>& fst,
                             const vector<pair<bool, bool>>& snd);
map<string, vector<pair<bool, bool>>> SampleInput() {
  size_t bit_size = 3;
  map<string, vector<pair<bool, bool>>> variables;
  variables["?"] = vector<pair<bool, bool>>(bit_size, std::make_pair(0, 1));
  variables["a"] = Pure(vector<bool>{1, 0, 1});
  variables["b"] = Pure(vector<bool>{0, 1, 1});
  variables["c"] = Xor(variables["b"], variables["?"]);
  return variables;
}
pair<string, vector<pair<bool, bool>>> ParseString(
    string& line, const map<string, vector<pair<bool, bool>>>& varianles);
map<string, vector<pair<bool, bool>>> ReadInput(std::istream& in_stream) {
  size_t variables_size, bit_size;
  in_stream >> variables_size >> bit_size;
  map<string, vector<pair<bool, bool>>> variables;
  variables["?"] = vector<pair<bool, bool>>(bit_size, std::make_pair(0, 1));
  in_stream.get();
  for (size_t var = 0; var < variables_size; ++var) {
    string line;
    getline(in_stream, line);
    variables.insert(ParseString(line, variables));
  }
  return variables;
}
enum Operation { NONE, AND, OR, XOR };
pair<string, vector<pair<bool, bool>>> ParseString(
    string& line, const map<string, vector<pair<bool, bool>>>& varianles) {
  while (line.back() == ' ') {
    line.pop_back();
  }
  size_t finished_lexems = 0;
  string name, fst = "", snd = "";
  vector<pair<bool, bool>> variants;
  Operation operation = NONE;
  for (auto charapter : line) {
    if (charapter != ' ') {
      if ('A' <= charapter && charapter <= 'Z') {
        if (operation == NONE) {
          switch (charapter) {
            case 'X':
              operation = XOR;
              break;
            case 'O':
              operation = OR;
              break;
            default:
              operation = AND;
              break;
          }
        }
      } else if (('a' <= charapter && charapter <= 'z') || charapter == '?' ||
                 '0' == charapter || '1' == charapter) {
        if (finished_lexems == 0) {
          name += charapter;
        } else if (finished_lexems <= 2) {
          fst += charapter;
        } else {
          snd += charapter;
        }
      }
    } else {
      ++finished_lexems;
    }
  }
  switch (operation) {
    case NONE:
      variants = Pure(fst);
      break;
    case AND:
      variants = And(varianles.at(fst), varianles.at(snd));
      break;
    case OR:
      variants = Or(varianles.at(fst), varianles.at(snd));
      break;
    case XOR:
      variants = Xor(varianles.at(fst), varianles.at(snd));
      break;
    default:
      variants = Pure(fst);
      break;
  }
  return std::make_pair(name, variants);
}
vector<pair<bool, bool>> Pure(const string& str) {
  vector<pair<bool, bool>> result;
  for (auto charapter : str) {
    bool bit = charapter == '1';
    result.push_back(std::make_pair(bit, bit));
  }
  return result;
}
vector<pair<bool, bool>> Pure(const vector<bool>& bit_mask) {
  vector<pair<bool, bool>> result;
  for (auto bit : bit_mask) {
    result.push_back(std::make_pair(bit, bit));
  }
  return result;
}
vector<pair<bool, bool>> And(const vector<pair<bool, bool>>& fst,
                             const vector<pair<bool, bool>>& snd) {
  vector<pair<bool, bool>> result;
  for (auto it_1 = fst.begin(), it_2 = snd.begin();
       it_1 != fst.end() && it_2 != snd.end(); ++it_1, ++it_2) {
    result.push_back(std::make_pair(it_1->first && it_2->first,
                                    it_1->second && it_2->second));
  }
  return result;
}
vector<pair<bool, bool>> Or(const vector<pair<bool, bool>>& fst,
                            const vector<pair<bool, bool>>& snd) {
  vector<pair<bool, bool>> result;
  for (auto it_1 = fst.begin(), it_2 = snd.begin();
       it_1 != fst.end() && it_2 != snd.end(); ++it_1, ++it_2) {
    result.push_back(std::make_pair(it_1->first || it_2->first,
                                    it_1->second || it_2->second));
  }
  return result;
}
vector<pair<bool, bool>> Xor(const vector<pair<bool, bool>>& fst,
                             const vector<pair<bool, bool>>& snd) {
  vector<pair<bool, bool>> result;
  for (auto it_1 = fst.begin(), it_2 = snd.begin();
       it_1 != fst.end() && it_2 != snd.end(); ++it_1, ++it_2) {
    result.push_back(
        std::make_pair(it_1->first ^ it_2->first, it_1->second ^ it_2->second));
  }
  return result;
}
void WriteAnswer(const vector<bool>& min, const vector<bool>& max,
                 std::ostream& out_stream) {
  for (auto digit : min) {
    out_stream << digit;
  }
  out_stream << std::endl;
  for (auto digit : max) {
    out_stream << digit;
  }
  out_stream << std::endl;
}
