program i_hate_matrix_construction
  implicit none
  integer, parameter :: intkind = 16
  integer :: n, i, j, k
  integer, dimension(500) :: s = 0, t = 0, r = 0
  integer(intkind), dimension(500) :: u = 0, v = 0, w = 0
  integer(intkind), dimension(500, 500) :: a = 0
  logical, dimension(500) :: b = .false.
  read(*,*) n
  read(*,*) s(1:n)
  read(*,*) t(1:n)
  read(*,*) u(1:n)
  read(*,*) v(1:n)
  do k = 0, 8 * intkind - 1
    do j = 1, 2
      do i = 1, n
        b(i) = btest(u(i), k)
      end do
      call solve()
      a(1:n, 1:n) = transpose(a(1:n, 1:n))
      r(1:n) = s(1:n)
      s(1:n) = t(1:n)
      t(1:n) = r(1:n)
      w(1:n) = u(1:n)
      u(1:n) = v(1:n)
      v(1:n) = w(1:n)
    end do
  end do
  do j = 1, 2
    call check()
    a(1:n, 1:n) = transpose(a(1:n, 1:n))
    r(1:n) = s(1:n)
    s(1:n) = t(1:n)
    t(1:n) = r(1:n)
    w(1:n) = u(1:n)
    u(1:n) = v(1:n)
    v(1:n) = w(1:n)
  end do
  do i = 1, n
    write(*,'(i0)',advance='no') a(i, 1)
    do j = 2, n
      write(*,'(x,i0)',advance='no') a(i, j)
    end do
    write(*,*)
  end do
contains
  subroutine solve()
    integer :: i, j
    do i = 1, n
      if (s(i) == 1 .eqv. b(i)) cycle
      do j = 1, n
        a(i, j) = set(a(i, j), k, b(i))
      end do
    end do
    do i = 1, n
      if (s(i) == 0 .or. .not.b(i)) cycle
      if (any(btest(a(i, 1:n), k))) cycle
      do j = 1, n
        if (t(j) == 1 .or. btest(v(j), k)) cycle
        if (btest(a(i, j), k)) cycle
        a(i, j) = ibset(a(i, j), k)
        if (all(btest(a(1:n, j), k))) then
          a(i, j) = ibclr(a(i, j), k)
        else
          exit
        end if
      end do
    end do
  end
  subroutine check()
    integer :: i, j
    integer(intkind) :: x
    do i = 1, n
      x = a(i, 1)
      do j = 2, n
        x = operate(x, a(i, j), s(i) == 0)
      end do
      if (x /= u(i)) then
        write(*,'(i0)') -1
        stop
      end if
    end do
  end
  integer(intkind) function set(a, k, b) result(res)
    integer(intkind), intent(in) :: a
    integer, intent(in) :: k
    logical, intent(in) :: b
    res = merge(ibset(a, k), ibclr(a, k), b)
  end
  integer(intkind) function operate(a1, a2, b) result(res)
    integer(intkind), intent(in) :: a1, a2
    logical, intent(in) :: b
    res = merge(and(a1, a2), or(a1, a2), b)
  end
end program i_hate_matrix_construction