@OptIn(kotlin.ExperimentalUnsignedTypes::class)
fun verify(suspect: Array<ULongArray>, s: IntArray, t: IntArray, u: ULongArray, v: ULongArray): Boolean {
    for (i in s.indices) {
        val result = if (s[i] == 0) {
            t.indices.fold(0UL.inv()){acc, j -> acc and suspect[i][j]}
        }else {
            t.indices.fold(0UL){acc, j -> acc or suspect[i][j]}
        }
        if (result != u[i])
            return false
    }
    for (j in t.indices) {
        val result = if (t[j] == 0) {
            s.indices.fold(0UL.inv()){acc, i -> acc and suspect[i][j]}
        }else {
            s.indices.fold(0UL){acc, i -> acc or suspect[i][j]}
        }
        if (result != v[j])
            return false
    }
    return true
}
@OptIn(kotlin.ExperimentalUnsignedTypes::class)
fun ULong.nthBit(n: Int): ULong {
    return (this and (1UL shl n))
}
@OptIn(kotlin.ExperimentalUnsignedTypes::class)
fun main() {
    val n = readLine()!!.trim().toInt()
    val s = readLine()!!.trim().split(' ').map(String::toInt).toIntArray()
    val t = readLine()!!.trim().split(' ').map(String::toInt).toIntArray()
    val u = readLine()!!.trim().split(' ').map(String::toULong).toULongArray()
    val v = readLine()!!.trim().split(' ').map(String::toULong).toULongArray()
    val result = Array(n){ULongArray(n){0UL} }
    val (andRow, orRow) = s.indices.partition{s[it] == 0}
    val (andColumn, orColumn) = t.indices.partition{t[it] == 0}
    for (d in 0 until 64) {
        val oneRow = BooleanArray(n){false}
        val zeroRow = BooleanArray(n){false}
        val oneColumn = BooleanArray(n){false}
        val zeroColumn = BooleanArray(n){false}
        val (andOneS, andZeroS) = andRow.partition { u[it].nthBit(d) != 0UL }
        val (orOneS, orZeroS) = orRow.partition { u[it].nthBit(d) != 0UL }
        val (andOneT, andZeroT) = andColumn.partition { v[it].nthBit(d) != 0UL }
        val (orOneT, orZeroT) = orColumn.partition { v[it].nthBit(d) != 0UL }
        for (i in andOneS) {
            for (j in 0 until n) {
                result[i][j] = result[i][j] or (1UL shl d)
                oneRow[i] = true
                oneColumn[j] = true
            }
        }
        for (j in andOneT) {
            for (i in 0 until n) {
                result[i][j] = result[i][j] or (1UL shl d)
                oneRow[i] = true
                oneColumn[j] = true
            }
        }
        for (i in orZeroS) {
            for (j in 0 until n) {
                zeroRow[i] = true
                zeroColumn[j] = true
            }
        }
        for (j in orZeroT) {
            for (i in 0 until n) {
                zeroRow[i] = true
                zeroColumn[j] = true
            }
        }
        //TODO()
        if (orOneS.size + andZeroS.size >= 2 && orOneT.size + andZeroT.size >= 2) {
            var start = 1
            val r = orOneS + andZeroS
            val c = orOneT + andZeroT
            for (i in r) {
                var digit = start
                start = 1 - start
                for (j in c) {
                    result[i][j] = result[i][j] or (digit.toULong() shl d)
                    digit = 1 - digit
                }
            }
        }else if (orOneS.size + andZeroS.size == 1){
            for (i in orOneS) {
                for (j in orOneT) {
                    result[i][j] = result[i][j] or (1UL shl d)
                }
                for (j in andZeroT) {
                    if (zeroColumn[j]) {
                        result[i][j] = result[i][j] or (1UL shl d)
                    }
                }
            }
            for (i in andZeroS) {
                for (j in orOneT) {
                    if (!oneColumn[j]) {
                        result[i][j] = result[i][j] or (1UL shl d)
                    }
                }
            }
        }else if (orOneT.size + andZeroT.size == 1) {
            for (j in orOneT) {
                for (i in orOneS) {
                    result[i][j] = result[i][j] or (1UL shl d)
                }
                for (i in andZeroS) {
                    if (zeroRow[i]) {
                        result[i][j] = result[i][j] or (1UL shl d)
                    }
                }
            }
            for (j in andZeroT) {
                for (i in orOneS) {
                    if (!oneRow[i]) {
                        result[i][j] = result[i][j] or (1UL shl d)
                    }
                }
            }
        }
    }
    if (verify(result, s, t, u, v)) {
        println(result.joinToString("\n"){it.joinToString(" ")})
    }else {
        println(-1)
    }
}


Measured memory_bytes: 108052