#!/usr/bin/perl -w

use v5.38;

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

my $sum;
my $retry = 1;
while ($retry) {
	$retry = 0;
for my $y (1 .. $ymax) {
	for my $x (0 .. $xmax) {
		if ($map[$y][$x] eq 'O' && $map[$y-1][$x] eq '.') {
			$map[$y-1][$x] = 'O';
			$map[$y][$x] = '.';
			$retry = 1;
		}
	}
}
}
	
say join('', @$_) for @map;

for my $y (0 .. $ymax) {
	for my $x (0 .. $xmax) {
		$sum += $ymax + 1 - $y if $map[$y][$x] eq 'O';
	}
}
say $sum;
