I can't conceive of how that gets done in under 100 characters, much less 50 or 51 characters. I'm at 147 and I use about 25 characters just to get input and 18 to output it. Is this some kind of crazy executable regular expression substitution that gets it this small?
It is very common in Perl golf to apply a regular expression and then use the side effect of that, especially the values of the $& and $' variables set by the regex. In this challenge if you apply a simple regex to each line and use the values of $& and $' you should be on your way. Oh, and remember that Perl has two dimensional arrays (I use them so rarely in real code that I tend to forget they exist). To go lower you might like to study the Perl built-in vec function. HTH.