I whipped up this in raku:
use Physics::Measure :ALL;
sub infix:<·>(@x1, @x2) {
die "Incompatible dimensions."
unless @x1 == @x2[0] && @x1[0] == @x2;
[for ^@x1 -> $m {
[for ^@x1 -> $n {
[+] @x1[$m;*] >>*<< @x2[*;$n]
}]
}]
}
my @m = [[1m,2m],[3m,4m]];
say @m · [Z] @m; #[[5m^2 11m^2] [11m^2 25m^2]]
Since Physics::Measure is strong on illegal combinations and since there are not many realistic random combinations of Units (s^2 anyone) I have not gone random for my example.