$;=''
board = $<.map(&:chomp).map(&:split).map{|a| [?.] + a + [?.]}
board.unshift [?.] * 10
board << [?.] * 10

def to_be_turned(board, i, j, di, dj, c)
	count = 0
	loop do 
		i += di
		j += dj
		return count if board[i][j] == c
		return 0 if board[i][j] == ?.
		count += 1
	end
end

def play(board, i, j, c)
	[-1, 0, 1].repeated_permutation(2).map { |di, dj|
		next if di == 0 && dj == 0
		turn(board, i, j, di, dj, c)
	}
end

def turn(board, i, j, di, dj, c)
	board[i][j] = c
	loop do 
		i += di
		j += dj
		return if board[i][j] == ?.
		if board[i][j] == c
			loop do
				i -= di
				j -= dj
				return if board[i][j] == c
				board[i][j] = c
			end
		end
	end
end

c = ?o
skip = false
loop do
	max = 0
	max_by = nil
	ord = (1..8).to_a
	ord.reverse! if c == ?x
	ord.each do |i|
		ord.each do |j|
			next unless board[i][j] == ?.
			x = [-1, 0, 1].repeated_permutation(2).map { |di, dj|
				next 0 if di == 0 && dj == 0
				to_be_turned(board, i, j, di, dj, c)
			}.inject(:+)
			if x > max
				max = x
				max_by = [i, j]
			end
		end
	end

	if max_by
		play(board, *max_by, c)
		skip = false
	elsif skip
		break
	else
		skip = true
	end
	c = (c == ?o ? ?x : ?o)
end

board.shift
board.pop
board.each do |a|
	a.pop
	a.shift
end
puts board.map(&:join)