#!/usr/bin/perl -w

use v5.38;

my $map; { local $/; $map = <>; }

my (@score, %seen, $round);
while (!$seen{$map}) {
	$seen{$map} = $round++;
	for (1 .. 4) {
		my $map1;
		do { # rotate the map:
			$map1 .= join('', reverse $map =~ /^./gm) . "\n"
		} while $map =~ s/^.//gm;
		chomp ($map = $map1);
		1 while $map =~ s/O(\.+)/$1O/g; # move O's to the right
	}
	$score[$round] +=()= substr($map, pos($map)) =~ /\n/g
		while $map =~ /O/g;
}

my $remains = (1000000000 - $round) % ($round - $seen{$map});
say $score[$seen{$map} + $remains];
