require 'set'

class Digraph
	def add_node(property = {})
		Node.new(property)
	end

	def add_edge(u, v, property = {})
		e = Edge.new(u, v, property)
		u.add_out_edge(e)
		v.add_in_edge(e)
		e
	end
	
	def del_edge(e)
		u, v = e.from, e.to
		u.out_edges.delete(e)
		v.in_edges.delete(e)
		e
	end

	class Node
		def initialize(property)
			@property = property
			@out_edges = Set.new
			@in_edges = Set.new
		end

		def add_out_edge(e)
			@out_edges << e
		end

		def del_out_edge(e)
			@out_edges.delete(e)
		end

		def add_in_edge(e)
			@in_edges << e
		end

		def del_in_edge(e)
			@in_edges.delete(e)
		end

		attr_reader :property, :in_edges, :out_edges
	end

	class Edge
		def initialize(u, v, property)
			@from = u	
			@to = v
			@property = property
		end

		def length
			@property[:length] || 1
		end

		attr_reader :from, :to
	end
end

def min_assign(cost_mat)
	n = cost_mat.size
	g = Digraph.new
	s = g.add_node
	t = g.add_node
	l_nodes = (0..n-1).map{|i| g.add_node({:index => i}) }
	l_nodes.each {|v| g.add_edge(s, v, {:length => 0}) }
	r_nodes = (0..n-1).map{|i| g.add_node({:index => i})}
	r_nodes.each {|v| g.add_edge(v, t, {:length => 0}) }
	(0..n-1).each{|i|
		(0..n-1).each{|j|
			g.add_edge(l_nodes[i], r_nodes[j], {:length => cost_mat[i][j]})
		}
	}

	n.times {
		queue = l_nodes + r_nodes + [s, t]
		dist = Hash.new(Float::INFINITY)
		dist[s] = 0
		path_edge = {}

		loop {
			u = queue.min_by{|v| dist[v]}
			if u == t
				loop {
					e = path_edge[u] || break
					g.del_edge(e)
					g.add_edge(e.to, e.from, {:length => e.length})
					u = e.from
				}
				break
			end
			queue.delete(u)	 
			u.out_edges.each{|e|
				d = dist[u] + e.length
				v = e.to
				if d < dist[v] 
					dist[v] = d
					path_edge[v] = e
				end
			}	
		}
	}

	l_nodes.map{|u|
		u.in_edges.to_a[0].from.property[:index]
	}
end

n = gets.to_i
write_cost = (1..n).map{gets.split.map &:to_i}
erase_cost = (1..n).map{gets.split.map &:to_i}
written = (1..n).map{gets.chomp.each_char.map{|c| c == ?o ? 1 : 0}}

cleanup_cost = (0..n-1).map{|i|
	(0..n-1).map{|j| erase_cost[i][j] * written[i][j]}.inject(:+)
}

select_cost = (1..n).map{[]}
(0..n-1).each{|i|
	(0..n-1).each{|j|
		select_cost[i][j] = cleanup_cost[i]
		if written[i][j] == 1
			select_cost[i][j] -= erase_cost[i][j]
		else
			select_cost[i][j] += write_cost[i][j]
		end
	}
}

assign = min_assign(select_cost)

min_cost = 0
operation = []
(0..n-1).each{|i|
	(0..n-1).each{|j|
		if written[i][j] == 1 && assign[i] != j
			operation << "#{i+1} #{j+1} erase"
		elsif assign[i] == j
			min_cost += select_cost[i][j]	
			if written[i][j] == 0
				operation << "#{i+1} #{j+1} write"
			end
		end
	}
}

p min_cost
p operation.size
puts operation