This one won't explain itself when you run it.
#!/usr/bin/perl do shift;{($t[$h],$*,$m)=@{$b{$m}{$t[$h]||0}};$h+=$*;$m?redo:die@t,$/}
If you can't figure out what the input file is supposed to look like, here are some samples: