#!/usr/bin/perl


@lines = <STDIN>;
chomp @lines;
($dim, $reps) = split(' ',shift(@lines));

for $r (0..($dim-1)) {
  for $c (0..($dim-1)) {
    my @temp = ();
    $data[$r][$c] = \@temp;
  }
}

for (1..$reps) {
  $prefix = shift(@lines);
  for $r (0..($dim-1)) {
    $text = shift(@lines);
    for $c (0..($dim-1)) {
      last if (length($text) < $c+1);
      if (substr($text, $c, 1) =~ /\w/) {
        push(@{$data[$r][$c]}, $prefix.substr($text, $c, 1));
      }
    }
  }
}

sub empty {
  return (@{$data[$_[0]][$_[1]]} == ());
}

for $r (0..($dim-1)) {
  for $c (0..($dim-1)) {
    my @temp = @{$data[$r][$c]};
    if (@temp == ()) {
      $row = $r-1;
      $col = $c-1;
      $row = $r if (!empty($row,$c));
      $col = $c if (!empty($r,$col));
      print "$row,$col";
    } else {
      print join(',',@temp);
    }
    print ";";
  }
  print "\n";
}

