#!/usr/bin/perl

use strict;
use warnings;

our %bigrami=(
    'ak'=>1, 'an'=>1, 'as'=>1, 'at'=>1, 'av'=>1, 'ci'=>1,
    'da'=>1, 'ed'=>1, 'en'=>1, 'ic'=>1, 'ij'=>1, 'in'=>1,
    'is'=>1, 'ja'=>1, 'je'=>1, 'ka'=>1, 'ko'=>1, 'li'=>1,
    'na'=>1, 'ne'=>1, 'ni'=>1, 'no'=>1, 'od'=>1, 'oj'=>1,
    'os'=>1, 'ov'=>1, 'po'=>1, 'pr'=>1, 'ra'=>1, 're'=>1,
    'ri'=>1, 'st'=>1, 'ta'=>1, 'ti'=>1, 'va'=>1, 'za'=>1
);

sub read_file {
    undef $/;
    open my $file, shift;
    my $line=lc<$file>;
    $line=~s/\s//g;
    our @sifrat=split '', $line;
}

sub make_table {
    our @sifrat;
    our $m=shift; # broj stupaca
    our $n=(@sifrat)/$m; # broj redaka
    
    print "  |";
    for (my $i=1; $i<=$m; $i++) {
	printf "%3d", $i;
    }
    print "\n--+", "---"x$m, "\n";
    for (my $i=0; $i<$m; $i++) {
	printf "%2d|", $i+1;
	for (my $j=0; $j<$m; $j++) {
	    if ($i==$j) {
		print "   ";
	    }
	    else {
		my $t=0;
		for (my $k=0; $k<$n; $k++) {
		    my $bigram=$sifrat[$i*$n+$k].$sifrat[$j*$n+$k];
		    if (exists $bigrami{$bigram}) { $t++; }
		}
		printf "%3d", $t;
	    }
	}
	print "\n";
    }
}

read_file shift @ARGV;
make_table @ARGV;
