Given a square input matrix \$A\$, pad the matrix with one row and one column on all four sides.

  • The value of each element in the top and bottom row should be the sum of the elements in each corresponding column.
  • The value of each element in the left and right column should be the sum of the elements in each corresponding row.
  • The value of the elements in the top left, and bottom right corner should be the sum of the elements on the diagonal
  • The value of the elements in the top right, and bottom left corner should be the sum of the elements in the anti-diagonal.


A = 
1   5   3
3   2   4
2   5   5

 8    6   12   12    7
 9    1    5    3    9
 9    3    2    4    9
12    2    5    5   12
 7    6   12   12    8


The top left and bottom right elements are the sum of the diagonal \$1+2+5=8\$. The top right and bottom left elements are the sum of the anti-diagonal \$2+2+3=7\$.

The top and bottom row (except the corners) are the sum of each of the columns in \$A\$: \$1+3+2=6\$, \$5+2+5=12\$ and \$3+4+5=12\$. Similarly, the left and right column (except the corners) are the sum of each of the rows of \$A\$: \$1+5+3=9\$, \$3+2+4=9\$ and \$2+5+5=12\$.


  • A non-empty square matrix, with non-negative integers.
  • Optional format


  • The matrix padded as explained above
  • Optional format, but it must be the same as the input format

Test cases:

Use the submissions in this challenge if you want to convert the input format to a more suitable one (for instance [[1, 5],[0, 2]]).

0 0 0
0 0 0
0 0 0
1 5
0 2
3 1 7 5
6 1 5 6
2 0 2 2
5 1 7 3
17   24    1    8   15
23    5    7   14   16
 4    6   13   20   22
10   12   19   21    3
11   18   25    2    9 
65   65   65   65   65   65   65
65   17   24    1    8   15   65
65   23    5    7   14   16   65
65    4    6   13   20   22   65
65   10   12   19   21    3   65
65   11   18   25    2    9   65
65   65   65   65   65   65   65
15    1    2   12
 4   10    9    7
 8    6    5   11
 3   13   14    0
30   30   30   30   30   30
30   15    1    2   12   30
30    4   10    9    7   30
30    8    6    5   11   30
30    3   13   14    0   30
30   30   30   30   30   30

This is , so the shortest solution in each language wins. Explanations are highly encouraged.

  • 2
    \$\begingroup\$ Is that to check magic squares? \$\endgroup\$
    – mdahmoune
    Jul 4, 2017 at 12:19
  • \$\begingroup\$ Just checking is quite a bit easier, but it's indeed easy to see if a square is magic this way, yes :-) \$\endgroup\$ Jul 4, 2017 at 12:33

14 Answers 14


Jelly, 27 bytes


Try it online!

  • 2
    \$\begingroup\$ I want to know how this works, please. \$\endgroup\$
    – sporkl
    Jul 4, 2017 at 9:51
  • \$\begingroup\$ @ComradeSparklePony I don't know if I have time to write an explanation right now, sorry. \$\endgroup\$ Jul 4, 2017 at 9:53
  • \$\begingroup\$ It's fine, don't worry. \$\endgroup\$
    – sporkl
    Jul 4, 2017 at 9:55

Octave, 64 bytes

Thanks to Tom Carpenter for both saving 4 bytes, and correcting an error I had in the original code!


Try it online!


@(a)                 % Anonymous function that takes the matrix 'a' as input
 [ ... ]             % Concatenate everything inside to a single matrix
  b=(t=@trace)(a),   % Clever trick by Tom Carpenter. Save a function handle 
                     % for 't=trace', and call it with 'a' as input
                     % Save the result in the variable 'b'
  c=sum(a)           % Sum of all columns of 'a'
  d=t(flip(a));      % Save the trace of 'a' flipped as a variable 'd', while 
                     % concatenating [b,c,d] horizontally at the same time, creating the 
                     % first row of the output
  z=sum(a,2)         % The sum of each row of the input, and store it in a variable 'z'
  ,a,z;              % Concatenate it with 'a' and 'z' again, to create the middle part of the output
 d,c,b]              % Add [d,c,b] to complete the bottom row

Note, I wrote this long after I posted the challenge.


MATL, 27 26 bytes


