[Question]
  [
Take a matrix of positive integers as input, and **make it explode!**
---
The way you explode a matrix is by simply adding zeros around every element, including the outside borders.
Input/output formats are optional as always!
### Test cases:
```
1
-----
0 0 0
0 1 0
0 0 0
--------------
1 4
5 2
-----
0 0 0 0 0
0 1 0 4 0
0 0 0 0 0
0 5 0 2 0
0 0 0 0 0
--------------
1 4 7
-----
0 0 0 0 0 0 0
0 1 0 4 0 7 0
0 0 0 0 0 0 0
--------------
6
4
2
-----
0 0 0
0 6 0
0 0 0
0 4 0
0 0 0
0 2 0
0 0 0
```
  
[Answer]
# [Operation Flashpoint](https://en.wikipedia.org/wiki/Operation_Flashpoint:_Cold_War_Crisis) scripting language, 182 bytes
```
f={t=_this;c=count(t select 0);e=[0];i=0;while{i<c*2}do{e=e+[0];i=i+1};m=[e];i=0;while{i<count t}do{r=+e;j=0;while{j<c}do{r set[j*2+1,(t select i)select j];j=j+1};m=m+[r,e];i=i+1};m}
```
**Ungolfed:**
```
f=
{
  // _this is the input matrix. Let's give it a shorter name to save bytes.
  t = _this;
  c = count (t select 0);
  // Create a row of c*2+1 zeros, where c is the number of columns in the
  // original matrix.
  e = [0];
  i = 0;
  while {i < c*2} do
  {
    e = e + [0];
    i = i + 1
  };
  m = [e]; // The exploded matrix, which starts with a row of zeros.
  i = 0;
  while {i < count t} do
  {
    // Make a copy of the row of zeros, and add to its every other column 
    // the values from the corresponding row of the original matrix.
    r = +e;
    j = 0;
    while {j < c} do
    {
      r set [j*2+1, (t select i) select j];
      j = j + 1
    };
    // Add the new row and a row of zeroes to the exploded matrix.
    m = m + [r, e];
    i = i + 1
  };
  // The last expression is returned.
  m
}
```
**Call with:**
```
hint format["%1\n\n%2\n\n%3\n\n%4",
    [[1]] call f,
    [[1, 4], [5, 2]] call f,
    [[1, 4, 7]] call f,
    [[6],[4],[2]] call f];
```
**Output:**
[![](https://i.stack.imgur.com/TY9Fu.jpg)](https://i.stack.imgur.com/TY9Fu.jpg)
**In the spirit of the challenge:**
[![](https://i.stack.imgur.com/Ut1xq.gif)](https://i.stack.imgur.com/Ut1xq.gif)
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~12~~ 11 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
-1 byte thanks to Erik the Outgolfer (no need to use swapped arguments for a join)
```
j00,0jµ€Z$⁺
```
**[Try it online!](https://tio.run/##y0rNyan8/z/LwEDHIOvQ1kdNa6JUHjXu@v//f3S0oY5JrE60qY5RbCwA "Jelly – Try It Online")** Or see a [test suite](https://tio.run/##y0rNyan8/z/LwEDHIOvQ1kdNa6JUHjXu@n90z@F2IMcbioEoEoizHjXuO7Tt0Lb//6O5FKKjDWNjdcC0jkmsTrSpjhGCr2MOZZsBZUCyQDmuWAA "Jelly – Try It Online").
A monadic link accepting and returning lists of lists.
### How?
```
j00,0jµ€Z$⁺ - Link: list of lists, m
          ⁺ - perform the link to the left twice in succession:
         $  -   last two links as a monad
      µ€    -     perform the chain to the left for €ach row in the current matrix:
j0          -       join with zeros                [a,b,...,z] -> [a,0,b,0,...,0,z]
  0,0       -       zero paired with zero = [0,0]
     j      -       join                     [a,0,b,0,...,0,z] -> [0,a,0,b,0,...,0,z,0]
        Z   -     and then transpose the resulting matrix
```
[Answer]
# [Haskell](https://www.haskell.org/), 38 bytes
```
a%l=a:(=<<)(:[a])l
f l=(0%)<$>(0<$l)%l
```
[Try it online!](https://tio.run/##BcE7DoAgDADQ3VN0gKQkDP4mUrxI06GDRGIlRr0/vnfoe@5mvau3rAkzUcDEKsGGApZx9IHchiM5C976pbVBhvup7QMHBZinOEvkJa4i/Qc "Haskell – Try It Online")
[Answer]
# [MATL](https://github.com/lmendo/MATL), 12 bytes
```
FTXdX*0JQt&(
```
Input is a matrix with `;` as row separator.
[**Try it online!**](https://tio.run/##y00syfn/3y0kIiVCy8ArsERN4///aEMFE2sFUwWjWAA "MATL – Try It Online")
### Explanation
```
FT     % Push [0 1]
Xd     % Matrix with that diagonal: gives [0 0; 0 1]
X*     % Implicit input. Kronecker product
0      % Push 0
JQt    % Push 1+j (interpreted as "end+1" index) twice
&(     % Write a 0 at (end+1, end+1), extending the matrix. Implicit display
```
[Answer]
# [Japt](https://github.com/ETHproductions/japt), 18 bytes
```
Ov"y ®î íZ c p0Ã"²
```
[Test it online!](http://ethproductions.github.io/japt/?v=1.4.5&code=T3YieSCu7iDtWiBjIHAwwyKy&input=WwpbMSAyIDNdCls0IDUgNl0KWzcgOCA5XQpdIC1R) (Uses the `-Q` flag so the output is easier to understand.)
Similar to the Jelly answer, but a whole lot longer...
### Explanation
The outer part of the code is just a workaround to simulate Jelly's `⁺`:
```
  "             "²   Repeat this string twice.
Ov                   Evaluate it as Japt.
```
The code itself is:
```
y ®   î íZ c p0Ã
y mZ{Zî íZ c p0}   Ungolfed
y                  Transpose rows and columns.
  mZ{          }   Map each row Z by this function:
     Zî              Fill Z with (no argument = zeroes).
        íZ           Pair each item in the result with the corresponding item in Z.
           c         Flatten into a single array.
             p0      Append another 0.
```
Repeated twice, this process gives the desired output. The result is implicitly printed.
[Answer]
## [Husk](https://github.com/barbuz/Husk), 12 bytes
```
₁₁
Tm»o:0:;0
```
Takes and returns a 2D integer array. [Try it online!](https://tio.run/##yygtzv7//1FTIxBxheQe2p1vZWBlbfD////oaEMdIx3jWJ1oEx1THbPYWAA "Husk – Try It Online")
## Explanation
Same idea as in many other answers: add zeroes to each row and transpose, twice.
The row operation is implemented with a fold.
```
₁₁         Main function: apply first helper function twice
Tm»o:0:;0  First helper function.
 m         Map over rows:
  »         Fold over row:
   o         Composition of
      :       prepend new value and
    :0        prepend zero,
       ;0    starting from [0].
            This inserts 0s between and around elements.
T          Then transpose.
```
[Answer]
## Mathematica, 39 bytes
```
r=Riffle[#,0,{1,-1,2}]&/@Thread@#&;r@*r
```
[Try it at the Wolfram sandbox!](https://sandbox.open.wolframcloud.com/) Call it like "`r=Riffle[#,0,{1,-1,2}]&/@Thread@#&;r@*r@{{1,2},{3,4}}`".
Like many other answers, this works by transposing and riffling zeros in each row then doing the same thing again. Inspired by Jonathan Allan's [Jelly answer](https://codegolf.stackexchange.com/a/128270/66104) specifically, but only because I happened to see that answer first.
[Answer]
# Dyalog APL, 24 bytes
*4 bytes saved thanks to @ZacharyT*
*5 bytes saved thanks to @KritixiLithos*
```
{{⍵↑⍨-1+⍴⍵}⊃⍪/,/2 2∘↑¨⍵}
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT///P@1R24Tq6ke9Wx@1TXzUu0LXUMFQ@1HvFqBA7aOu5ke9q/R19I0UjB51zAAqOLQCJA7UpGDIpW6rzgWkFYwUjKFsoGJkLkgXyKDNJlABYwUTiIAlAA)
[Answer]
# Octave, 41 bytes
```
@(M)resize(kron(M,[0 0;0 1]),2*size(M)+1)
```
[Try it online!](https://tio.run/##y08uSSxL/f/fQcNXsyi1OLMqVSO7KD9Pw1cn2kDBwNpAwTBWU8dICyzhq6ltqPk/Ma9YI1pPT4/LUMGIy1TBLFbzPwA "Octave – Try It Online")
[Answer]
# [Python 3](https://docs.python.org/3/), ~~104 101 97 93~~ 86 bytes
* @Zachary T saved 3 bytes: unused variable `w` removed and one unwanted space
* @Zachary T saved yet another 4 bytes: `[a,b]` as just `a,b` while appending to a list
* @nore saved 4 bytes: use of slicing
* @Zachary T and @ovs helped saving 7 bytes: squeezing the statements in for loop
```
def f(a):
 m=[(2*len(a[0])+1)*[0]]
 for i in a:r=m[0][:];r[1::2]=i;m+=r,m[0]
 return m
```
[Try it online!](https://tio.run/##FY27DsMgDADn@is8QsJQ6CMSEV9ieUAqUZEKiSw69Ospme50yx2/9t7rrfdX2nBTUXvAEki56ZOqinRlPVs9DTLgtgtmzBWjl1BGI8@rkPXecchrmYOYMwNKal@pWHoMBBeyxpkHm2GLuZsnA8MhuTZ1HnX/Aw "Python 3 – Try It Online")
[Answer]
# Python 3, 118 bytes
```
def a(b):
    z='00'*len(b[0])+'0'
    r=z+'\n'
    for c in b:
        e='0'
        for d in c:e+=str(d)+'0'
        r+=e+'\n'+z+'\n'
    return r
```
First time golfing! Not the best, but I'm quite proud if I can say so myself!
* -17 bytes from Wheat comments
* -4 bytes from inlining the second for loop
[Answer]
# [Python 2](https://docs.python.org/2/), 64 bytes
```
lambda l:map(g,*map(g,*l))
g=lambda*l:sum([[x,0]for x in l],[0])
```
[Try it online!](https://tio.run/##K6gsycjPM/qfZhvzPycxNyklUSHHKjexQCNdRwtK5WhqcqXbQiS1cqyKS3M1oqMrdAxi0/KLFCoUMvMUcmJ1og1iNf/n2EZHG@oY6RgD@SY6pjpmsbFcBUWZeSUKaRo5mv8B "Python 2 – Try It Online")
The function `g` intersperses the input between zeroes. The main function transposes the input while applying `g`, then does so again. Maybe there's a way to avoid the repetition in the main function.
[Answer]
## R, 65 bytes
Thanks to Jarko Dubbeldam and Giuseppe for very valuable comments!
Code
```
f=function(x){a=dim(x);y=array(0,2*a+1);y[2*1:a[1],2*1:a[2]]=x;y}
```
Input for the function must be a matrix or two dimensional array.
Test
```
f(matrix(1))
f(matrix(c(1,5,4,2),2))
f(matrix(c(1,4,7),1))
f(matrix(c(6,4,2)))
```
Output
```
> f(matrix(1))
     [,1] [,2] [,3]
[1,]    0    0    0
[2,]    0    1    0
[3,]    0    0    0
> f(matrix(c(1,5,4,2),2))
     [,1] [,2] [,3] [,4] [,5]
[1,]    0    0    0    0    0
[2,]    0    1    0    4    0
[3,]    0    0    0    0    0
[4,]    0    5    0    2    0
[5,]    0    0    0    0    0
> f(matrix(c(1,4,7),1))
     [,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,]    0    0    0    0    0    0    0
[2,]    0    1    0    4    0    7    0
[3,]    0    0    0    0    0    0    0
> f(matrix(c(6,4,2)))
     [,1] [,2] [,3]
[1,]    0    0    0
[2,]    0    6    0
[3,]    0    0    0
[4,]    0    4    0
[5,]    0    0    0
[6,]    0    2    0
[7,]    0    0    0
```
[Answer]
# Mathematica, 55 bytes
```
(a=ArrayFlatten)@{o={0,0},{0,a@Map[{{#,0},o}&,#,{2}]}}&
```
[Answer]
# [PHP](https://php.net/), 98 bytes
Input as 2D array, Output as string
```
<?foreach($_GET as$v)echo$r=str_pad(0,(count($v)*2+1)*2-1," 0"),"
0 ".join(" 0 ",$v)." 0
";echo$r;
```
[Try it online!](https://tio.run/##JYyxCsIwGIT3PEU4MqQaS1J0isVJfAG3EEqIldShCW319eOPLsfH3ceVVOr5UlJhYrhd771zMFA4wiuHE1EH72195mUMMcmfxMMqPs0YUxZLv27LUMJDaiVjfs@bpGnX7Q3FwShwjUaBaY72ladZUsGhyGmJGOz/xdb6BQ "PHP – Try It Online")
# [PHP](https://php.net/), 116 bytes
Input and Output as 2D array
```
<?foreach($_GET as$v){$r[]=array_fill(0,count($v)*2+1,0);$r[]=str_split("0".join(0,$v)."0");}$r[]=$r[0];print_r($r);
```
[Try it online!](https://tio.run/##HcrBCgIhGATge08RPx60ZHGXOtnSKXqBbiIiS8u6iMqvBRE9u8lehhnmS0uql2ta0o6Y@@0xKgU9cDiB5grOrQ2gtaxzxKedFrqhvc3kzb4ElR4tov2Y2XlPBZ/iKxTavsNw7LlgciO5oMnJu0JBQLdGFxptqGuTyd9mWggtE7pQDFKCTNb6Bw "PHP – Try It Online")
[Answer]
## Clojure, 91 bytes
```
#(for[h[(/ 2)]i(range(- h)(count %)h)](for[j(range(- h)(count(% 0))h)](get(get % i[])j 0)))
```
Iterates over ranges in half-steps.
[Answer]
# [Perl 6](http://perl6.org/), 33 bytes
```
{map {0,|$_,0},0 xx$_,|$_,0 xx$_}
```
[Try it online!](https://tio.run/##K0gtyjH7n1upoJamYPu/OjexQKHaQKdGJV7HoFbHQKGiAsgC88DM2v/WCsWJlQppGho2hgomdjoKNqYKRnaamv8B "Perl 6 – Try It Online")
[Answer]
## JavaScript (ES6), ~~73~~ 72 bytes
```
a=>(g=a=>(r=[b],a.map(v=>r.push(v,b)),b=0,r))(a,b=a[0].map(_=>0)).map(g)
```
### Formatted and commented
Inserting zeros horizontally and vertically are very similar operations. The idea here is to use the same function **g()** for both steps.
```
a =>                            // a = input array
  (g = a =>                     // g = function that takes an array 'a',
    (                           //     builds a new array 'r' where
      r = [b],                  //     'b' is inserted at the beginning
      a.map(v => r.push(v, b)), //     and every two positions,
      b = 0,                    //     sets b = 0 for the next calls
      r                         //     and returns this new array
  ))(a, b = a[0].map(_ => 0))   // we first call 'g' on 'a' with b = row of zeros
  .map(g)                       // we then call 'g' on each row of the new array with b = 0
```
### Test cases
```
let f =
a=>(g=a=>(r=[b],a.map(v=>r.push(v,b)),b=0,r))(a,b=a[0].map(_=>0)).map(g)
console.log(JSON.stringify(f([
  [1]
])))
console.log(JSON.stringify(f([
  [1, 4],
  [5, 2]
])))
console.log(JSON.stringify(f([
  [1, 4, 7]
])))
console.log(JSON.stringify(f([
  [6],
  [4],
  [2]
])))
```
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 49 bytes
```
Ａ⪪θ;αＡ””βＦ⁺¹×²Ｌ§α⁰Ａ⁺β⁰βＡ⁺β¶βＦα«βＡ0δＦιＡ⁺δ⁺κ⁰δ⁺δ¶»β
```
[Try it online!](https://tio.run/##S85ILErOT8z5///9noWPVq06t8P63EYQs2EuEJ3b9H7PskeNuw7tPDz9EJC95tDycxsfNW4AKWjcdW4TkAlSAuEc2gZWfm7jodVgQYNzW0DcnVD5LSBiF0gHmLXl0LZDu89t@v/f0MTc2szECAA "Charcoal – Try It Online")
The input is a single string separating the rows with a semicolon.
[Answer]
# C++17 + Modules, 192 bytes
Input as rows of strings from *cin*, Output to *cout*
```
import std.core;int main(){using namespace std;int i;auto&x=cout;string s;while(getline(cin,s)){for(int j=i=s.length()*2+1;j--;)x<<0;x<<'\n';for(auto c:s)x<<'0'<<c;x<<"0\n";}for(;i--;)x<<'0';}
```
[Answer]
## [C#](http://csharppad.com/gist/dabea5dee31d5aeab869e01f65b95f7a), 146 bytes
---
### Data
* **Input** `Int32[,]` `m` The matrix to be exploded
* **Output** `Int32[,]` The exploded matrix
---
[Answer]
# Java 8, ~~183~~ ~~166~~ ~~162~~ ~~129~~ 119 bytes
```
m->{int a=m.length,b=m[0].length,i=a*b,r[][]=new int[a-~a][b-~b];for(;i-->0;)r[i/b*2+1][i%b*2+1]=m[i/b][i%b];return r;}
```
Input and output as a `int[][]`.
-33 bytes by creating a port of [*@auhmaan*'s C# answer](https://codegolf.stackexchange.com/a/128616/52210).  
-10 bytes thanks to *@ceilingcat*.
**Explanation:**
[Try it here.](https://tio.run/##tVDLboMwELznK/ZSCVLjJlEfB4tI/YDmkt6QD2twUlMwyJhUEXJ@nRoo6qWReolkSzuz3tkZ53jCqKqlzrPPXpV1ZSzknqOtVQV9NQbPDVukBTYNvKHS3QJAaSvNAVMJuwGORMITDmkwVxgy33H@@tNYtCqFHWiIoS@jbeefAcYlLaQ@2g8i4jJZ8RmpGJeCmEEn1vJrVMfogjwR0UVwdqhMwFQUbVcsNIl6EMvN/Zon6m4qvJbnRsyZkbY1GgxzPZvM1K0ovJkfT6dKZVD6XMHeGqWPo/Up1P7cWFnSqrW09i1b6GD6DppJWb9X00CgaRrMLr3jrls7F4Zj/D81rnf@p04eHemeyObWW8jLTTc8@xRDkt8cbuH6bw)
```
m->{                             // Method with integer-matrix as both parameter & return
  int a=m.length,                //  Set `a` to the input-height
      b=m[0].length,             //  Set `b` to the input-width
      i=a*b,                     //  Index integer, starting at `a*b`
      r[][]=new int[a-~a][b-~b]; //  Result integer-matrix with dimensions a*a+1 by b*b+1
  for(;i-->0;)                   //  Loop `i` in the range (a*b, 0] (so over each cell):
    r[i/b*2+1][i%b*2+1]=         //   Fill the current cell of the result-matrix:
      m[i/b][i%b];               //    With the correct input-integers
  return r;}                     //  Return result integer-matrix
```
[Answer]
# Dyalog APL, 24 bytes
```
{{⍵↑⍨¯1-⍴⍵}⊃⍪/,/2 2∘↑¨⍵}
```
Any improvements are welcome and wanted!
[Answer]
# [Python 2](https://docs.python.org/2/), 92 bytes
```
n=input()
a=[[0]*(2*len(n[0])+1)for i in[0]+2*n]
for l,v in zip(a[1::2],n):l[1::2]=v
print a
```
[Try it online!](https://tio.run/##JYm9CoAgEIB3n8JRzSGtSfBJjhscig7kkrCgXt6Mtu@n3HXb2bfGkbicVWmRIsCIRnmTF1bcWQ9Or/shSdKngzeM4gvZXj3Jh4pK4ELwaFmH/GO8RDmIq0ytATjbJ0x2RnwB "Python 2 – Try It Online")
[Answer]
# [Python 3](https://docs.python.org/3/) + [Numpy](http://www.numpy.org/), 87 bytes
```
from numpy import*
def f(a):b=zeros((2*len(a)+1,2*len(a[0])+1));b[1::2,1::2]=a;return b
```
[Try it online!](https://tio.run/##LYzLCsMgFETX9StcanoXTfoIGPwSuQulSgP1wa1ZpD9vDXQznDkMU/b6yunaWqAcedpi2fkaS6Y6sKcPPAgrldNfT/kjxDS8fermPMIfzQV7k3JxZlRqgiNQ24V83Shx16w27GT6Hu4InWa4wQMZskJrquL4l@0H "Python 3 – Try It Online")
[Answer]
## JavaScript (ES6), ~~80~~ 78 bytes
```
a=>[...a,...a,a[0]].map((b,i)=>[...b,...b,0].map((_,j)=>i&j&1&&a[i>>1][j>>1]))
```
[Answer]
# Pyth, 13 bytes
```
uCm+0s,R0dG2Q
```
[Demonstration](https://pyth.herokuapp.com/?code=uCm%2B0s%2CR0dG2Q&input=%5B%5B1%2C+2%5D%2C+%5B3%2C+4%5D%5D&debug=0)
Another 13:
```
uC.iL+0m0dG2Q
```
[Answer]
# [APL (Dyalog)](https://www.dyalog.com/), 22 bytes
Prompts for matrix, returns enclosed matrix.
```
{⍺⍀⍵\a}/⍴∘0 1¨1+2×⍴a←⎕
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT///qKPdtaIgJz8l9X/1o95dj3obHvVujUms1X/Uu@VRxwwDBcNDKwy1jQ5PB/ITH7VNeNQ3FaTnv2ZyfkGlQnFiTglXdW1qXmJSTmqwo08IVyxQTVJ@hUJ@noJuWl6xbX4eF5cCGECt4XrUu8oQXahtooahgommhqmCkSaG8k6QDgUTBXNMg8yAwkYA "APL (Dyalog Unicode) – Try It Online")
`a←⎕` prompt for matrix and assign to *a*
`⍴` the dimensions of *a* (rows, columns)
`2×` multiply by two
`1+` add one
`⍴∘0 1¨` use each to **r**eshape (cyclically) the numbers zero and one
`{`…`}/` reduce by inserting the following anonymous function between the two numbers:
 `⍵\a` expand\* the columns of *a* according to the right argument
 `⍺⍀a` expand\* the rows of that according to the left argument
\* 0 inserts a column/row of zeros, 1 inserts an original data column/row
[Answer]
# [J](http://jsoftware.com/), 24 bytes
```
0,.0,[:,./^:2(0,.~,&0)"0
```
[Try it online!](https://tio.run/##y/r/P03B1krBQEfPQCfaSkdPP87KSAPIq9NRM9BUMuDiSk3OyFdIUzBUMFZQAZImCub//wMA "J – Try It Online")
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 22 bytes
```
"vy0ý0.ø})"©.V€Sø®.Vø»
```
[Try it online!](https://tio.run/##MzBNTDJM/f9fqazS4PBeA73DO2o1lQ6t1At71LQm@PCOQ@v0woDk7v//o6MNY3WiTWNjAQ "05AB1E – Try It Online")
---
Actually uses the same piece of code 2x, so I can store it as a string and use Eval for -10 bytes.
]