import java.util.*
import kotlin.collections.ArrayList


class Edge(val to: Int, val pair: Int, val cost: Int, var hasFlow: Boolean)
fun Array<ArrayList<Edge>>.addEdge(from: Int, to: Int, cost: Int): Unit {
    this[from].add(Edge(to, this[to].size, cost, true))
    this[to].add(Edge(from, this[from].size - 1, -cost, false))
}
fun bellmanFord(nodes: Array<out List<Edge>>, start: Int): IntArray {
    val memo = IntArray(nodes.size){Int.MAX_VALUE shr 1}.also{it[start] = 0}
    repeat(nodes.size) {
        for (i in nodes.indices) {
            for (next in nodes[i]) if (next.hasFlow) {
                if (memo[next.to] > memo[i] + next.cost) {
                    memo[next.to] = memo[i] + next.cost
                }
            }
        }
    }
    return memo
}
fun minCostMatching(writeCosts: List<List<Int>>, eraseCosts: List<List<Int>>, currentState: List<String>): Pair<Int, List<String>> {
    val nodes = Array(writeCosts.size + writeCosts.first().size + 2){ArrayList<Edge>()}
    val start = nodes.size - 2
    val goal = nodes.size - 1
    for (i in writeCosts.indices) {
        for (j in writeCosts[i].indices) {
            when (currentState[i][j]) {
                'o' -> {
                    nodes.addEdge(i, j + writeCosts.size, -eraseCosts[i][j])
                }
                '.' -> {
                    nodes.addEdge(i, j + writeCosts.size, writeCosts[i][j])
                }
                else -> TODO()
            }
        }
    }
    for (i in writeCosts.indices) {
        nodes.addEdge(start, i, 0)
    }
    for (j in writeCosts.first().indices) {
        nodes.addEdge(j + writeCosts.size, goal, 0)
    }
    val potential = bellmanFord(nodes, start).also{it[goal] = 0}
    val minCost = IntArray(nodes.size){Int.MAX_VALUE}
    val queue = PriorityQueue<Pair<Int, Int>>(compareBy(Pair<*, Int>::second))
    val prevEdge = Array(nodes.size){nodes.first().first()}
    var result = eraseCosts.indices.sumBy{i -> eraseCosts[i].indices.filter{j -> currentState[i][j] == 'o'}.sumBy{j -> eraseCosts[i][j]}}
    repeat(writeCosts.size) {
        minCost.run{
            fill(Int.MAX_VALUE)
            this[start] = 0
        }
        queue.run{
            clear()
            add(start to 0)
        }
        while (queue.isNotEmpty()) {
            val (current, cost) = queue.poll()
            for (e in nodes[current]) if (e.hasFlow) {
                if (minCost[e.to] > potential[current] + e.cost - potential[e.to] + cost) {
                    minCost[e.to] = potential[current] + e.cost - potential[e.to] + cost
                    queue.add(e.to to minCost[e.to])
                    prevEdge[e.to] = e
                }
            }
        }
        result += minCost[goal]
        var last = goal
        while(last != start) {
            val edge = prevEdge[last]
            edge.hasFlow = false
            nodes[edge.to][edge.pair].hasFlow = true
            last = nodes[edge.to][edge.pair].to
        }
        for (i in 0 until start) {
            potential[i] += minCost[i]
        }
    }
    val state = List(currentState.size){CharArray(currentState.first().length){'.'}}
    for (i in 0 until currentState.size) {
        state[i][nodes[i].find{ !it.hasFlow }!!.to - currentState.size] = 'o'
    }
    return result to state.map{it.joinToString("")}
}

fun main(args: Array<String>?): Unit {
    val n = readLine()!!.trim().toInt()
    val writeCosts = List(n){ readLine()!!.trim().split(' ').map(String::toInt)}
    val eraseCosts = List(n){ readLine()!!.trim().split(' ').map(String::toInt)}
    val currentState = List(n){ readLine()!!.trim()}
    val (minCost, distState) = minCostMatching(writeCosts, eraseCosts, currentState)
    println(minCost)
    (0 until n).flatMap{i -> (0 until n).filter{j -> currentState[i][j] != distState[i][j]}.map{i to it}}.let{
        println(it.size)
        for ((i, j) in it) {
            when(currentState[i][j]) {
                'o' -> println("${i + 1} ${j + 1} erase")
                '.' -> println("${i + 1} ${j + 1} write")
                else -> TODO()
            }
        }
    }
}





