[Question]
  [
For each row and then column of a matrix, we can add an extra entry with the sum of the last two entries in that row or column. For example with the following input matrix:
```
[ 1 1 1 ]
[ 2 3 4 ]
```
The resulting matrix would be:
```
[ 1 1 1 2 ]
[ 2 3 4 7 ]
[ 3 4 5 9 ]
```
Given an input of an integer N and an [X,Y] matrix of size at least 2x2, perform the above expansion N times and output the result. The resulting matrix will always be of size [X+N,Y+N].
### Examples:
```
Input:                     Output:
2, [ 0 0 ]                 [ 0 0 0 0 ]
   [ 0 0 ]                 [ 0 0 0 0 ]
                           [ 0 0 0 0 ]
                           [ 0 0 0 0 ]
3, [ 1 1 1 ]               [ 1  1  1  2  3  5 ]
   [ 2 3 4 ]               [ 2  3  4  7 11 18 ]
                           [ 3  4  5  9 14 23 ]
                           [ 5  7  9 16 25 41 ]
                           [ 8 11 14 25 39 64 ]
```
  
[Answer]
# [MATL](https://esolangs.org/wiki/MATL), 13 ~~14~~ ~~15~~ ~~16~~ ~~20~~ ~~21~~ bytes
```
2*:"!tP2:Y)sv
```
Thanks @Zgarb for removing 1 byte!
[**Try it online!**](http://matl.tryitonline.net/#code=Mio6IiF0UDI6WSlzdg&input=MwpbMSAxIDE7IDIgMyA0XQ)
```
2*         % implicitly input number N and multiply by 2
:          % create vector [1,2,...,2*N]
"          % for loop: do this 2*N times
  !        %   transpose. Implicitly input matrix in the first iteration
  tP       %   duplicate and flip vertically
  2:       %   vector [1,2]
  Y)       %   pick submatrix formed by the first two rows
  s        %   sum of each column
  v        %   append as a new row
           % end for
           % implicit display
```
[Answer]
## J, 19 bytes
```
(v"1@v=.,[+&{:}:)^:
```
This defines an adverb, which takes the number on its left, and produces a verb taking the matrix on its right.
For the second example, it gives
```
  3 ((v"1@v=.,[+&{:}:)^:) 2 3 $ 1 1 1 2 3 4
1  1  1  2  3  5
2  3  4  7 11 18
3  4  5  9 14 23
5  7  9 16 25 41
8 11 14 25 39 64
```
## Explanation
```
(v"1@v=.,[+&{:}:)^:  Left argument x, right argument y
(               )^:  Repeat x times:
     v=.               Bind the following verb to v, and apply to y:
         [    }:         y and y-without-last-item
          +&{:           Sum of their last items
        ,                Append that to y
                       (v automatically threads to rows)
 v"1@                  then apply v to columns
```
[Answer]
# K, 23 bytes
```
{x(2({x,+/-2#x}'+)/)/y}
```
In action:
```
  {x(2({x,+/-2#x}'+)/)/y}[3;(1 1 1;2 3 4)]
(1 1 1 2 3 5
 2 3 4 7 11 18
 3 4 5 9 14 23
 5 7 9 16 25 41
 8 11 14 25 39 64)
```
Try it [here](http://johnearnest.github.io/ok/index.html?run=%20%7Bx%282%28%7Bx%2C%2B%2F-2%23x%7D'%2B%29%2F%29%2Fy%7D%5B3%3B%281%201%201%3B2%203%204%29%5D).
[Answer]
# Jelly, ~~15~~ ~~13~~ 12 bytes
*-1 byte by @Dennis*
```
ṫ-S;@"Z
ÇḤ}¡
```
Like @LuisMendo's MATL answer, this transposes the array before doing the transformation along one axis. Therefore, we need to call the function 2\*n times.
```
ṫ-S;@"Z       Helper link. Input: x (2D array)
 -              Numeric literal: -1
ṫ               Get x[-1:], i.e. last two rows in x
  S             Sum
   ;@"          Append each to x. " is 'zipWith'; @ switches argument order.
      Z         Transpose the array.
ÇḤ}¡          Main link. Input: a, n
Ç               Call the last link on a
 Ḥ}             2n
   ¡            times.
  
```
Try it [here](http://jelly.tryitonline.net/#code=4bmrLVM7QCJaCsOH4bikfcKh&input=&args=WzEsMSwxXSxbMiwzLDRd+Mw).
[Answer]
## ES6, 134 bytes
```
(n,a)=>[...a.map(b=>[...b,...Array(n)].map(c=>(c<1/0?0:c=a+d,d=a,a=c))),...Array(n)].map(b=>(b?0:b=[...a].map((c,j)=>c+d[j]),d=a,a=b))
```
Explanation:
```
(n,a)=> // arguments n is number to expand, a is original array
    [...
        a.map(b=> // for each row in a
            [...b,...Array(n)] // append n elements to the row
            .map(c=>(c<1/0?0:c=a+d,d=a,a=c))) // scan the elements and fill the new ones by summing the previous two
        ,...Array(n)] // append n rows
    .map(b=>(b?0:b=[...a].map((c,j)=>c+d[j]),d=a,a=b)) // scan the rows and fill the new rows by summing the previous two rows
```
[Answer]
## Haskell, 67 bytes
```
o%m=m++[o(+)(last m)$last$init m]
(!!).iterate(map(id%).(zipWith%))
```
Usage example:
```
*Main> ( (!!).iterate(map(id%).(zipWith%)) ) [[1,1,1],[2,3,4]] 3
[[1,1,1,2,3,5],[2,3,4,7,11,18],[3,4,5,9,14,23],[5,7,9,16,25,41],[8,11,14,25,39,64]]
```
How it works:
```
(!!).iterate(    ...         )  -- repeatedly apply ... to the first agrument and
                                -- pick the iteration defined by the second arg
                   (zipWith%)   -- for one iteration add a new row and
          map(id%)              -- then a new element at the end of each each row
o%m                             -- add row or element at the end of a row resp.
                                -- argument o is a "modify function"
                                --          m the whole matrix or a row
 m++[    (last m)(last$init m)] -- take m and append the result of combining the
                                -- last and 2nd last element of m
     o(+)                       -- with a modified version of (+)
                                -- modification is none (aka. id) when adding an
                                -- element to the end of a row and
                                -- zipping elementwise (zipWith) when adding a row
```
[Answer]
## CJam, ~~17~~ 16 bytes
```
q~2*{~_2$.+]z}*p
```
Input format is the matrix first (as a CJam-style 2D array) and the number of iterations afterwards.
[Test it here.](http://cjam.aditsu.net/#code=q~2*%7B~_2%24.%2B%5Dz%7D*p&input=%5B%5B%201%201%201%20%5D%20%5B%202%203%204%20%5D%5D%203)
### Explanation
Turns out this is the same solution as everyone else's:
```
q~      e# Read and evaluate input.
2*      e# Double the iteration count.
{       e# Run this block that many times...
  ~     e#   Dump all rows on the stack.
  _     e#   Copy the last row.
  2$    e#   Copy the penultimate row.
  .+    e#   Vectorised addition.
  ]     e#   Wrap all rows in a new array.
  z     e#   Transpose such that the next iteration processes the other dimension.
}*
p       e#   Pretty-print.
```
[Answer]
## Seriously, 20 bytes
```
,,τ"┬`;d@d@X+@q`M"£n
```
Takes input the matrix (as a 2D list), then `N`. Outputs a 2D list.
This version doesn't work on the online interpreter for some reason, but does work with [this pre-challenge commit](https://github.com/Mego/Seriously/tree/5a97f8b14b4cf38b5e21bd11fa29359a813ea01b).
A version that works online, for 23 bytes:
```
,τ",┬`;d@d@X+@q`M"nkΣ£ƒ
```
Takes input in the opposite order (`N`, then matrix).
[Try it online!](http://seriously.tryitonline.net/#code=LM-EIizilKxgO2RAZEBYK0BxYE0ibmvOo8KjxpI&input=MwpbWzEsMSwxXSxbMiwzLDRdXQ)
I will add an explanation after I sleep for a little while. Working around interpreter bugs is never fun.
[Answer]
# Pyth, ~~13~~ 12 bytes
```
u+Rs>2dCGyEQ
```
[Try it online.](https://pyth.herokuapp.com/?code=u%2BRs%3E2dCGyEQ&input=%5B%5B1%2C+1%2C+1%5D%2C+%5B2%2C+3%2C+4%5D%5D%0A3&debug=0) [Test suite.](https://pyth.herokuapp.com/?code=u%2BRs%3E2dCGyEQ&test_suite=1&test_suite_input=%5B%5B1%2C+1%2C+1%5D%2C+%5B2%2C+3%2C+4%5D%5D%0A1%0A%5B%5B0%2C+0%5D%2C+%5B0%2C+0%5D%5D%0A2%0A%5B%5B1%2C+1%2C+1%5D%2C+%5B2%2C+3%2C+4%5D%5D%0A3&debug=0&input_size=2)
Uses the same algorithm to most answers. Takes as input the matrix as a 2D array on the first line and `n` on the second line.
### Explanation
```
u        yEQ     do 2*N times, starting with input matrix:
       CG          transpose
 +R                append to each row:
   s                 sum of
    >2d              last 2 elements of row
```
[Answer]
# Matlab, 60 bytes
I was first messing about with Matlab's fancy indexing methods (i.e., `A(end+1,:)=sum...`) before I figured that in this rare case, simple concatenation is actually cheaper in Matlab. Too bad I had to convert this into an actual function.
Should work with Octave as well.
```
function A=f(A,n)
for i=1:2*n
A=[A;sum(A(end-1:end,:))]';end
```
I suppose this is a prime example of how **not** to make algorithms. For A=2x2, n=1000 this algorithm already takes 5 seconds on my laptop, n=2000 it's almost 50 seconds! (or approximately 30s if A is a `gpuArray` thanks to my trusty Quadro 1000M)
[Answer]
# Java, 2179 bytes
Just Worked it out:-
This code is on Java Language.
```
import java.util.Scanner;
public class FebonnaciMatrix {
        static Scanner scan=new Scanner(System.in);
        public static void main(String[] args) {
        
        int x,y;
        System.out.println("For the Array to Work Upon:- ");
        
        System.out.println("Enter the Row:- ");
        int row=scan.nextInt();
        System.out.println("Enter the Column:- ");
        int col=scan.nextInt();
        
        int inpArr[][]=new int[row][col];
        
        System.out.println("Enter the values");
        inpArr=inpValues(row,col);
        
        System.out.println("The Input Array is:- ");
        display(inpArr,row,col);
        
        System.out.println("Input the Array size of Febonacci Array ");
        
        System.out.println("Enter the Row");
        int frow=scan.nextInt();
        System.out.println("Enter the Column");
        int fcol=scan.nextInt();
        
        int febArr[][]=new int[frow][fcol];
        febArr=copyValue(inpArr,febArr,row,col);
        
        for(x=0;x<row;x++)
        {
            for(y=col;y<fcol;y++)
                febArr[x][y]=febArr[x][y-2]+febArr[x][y-1];
        }
        
        for(x=row;x<frow;x++)
        {
            for(y=0;y<fcol;y++)
                febArr[x][y]=febArr[x-2][y]+febArr[x-1][y];
        }
        
        System.out.println();
        System.out.println("The Febonacci Array:-");
        display(febArr,frow,fcol);
    }
    
    static void display(int[][] arr,int row,int col)
    {
        int x,y;
        for(x=0;x<row;x++)
        {
            for(y=0;y<col;y++)
                System.out.print(arr[x][y]+"\t");
            System.out.println();
        }
    }
    
    static int[][] inpValues(int row,int col)
    {
        int arr[][]=new int[row][col];
        int x,y;
        for(x=0;x<row;x++)
        {
            for(y=0;y<col;y++)
            {
                System.out.print("Enter the value:- ");
                arr[x][y]=scan.nextInt();
            }
        }
        return arr;
    }
    
    static int[][] copyValue(int[][] old, int[][] ne, int row,int col)
    {
        int x,y;    
        for(x=0;x<row;x++)
        {
            for(y=0;y<col;y++)
                ne[x][y]=old[x][y];
            
        }
        return ne;
    }
}
```
[Answer]
# Python, 103 ~~105~~ bytes
```
f=lambda n,L:f(n-1,[l+[sum(l[-2:])]for l in L])if n else L
lambda n,L:zip(*f(n,map(list,zip(*f(n,L)))))
```
Anonymous function takes list of list and passes to recursive function `f`. Output is transposed and then passed to `f` again, then the output of the second go is re-transposed. Output is a list of tuples
Saved two bytes thanks to [bakuriu](https://codegolf.stackexchange.com/users/5219/bakuriu)
[Answer]
# [APL (Dyalog Classic)](https://www.dyalog.com/), 17 bytes
```
{⍉⍵,⊢/2+/⍵}⍣2⍣⎕⊢⎕
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT9dNzkksLs5M/v@ob2pm/qO2CQZcjzra0/5XP@rtfNS7VedR1yJ9I219ILP2Ue9iIyAGKgQKAsn/QIX/07getU3UMFAw0AQTXEZc6upcEEFDBSDU1DBSMFYw0eQyBgA "APL (Dyalog Classic) – Try It Online")
[Answer]
# [Perl 6](http://perl6.org), ~~87 73~~ 71 bytes
```
->\c,\m{for ^c {.[+*]=[+] .[*X-1,2]for m;m.push: [map {[+] m[*X-1,2;$_]},m[0].keys]};m}
->\c,\m{for ^c {.[+*]=[+] .[*X-1,2]for m;m[+*]=[m[*-2;*] »+«m[*-1]]};m}
->\c,\m{for ^c {.[+*]=[+] .[*X-1,2]for m;m[+*]=[m[*-2;*]Z+m[*-1;*]]};m}
```
```
-> \c, \m {
  for ^c { # 0 ..^ c
    # each row
    .[+*]                            # new column at the end of row ($_)
          = [+] .[ * X- 1,2 ]        # add up the last two entries in row ($_)
                              for m; # for every row
    # too bad this was longer than the above code
    # m[*;+*]=map *+*,m[*;*-2,*-1]
    # each column
    m[ +* ]                 # add new row
            = [             # make it an Array rather than a List
                m[ *-2; * ] # the second to last row
                »+«         # added columnwise with
                m[ *-1 ]    # the last row
              ]
  };
  m # return the result
}
```
### Usage:
```
use v6.c;
# give it a lexical name
my &code = ->\c,\m{ … }
my @return = code 3,[[1,1,1],[2,3,4]];
put '[ ', $_».fmt('%2d'), ' ]' for @return;
put '';
put @return.perl ~~ {S:g/' '//};
```
```
[  1  1  1  2  3  5 ]
[  2  3  4  7 11 18 ]
[  3  4  5  9 14 23 ]
[  5  7  9 16 25 41 ]
[  8 11 14 25 39 64 ]
[[1,1,1,2,3,5],[2,3,4,7,11,18],[3,4,5,9,14,23],[5,7,9,16,25,41],[8,11,14,25,39,64]]
```
]