#!/usr/bin/perl -w

use v5.38;

my @map = map { chomp; [ split // ] } <>;
my $xmax = $#{ $map[0] };
my $ymax = $#map;

my $sum;
for my $y (0 .. $ymax) {
	for my $x (0 .. $xmax) {
		next if $map[$y][$x] ne 'O';
		my $y1 = $y;
		$y1-- while $y1 && $map[$y1-1][$x] eq '.';
		if ($y1 != $y) {
			$map[$y1][$x] = 'O';
			$map[$y][$x] = '.';
		}
		$sum += $ymax + 1 - $y1;
	}
}

say join('', @$_) for @map;
say $sum;