Try it online! Or verify all test cases.


,        % Do the following twice
  !      %   Tranpose. Takes input implititly in the first iteration
  t      %   Duplicate
  Xs     %   Row vector with the sum of each column
  wy     %   Push a copy to the bottom of the stack
  v      %   Concatenate stack vertically. This attaches the sum of
         %   each row (first iteration) and column (second), leaving 
         %   the matrix with the correct orientation (transposed twice)
]        % End
G        % Push input again
Xds      % Column vector with the diagonal of the matrix. Sum of vector
5L(      % Write that into first and last entries of the result matrix
         % matrix; that is, its upper-left and lower-right corners
P        % Flip result matrix vertically
GP       % Push input matrix vertically flipped
Xds      % Diagonal, sum. Since the input has been vertically flipped,
         % this gives the sum of the anti-diagonal of the input.
5L(      % Write that into the upper-left and lower-right corners of
         % the verticallly flipped version of the result matrix
P        % Flip vertically again, to restore initial orientation
         % Implicitly display
  • \$\begingroup\$ Of course MATL is designed to work with matrices unlike Jelly. >_> \$\endgroup\$ Jul 4, 2017 at 11:33
  • \$\begingroup\$ @EriktheOutgolfer But your answer has more euros! \$\endgroup\$
    – Luis Mendo
    Jul 4, 2017 at 11:38
  • 3
    \$\begingroup\$ Yeah it has euros dollars and yens...unfortunately that's not the winning criterion here. D: \$\endgroup\$ Jul 4, 2017 at 11:43

Jelly, 26 bytes


Try it online!

Looks surprisingly different from Erik's solution.

I finally managed to understand how ¦ works (by debugging through Jelly's code, lol). Too bad it requires a to work with Ç in my case.


The code uses three links. The first helper link pads a vector with its sum on both ends, the second helper link fixes two corners of the matrix and the main link calls these appropriately.

Ç€Zµ⁺ÑÑ    Main link. Argument: M (matrix)
Ç            Call the first helper link (pad row with sums)...
 €           ...on each row of the matrix.
  Z          Transpose, so that the second invocation uses the columns.
   µ         Begin a new monadic chain.
    ⁺        Repeat the previous chain (everything up to here).
     ÑÑ      Call the second helper link twice on the whole matrix.

S;;S    First helper link. Argument: v (1-dimensional list)
S         Sum the argument list.
 ;        Append the argument list to the sum.
  ;       Append...
   S      ...the sum of the argument list.

ŒDµḊṖѵ€1¦ŒḌU    Second helper link. Argument: M (matrix)
ŒD                 Get the diagonals of the matrix, starting with the main diagonal.
  µ                Begin a new monadic chain.
      µ€           Perform the following actions on each diagonal...
        1¦         ...and keep the result for the first item (main diagonal):
   Ḋ                 Remove the first item (incorrect top corner).
    Ṗ                Remove the last item (incorrect bottom corner).
     Ñ               Call the first helper link on the diagonal to pad it with its sum.
          ŒḌ       Convert the diagonals back to the matrix.
            U      Reverse each row, so that consecutive calls fix the other corners.

APL (Dyalog), 37 bytes

(d,+⌿,d∘⌽)⍪(+/,⊢,+/)⍪d∘⌽,+⌿,d←+/1 1∘⍉

Try it online!

1 1∘⍉ diagonal (lit. collapse both axes into one)

d← store that function as d and apply it to the argument

+⌿ prepend the column sums

d∘⌽, prepend d applied to the reversed argument

()⍪ stack the following on top:

+/,⊢,+/ row sums, the unmodified argument, row sums

()⍪ stack the following on top:

d,+⌿,d∘⌽ applied to the argument, column sums, d applied to the reversed argument


Python 3, 155 bytes

This is the suggestion of @LeakyNun, which saves 54 bytes. I then golfed it myself a bit.

def f(m):l=len(m);r=range(l);s=sum;b=[s(m[i][i]for i in r)];c=[s(m[i][l+~i]for i in r)];d=[*map(s,zip(*m))];return[b+d+c,*[[s(a),*a,s(a)]for a in m],c+d+b]

Try it online!

Initial solution - Python 3, 216 bytes

def f(m):l=len(m);r,s=range(l),sum;a,b,c,d=s(m[i][i]for i in r),s(m[i][l-i-1]for i in r),[s(m[i][j]for j in r)for i in r],[s(m[i][j]for i in r)for j in r];print([[a]+d+[b]]+[[c[i]]+m[i]+[c[i]]for i in r]+[[b]+d+[a]])

Try it online!

  • \$\begingroup\$ 162 bytes \$\endgroup\$
    – Leaky Nun
    Jul 4, 2017 at 12:46
  • \$\begingroup\$ @LeakyNun Thanks. Was just updating with ~190 bytes, this is much shorter :P \$\endgroup\$
    – Mr. Xcoder
    Jul 4, 2017 at 12:52

Python 2, 268 250 184 174 bytes

10 thanks to Stewie Griffin

from numpy import *
print hstack((v(([[d]],r,[[e]])),v((l,s,l)),v(([[e]],r,[[d]])))).tolist()

Try it online!

Some explanations The input is uploaded as a matrix. First the code computes the sum of each column and each row using numpy.sum. Then it computes the sum of the diagonal by numpy.trace. After this it obtains the other diagonal by making a left-right flip on the matrix. Finally, it uses numpy.vstack and numpy.hstack to glue the pieces together.

  • \$\begingroup\$ @StewieGriffin Ok, I just updated the code :) \$\endgroup\$
    – mdahmoune
    Jul 4, 2017 at 12:02
  • 1
    \$\begingroup\$ I believe this works for 174 tio.run/… \$\endgroup\$ Jul 4, 2017 at 12:19

R, 129 bytes


An anonymous function that takes a square matrix as input. I'll post an explanation if there's interest.


PHP, 211 bytes

<?foreach($_GET as$l=>$r){$y=0;foreach($r as$k=>$c){$y+=$c;$x[$k]+=$c;$l-$k?:$d+=$c;($z=count($_GET))-1-$k-$l?:$h+=$c;}$o[]=[-1=>$y]+$r+[$z=>$y];}$o[]=[-1=>$h]+$x+[$z=>$d];print_r([-1=>[-1=>$d]+$x+[$z=>$h]]+$o);

Try it online!


foreach($_GET as$l=>$r){
  $y=0; # sum for a row
  foreach($r as$k=>$c){
    $y+=$c; # add to sum for a row
    $x[$k]+=$c; # add to sum for a column and store in array
    $l-$k?:$d+=$c; # make the diagonal sum left to right
    ($z=count($_GET))-1-$k-$l?:$h+=$c; # make the diagonal sum right to left
  $o[]=[-1=>$y]+$r+[$z=>$y]; # add to result array the actual row with sum of both sides
$o[]=[-1=>$h]+$x+[$z=>$d]; # add to result array the last array
print_r([-1=>[-1=>$d]+$x+[$z=>$h]]+$o); #output after adding the first array to the result array

Python 3, 125 bytes

from numpy import*
f=lambda m,t=trace,s=sum:c_[r_[t(m),s(m,1),t(m[::-1])],c_[s(m,0),m.T,s(m,0)].T,r_[t(m[::-1]),s(m,1),t(m)]]

Try it online!

Slightly ungolfed:

import numpy as np

def f_expanded(m):
    return np.c_[np.r_[np.trace(m), np.sum(m, 1), np.trace(m[::-1])],
                 np.c_[np.sum(m, 0), m.T, np.sum(m, 0)].T,
                 np.r_[np.trace(m[::-1]), np.sum(m, 1), np.trace(m)]]

This takes input formatted as a numpy array, then uses the np.c_ and np.r_ indexing tools to build a new array in one go. np.trace and np.sumare used to calculate the sums along the diagonals and everywhere else, respectively. T is used to take the transpose before and after concatenating the sums because it's shorter than making all the arrays 2-dimensional and using np.r_. m[::-1] saves bytes when compared to rot90(m) or fliplr(m) for finding the trace for the second diagonal.

  • \$\begingroup\$ Nice answer! Welcome to the site :) \$\endgroup\$
    – DJMcMayhem
    Jul 6, 2017 at 23:08

Jelly, 24 19 bytes


Try it online!

I know there's already 2 Jelly answers, but this, I think, is fairly different from them both, and is shorter.

The Footer in the TIO link converts the inputs into 2D arrays, runs the above link over them then outputs them similar to the input format.

How it works

,U$⁺Æṭj€SW€j§,"`j"Ɗ - Main link. Takes a matrix A on the right
  $                 - Collect the previous two links:
 U                  -   Reverse each row of A
                        rev(A) = A with reversed rows
,                   -   Yield the pair [A, rev(A)]
   ⁺                - Do this again on [A, rev(a)]:
                      [A, rev(A)]U   -> [rev(A), A]
                      [A, rev(A)],U$ -> [[A, rev(A)], [rev(A), A]]

    Æṭ              - Take the trace of each.
                       This returns [[Tr(A), Tr(rev(A))], [Tr(rev(A)), Tr(A)]]
                       where Tr(·) returns the trace

        S           - Yield the sums of the columns of A
       €            - Over each pair in l:
      j             -   Insert the column sums between their elements
         W€         - Wrap each in an array
                  Ɗ - Group and run the previous commands on A:
            §       -   Yield the row sums of A
             ,"`    -   Pair each with itself
                 "  -   Zip the rows of A with the pairs of row sums
                j   -   Insert each row between each pair.
           j        - Insert the rows plus their sums in between the diagonal and column sums

JavaScript (ES6), 170 bytes


Input and output is a 2D array of numbers.


(a,                             // input matrix: a
    m=g=>a.map((_,i)=>g(i)),    // helper func m: map by index
    s=x=>eval(x.join`+`)        // helper func s: array sum
) =>
        d = s(m(i=>a[i][i])),           // diagonal sum: d
        ...c=m(i=>s(m(j=>a[j][i]))),    // column sums: c
        g = s(m(i=>a[i][a.length-i-1])) // antidiagonal sum: g
    ...a.map(b=>[r = s(b), ...b, r]),   // all rows with row sums on each end
    [g, ...c, d]                        // same as top row, with corners flipped

Test Snippet

Input/output has been formatted with newlines and tabs.


let tests=[[[0]],[[1,5],[0,2]],[[17,24,1,8,15],[23,5,7,14,16],[4,6,13,20,22],[10,12,19,21,3],[11,18,25,2,9]],[[15,1,2,12],[4,10,9,7],[8,6,5,11],[3,13,14,0]]];
<select id=S oninput="I.value=S.selectedIndex?tests[S.value-1].map(s=>s.join`\t`).join`\n`:''"><option>Tests<option>1<option>2<option>3<option>4</select> <button onclick="O.innerHTML=I.value.trim()?f(I.value.split`\n`.map(s=>s.trim().split(/\s+/g))).map(s=>s.join`\t`).join`\n`:''">Run</button><br><textarea rows=6 cols=50 id=I></textarea><pre id=O>


LOGO, 198 bytes

to g :v[:h reduce "+ :v]
op(se :h :v :h)
to f :s[:a reduce "+ map[item # ?]:s][:b reduce "+ map[item # reverse ?]:s][:c apply "map se "sum :s]
op `[[,:a ,@:c ,:b],@[map "g :s][,:b ,@:c ,:a]]

The function f takes in a matrix as a 2D list, then output the resulting matrix. g is helper function.


05AB1E, 38 bytes


Try it online!


A list of lists as a representation of a matrix.


A list of lists as a representation of a matrix.


OD                                            Sum of rows and duplicate
  Š                                           Triple swap
   ζ                                          Zip arrays (to append to the rows)
    sζ                                        Swap and do the same (to prepend to the rows)
      €R€˜                                    Reverse each row and deep flat each row, making the new rows.
          IÅ\OU                               Get the diagonal of the input, sum it and save in variable X
               IÅ/OV                          Get the anti-diagonal of the input, sum it and save in variable Y
                    IÅ|OD                     Get the columns of the input, sum it and duplicate the sums vector.
                         Xš                   Prepend variable x
                           Yª                 Append variable y
                             sXªYš            Repeat for the second row, but now the prepend and append values are switched
                                  Ššsª        Append and Prepend the new rows (first and last)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.