Language: C++

#include <cstdio>
#include <cstdint>
#include <cinttypes>
#include <iostream>
#include <vector>
#include <utility>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <string>
using u8t = std::uint64_t;
using i8t = std::int64_t;
#define F(I,N) for(i8t I=0,I##_N=N;I<I##_N;I++)
#define FR(I,N) for(i8t I=N;I--;)
#define R(V) u8t V;std::cin>>V;
#define RV(V,N) std::vector<u8t> V;F(_i,N){R(_x)V.push_back(_x);}
#define RL1(line) std::getline(std::cin,line);
#define RL(line) std::string line;RL1(line)
#define RM(V,L,C) std::vector<char> V;F(_i,L){RL(_l)F(_j,C)V.push_back(_l[_j]);}
#define P(X) std::cout<<(X)<<std::endl;
int main() {
  R(N);
  RV(S,N);
  RV(T,N);
  RV(U,N);
  RV(V,N);

  std::vector<u8t> result(N*N,0);

  std::vector<int> mat;
  for(int i=0;i<64;i++){
    u8t bit = (u8t)1<<i;
    mat.clear();
    mat.resize(N*N,2); // 2 荳咲｢ｺ螳夂憾諷・
    // 遒ｺ螳夐Κ蛻・・遒ｺ螳壹＆縺帙ｋ
    F(x,N){
      int s=S[x];
      int u=U[x]&bit?1:0;
      int t=T[x];
      int v=V[x]&bit?1:0;
      if (s!=u){
        F(y,N){
          if(mat[x*N+y]==1-u)goto No;
          mat[x*N+y]=u;
        }
      }
      if (t!=v){
        F(y,N){
          if(mat[y*N+x]==1-v)goto No;
          mat[y*N+x]=v;
        }
      }
    }

    //荳咲｢ｺ螳夐Κ蛻・〒 "縺ｩ繧後°荳縺､" 縺ｮ隕∵ｱゅ′譛ｪ蜈・ｶｳ縺ｮ迚ｩ
    std::vector<i8t> s_solve_x[2];
    std::vector<i8t> t_solve_x[2];
    while (1) {
      int propagate=0;
      s_solve_x[0].clear();
      s_solve_x[1].clear();
      t_solve_x[0].clear();
      t_solve_x[1].clear();
      F(x,N){
        int s=S[x];
        int u=U[x]&bit?1:0;
        int t=T[x];
        int v=V[x]&bit?1:0;
        if (s==u){
          bool found=false;
          F(y,N)if(mat[x*N+y]==u){found=true;break;}
          if(!found){
            s_solve_x[u].push_back(x);
          }else{
            F(y,N)if(mat[x*N+y]==2){mat[x*N+y]=T[y];propagate++;}
          }
        }
        if (t==v){
          bool found=false;
          F(y,N)if(mat[y*N+x]==v){found=true;break;}
          if(!found){
            t_solve_x[v].push_back(x);
          }else{
            F(y,N)if(mat[y*N+x]==2){mat[y*N+x]=S[y];propagate++;}
          }
        }
      }
      if(propagate==0)break;
    }

    auto s_has = s_solve_x[0].size()+s_solve_x[1].size();
    auto t_has = t_solve_x[0].size()+t_solve_x[1].size();
    if(s_has||t_has){
      if(!s_has||!t_has){
        // 荳譁ｹ縺縺代′譛ｪ遒ｺ螳壹・蝣ｴ蜷医↓縺ｯ鬧・岼
        goto No;
      }else if(s_solve_x[0].size()&&s_solve_x[1].size()){
        // s0 s1 荳｡譁ｹ縺ゅｋ譎・        for(auto x:s_solve_x[0])
          F(y,N)if(mat[x*N+y]==2)mat[x*N+y]=S[x];
        for(auto x:s_solve_x[1])
          F(y,N)if(mat[x*N+y]==2)mat[x*N+y]=S[x];
      }else if(t_solve_x[0].size()&&t_solve_x[1].size()){
        // t0 t1 荳｡譁ｹ縺ゅｋ譎・        for(auto x:t_solve_x[0])
          F(y,N)if(mat[y*N+x]==2)mat[y*N+x]=T[x];
        for(auto x:t_solve_x[1])
          F(y,N)if(mat[y*N+x]==2)mat[y*N+x]=T[x];
      }else if(t_solve_x[0].size()&&s_solve_x[0].size()){
        // t0 縺ｨ s0 縺後≠繧区凾(莉悶・縺ｪ縺・
        for(auto x:s_solve_x[0])
          F(y,N)if(mat[x*N+y]==2)mat[x*N+y]=0;
        for(auto x:t_solve_x[0])
          F(y,N)if(mat[y*N+x]==2)mat[y*N+x]=0;
      }else if(t_solve_x[1].size()&&s_solve_x[1].size()){
        // t1 縺ｨ s1 縺後≠繧区凾(莉悶・縺ｪ縺・
        for(auto x:s_solve_x[1])
          F(y,N)if(mat[x*N+y]==2)mat[x*N+y]=1;
        for(auto x:t_solve_x[1])
          F(y,N)if(mat[y*N+x]==2)mat[y*N+x]=1;
      }else if(s_has>=2&&t_has>=2){
        std::vector<i8t> ss=s_solve_x[0];
        for(auto x:s_solve_x[1])ss.push_back(x);
        std::vector<i8t> tt=t_solve_x[0];
        for(auto x:t_solve_x[1])tt.push_back(x);

        for(int i=0;i<ss.size();i++){
          for(int j=0;j<tt.size();j++) {
            auto const x = ss[i];
            auto const y = tt[j];
            mat[x*N+y]= i==j?1:0;
          }
        }
      }else{
        goto No;
      }
    }

    F(I,N*N){
      if(mat[I]&1)result[I]|=bit;
    }
  }
  F(x,N){
    F(y,N){
      std::cout<<result[x*N+y];
      std::cout<<(y+1==N?"\n":" ");
    }
  }
  std::cout<<std::flush;
  return 0;
No:
  P(-1);
  return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:99:24: error: 'x' was not declared in this scope
   99 |           F(y,N)if(mat[x*N+y]==2)mat[x*N+y]=S[x];
      |                        ^
a.cc:104:28: error: 'x' was not declared in this scope
  104 |           F(y,N)if(mat[y*N+x]==2)mat[y*N+x]=T[x];
      |                            ^