05AB1E, 18 1717 8 bytes
Dª€gà©nи®ô¹ì²ζø®∍2FDªI怨
Try it online-9 bytes by porting @JonathanAllan's Jelly answer, so make sure to upvote him as well!
Try it online or verify all test casesverify all test cases.
Could have been 15Previous 17 bytes answer: if the input-matrix is guaranteed to be non-empty:
Ъεε²]²ζø«²ζø¤∍Dª€gà©nи®ô¹ì²ζø®∍
Try it online or verify all test casesTry it online or see the incorrect result for an empty matrixverify all test cases.
D 2F # Duplicate the firstLoop (implicit)2 input-matrixtimes:
ª D # Append theDuplicate matrixthe tocurrent itselfmatrix
€ # Map over(which eachwill innerbe list:the implicit input-matrix in the first iteration)
gª # PopAppend andthe pushmatrix itsto lengthitself
à ζ # Pop and pushZip/transpose; theswapping maximumrows/columns,
I # (we now haveusing the dimension of thesecond outputinput-square, whichcharacter isas eitherfiller
۬ # equal to theAnd amountthen ofremove rowsthe orlast amountitem of columns, whichevereach ofrow
# (after the twoloop, the resulting matrix is largeroutput implicitly)
D © # Store this maximum inDuplicate variablethe `®`first (without poppingimplicit) input-matrix
ª n # Square it
Append the matrix to itself
и € # Repeat the second (implicit) input that# amountMap ofover timeseach asinner list:
g ®ô # Split# it intoPop partsand ofpush sizeits `®`length
à ¹ì # Prepend the# firstPop input-matrixand atpush the frontmaximum
ζ # Zip/transpose; swapping rows/columns,
# (we now have the dimension ²of the output-square, which is either
# using the second input-character as filler
# øequal to the amount #of Androws thenor zip/transposeamount theof rows/columns, backwhichever of
®∍ # Shorten the# matrix to the first `®` amounttwo ofis rowslarger)
© # (afterStore whichthis themaximum resultingin matrixvariable is`®` output(without implicitlypopping)
Ð n # Triplicate the first (implicit)Square input-matrixit
ª и # AppendRepeat the matrix to itself
ε second (implicit) input #that Mapamount overof eachtimes inneras list:
ε ®ô # MapSplit overit eachinto itemparts of thatsize list:`®`
² ¹ì # And simply replace it withPrepend the secondfirst input-character
] #matrix Closeat the nested mapsfront
ζ ζ # Zip/transpose; swapping rows/columns,
² ² # using the second input-character as filler
ø ø # And then zip/transpose the rows/columns back
« # Merge this matrix to the input-matrix
²ζø # Add fillers again in a similar matter
¤ ®∍ # Push the final row (without poppingShorten the matrix itself)
∍ # Shortento the matrix tofirst an`®` amount of rows equal to this row-length
# (after which the resulting matrix is output implicitly)