[Question]
  [
You are given a square matrix of width \$\ge2\$, containing square numbers \$\ge1\$.
Your task is to make all square numbers 'explode' until all of them have disappeared. You must print or return the final matrix.
More specifically:
1. Look for the highest square \$x^2\$ in the matrix.
2. Look for its smallest adjacent neighbor \$n\$ (either horizontally or vertically and without wrapping around).
3. Replace \$x^2\$ with \$x\$ and replace \$n\$ with \$n\times x\$.
Repeat the process from step 1 until there's no square anymore in the matrix.
### Example
Input matrix:
$$\begin{pmatrix}
625 & 36\\
196 & 324
\end{pmatrix}$$
The highest square \$625\$ explodes into two parts of \$\sqrt{625}=25\$ and merges with its smallest neighbor \$36\$, which becomes \$36\times 25=900\$:
$$\begin{pmatrix}
25 & 900\\
196 & 324
\end{pmatrix}$$
The highest square \$900\$ explodes and merges with its smallest neighbor \$25\$:
$$\begin{pmatrix}
750 & 30\\
196 & 324
\end{pmatrix}$$
The highest square \$324\$ explodes and merges with its smallest neighbor \$30\$:
$$\begin{pmatrix}
750 & 540\\
196 & 18
\end{pmatrix}$$
The only remaining square \$196\$ explodes and merges with its smallest neighbor \$18\$:
$$\begin{pmatrix}
750 & 540\\
14 & 252
\end{pmatrix}$$
There's no square anymore, so we're done.
### Rules
* The input matrix is **guaranteed** to have the following properties:
	+ at each step, the highest square will always be unique
	+ at each step, the smallest neighbor of the highest square will always be unique
	+ the sequence will not repeat forever
* The initial matrix may contain \$1\$'s, but you do not have to worry about making \$1\$ explode, as it will never be the highest or the only remaining square.
* I/O can be processed in any reasonable format
* This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'")
### Test cases
```
Input : [[16,9],[4,25]]
Output: [[24,6],[20,5]]
Input : [[9,4],[1,25]]
Output: [[3,12],[5,5]]
Input : [[625,36],[196,324]]
Output: [[750,540],[14,252]]
Input : [[1,9,49],[1,4,1],[36,25,1]]
Output: [[3,6,7],[6,2,7],[6,5,5]]
Input : [[81,4,64],[16,361,64],[169,289,400]]
Output: [[3,5472,8],[624,323,1280],[13,17,20]]
Input : [[36,100,1],[49,144,256],[25,49,81]]
Output: [[6,80,2],[42,120,192],[175,21,189]]
Input : [[256,169,9,225],[36,121,144,81],[9,121,9,36],[400,361,100,9]]
Output: [[384,13,135,15],[24,1573,108,54],[180,11,108,6],[380,209,10,90]]
Input : [[9,361,784,144,484],[121,441,625,49,25],[256,100,36,81,529],[49,4,64,324,16],[25,1,841,196,9]]
Output: [[171,19,700,4032,22],[11,210,525,7,550],[176,60,6,63,23],[140,112,1152,162,368],[5,29,29,14,126]]
```
  
[Answer]
# [R](https://www.r-project.org/), ~~301~~ ~~287~~ ~~277~~ ~~274~~ ~~222~~ ~~217~~ ~~195~~ ~~186~~ ~~178~~ 174 bytes
Nothing particularly creative, including the zero buffering of the peripheral elements of the entry matrix, [an earlier version](https://tio.run/##JY3RasMwDEXf/RXZQ0BulFK3adno9CXGg86h1AFroUmJna3fninbwxWcy@HqvvS0XB/sx/DFkPS3mmi6BX/bxks6t@Q/A7eKaXhESD9Gf2yPavg34CUJlaXRKlxhFQYRZKHI1ML8XsuCLFYGRxDGhLPWElUEmiBvchm4THZw0jDVHgxytReIFCoPjDULdBTtBHW20WmnipmyDe7vyPNztt0KndvMqkjUQ7aMziI7/VTpufQQL@M9JPBwOKHZ7dBg84amaXB/PElWejUaD1ovvw) later improved by Robin:
```
function(x){w=which.max
if(any(s<-!x^.5%%1)){
y=cbind(NA,rbind(NA,x,NA),NA)
z=y[i]=y[i<-w(y*y%in%x[s])]^.5
m=i+c(r<--c(1,nrow(y)),-r)
y[j]=y[j<-m[w(-y[m])]]*z
x=p(y[r,r])}
x}
```
[Try it on-line](https://tio.run/##NYxBCoMwFET3OUW7EP63iaCtQsEsegEvEFKwKWKEpJK2mCiePdVFYWaYxbxxceSx@1r10S8LHpeJT71WfWZaT3QHrQ3wrtnR37MySXLEhQSuHto@oblR9y@eNjfcTWYehJZ71GyCkIZE28SLt0S5XRDD9UmBqxlTkFPrXtsGkTKHJIhh54aaGTEBC8JsjExn4vkIQTjqJK7Er3EE036c9qCgKkqaXyt63lRckB4KxPgD)
Using a sequence of numbers as its entry, and hence removing the call to a function, Nick Kennedy earlier managed [a 186 bytes version](https://tio.run/##FU5basMwELyK@mHYTawQJXZIKu8BegahEtd5WHG0kNhgiVJf3ZVhZ2AeDPue55HG1jXtxtdBn6czNT@OLzpQ39QMqFP4vELNEfpKfoTvTZllCvE3EnT0xTecBugmXw9vFyDkXMnnle9DCwFTF6cunXY0QlzFzHEWTG9Re3LrBqTKJTDxeoe5yhl1NA9LiSrpzQgyGm/R2hVE4xbf2WUy/RbNq5L7T04DL/unw7w/CLXdCiWKk1BFIXblIWFRRzX/Aw) of the algorithm as follows (with [-10 bytes by Robin](https://tio.run/##FU5LCsIwFLxKXBTes40YrUVJs/ACXiBEWqPYSJNFWmiC2KvXdDED82EYvyyTmDqju51tA2/mRuiHcU8exKBbB8hT2L@gdRGGmm7CfXfKMob4jeJ2nUdIZNvRmwChcDXtX@49dhAw9XC@XVdwIyaI25gZlwU5KORWmFyDrynVwAqXH7CgHnmUHyUS1dTKCWiUVqFSW4jSrL5R62h6FqUvvPrxsBwrwvZ7wkh5IawsyeFUJazqzJY/)):
```
w=which.max;`~`=cbind;x=scan();while(any(s<-!x^.5%%1)){y=NA~t(NA~matrix(x,n<-length(x)^.5)~NA)~NA;i=w(y*y%in%x[s]);=i+c(r<--c(1,n+2),-r);y[j]=y[j<-m[w(-y[m])]]*(y[i]=y[i]^.5);x=y[r,r]};x
```
avoiding the definition of a (recursive) function, plus other nice gains.
[Try it on-line](https://tio.run/##FU5LCsIwFLxKXBTes40YrUVJs/ACXiBEWqPYSJNFWmiC2KvXdDED82EYvyyTmDqju51tA2/mRuiHcU8exKBbB8hT2L@gdRGGmm7CfXfKMob4jeJ2nUdIZNvRmwChcDXtX@49dhAw9XC@XVdwIyaI25gZlwU5KORWmFyDrynVwAqXH7CgHnmUHyUS1dTKCWiUVqFSW4jSrL5R62h6FqUvvPrxsBwrwvZ7wkh5IawsyeFUJazqzJY/)
[Answer]
# [Ruby](https://www.ruby-lang.org/), ~~140~~ 135 bytes
Takes a flat list as input, outputs a flat list.
```
->m{i=1;(i=m.index m.reject{|e|e**0.5%1>0}.max
m[i+[1,-1,l=m.size**0.5,-l].min_by{|j|i+j>=0&&m[i+j]||m.max}]*=m[i]**=0.5if i)while i;m}
```
[Try it online!](https://tio.run/##JY7dbsIwDEbv@xTZxRAUEzUl7dqh8CJVhPhxN1dJiyBsMMqzdy74zuc7n@zTZXcbajMs1v5ORq2mZLyk9oBX4eUJG9yHe489xnEis3e1Th7Sb6@Rr2heKVgocOyf6e8lwMJZ6and7G73vulp3qxNMpmMdmP73o/dh40NAxvHhhtUC5r9fpNDQSv/GKg9XoIwAn@2TnxhOEfdc6@rZyJrtw0BWxs5bBm/4Hh/BDKQx7M4dJHgOYpuzBzt8W2agGBhxg8cp5PP0G1oFmF7GKqqhGWu4KPQoLQGXWgLlUoVaK0gTzPQJaQZszTLQSUJ21AoyNKSGWcacg3LlNv5UwIFBTdVmUNp7T8 "Ruby – Try It Online")
Explanation:
```
->m{                                # Anonymous lambda
    i=1;                            # Initialize i for the while loop
        (                           # Start while loop
i=m.index                           # Get index at...
    m.reject{|e|          }         # Get all elements of m, except the ones with...
                e**0.5%1>0          # a square root with a fractional component
                           .max     # Get the largest of these
m[i+                                # Get item at...
    [1,-1,l=m.size**0.5,-l]         # Get possible neighbors (up, down, left, right)
        .min_by{|j|i+j>=0&&m[i+j]|| # Find the one with the minimum value at neighbor
                            m.max}  # If out of range, return matrix max so
                                    #   neighbor isn't chosen
    ]
    *=m[i]**=0.5                    # Max square becomes its square root, then multiply
                                    #   min neighbor by it
)while i                            # End while loop. Terminate when index is nil.
m}                                  # Return matrix.
```
[Answer]
# [Python 2](https://docs.python.org/2/), 188 bytes
```
M=input()
l=int(len(M)**.5)
try:
 while 1:m=M.index(max(i**.5%1or i for i in M));_,n=min((M[m+i],m+i)for i in m/l*[-l]+-~m%l*[1]+[l][:m/l<l-1]+m%l*[-1]);M[m]**=.5;M[n]*=M[m]
except:print M
```
[Try it online!](https://tio.run/##PU5LbsMgEN37FGwiAcZuIJjaTn0EToBQVTVUQQJiWURxNr26OzhSF/Pmzfsg5me@3pLYNj35NN8zJlUAlnFwCWtCaduRKi/PsUKPqw8O8TFOuvXp4lYcv1bsS@TAbwvy6GdHn5Am5PzJ0hR9wlibWHvLAMh/IL4Fappg6@Y3HoByW5tgzQj6R2jg2lUg5Ax1S@nUdsCSpVO5K7d@uzmP8wJfRXrbzMDQSXGG3nvJEJcAcmcCNCkBlOiAQaxs0SnwjsdSYqgHuxPDy4aSgjmJ0lavOPh9eYMPIAz2Dw "Python 2 – Try It Online")
Full program. Takes input and prints as a flat list.
[Answer]
# [Perl 6](https://github.com/nxadm/rakudo-pkg), 236 bytes
```
{my@k=.flat;my \n=$_;loop {my (\i,\j)=@k>>.sqrt.grep({$_+|0==$_},:kv).rotor(2).max(*[1]);last if 0>i;$/=((0,1),(0,-1),(1,0),(-1,0)).map({$!=i+n*.[0]+.[1];+$!,n>.[0]+i/n&.[1]+i%n>=0??@k[$!]!!Inf}).min(*[1]);@k[i,$0]=j,j*$1};@k.rotor(+n)}
```
[Try it online!](https://tio.run/##RY7LboMwEEX3/QojuZENjmNDQE0tE7b9hoAiFqEyD0MBVUUp307HUaRuZubcO6/hNrbJ1i1oV@nt3i1Zo3nVlrMCKbcaX1Xb9wMCB5HcsLymOmvSlE9f48w/x9tA7vga/AoNrSt7b74pH/u5H0lIeVf@EP8iC6racpqRqZBIjcIHTYhgkjKIe5ckExD3Lrkht9LTJrA@v4gi4LBBBdhjNn2wOdid0wLzalMtzuesuWCv8LwPW60wb@zzKOiGYVHomtU@lisIz98CS9dNvUzlgipESBLGDEUJZYjIUwJleKT0347fIvDDGBwZxq7LcQQkxIPAODJ0AqJq@wM "Perl 6 – Try It Online")
[Answer]
# [MATL](https://github.com/lmendo/MATL), ~~49~~ 48 bytes
```
`ttX^tt1\~*X>X>XJt?wy=(tt5M1Y6Z+*tXzX<=*Jq*+w}**
```
[Try it online!](https://tio.run/##FYu7CgIxEEV/5ZYy2@zMTsaEuNov2Mcn2mshDCwK@usxy@VU95zn3R@13tzL1Z3PPyrbtsl383tcuYc9H@zYkZdP2Yw0vaibv0S1nhIGY6yjglWhUTNYGKoMkwBNkJAbBu775iIygqS8PApTDNJSWxQwYss4GdLlDw) Or [verify all test cases](https://tio.run/##LU89bwIxDN37K7yBAkjnnOMmOmiXTkjdOqTAVXQvA1IkoFL716/Ppsri57wvnz7b13Scjq3Vj9b48BvqE962PV9um3lr6ZXfdbcIrX7X9SZsz2Fx@QlhGsfZajWrL7vr27RnpTKQUEzjw76QDMT3WWNa9gpYdNlHwYYJ/8UIQjz0Ch4x9tkWakqlXvl/LBQz@F0HBrjcdRBBTyyWBmfIAbNZALsEopgGMn5kZ2aIiqNCVgeGHmJ@xSsbeszibMmWDbIIetwDzND9XQlDSrF4E2tNuA3R3oYpQ4aD4fwH).
### How it works
```
`           % Do...while
  tt        %   Duplicate twice. Takes a matrix as input (implicit) the first time
  X^        %   Square root of each matrix entry
  tt        %   Duplicate twice
  1\~       %   Modulo 1, negate. Gives true for integer numbers, false otherwise
  *         %   Multiply, element-wise. This changes non-integers into zero
  X>X>      %   Maximum of matrix. Gives maximum integer square root, or zero
  XJ        %   Copy into clipboard J
  t         %   Duplicate
  ?         %   If non-zero
    wy      %     Swap, duplicate from below. Moves the true-false matrix to top
    =       %     Equals, element-wise. This gives a matrix which is true at the
            %     position of the maximum that was previously identified, and
            %     false otherwise
    (       %     Write the largest integer square root into that position
    tt      %     Duplicate twice
    5M      %     Push again the matrix which is true for the position of maximum
    1Y6     %     Push matrix [0 1 0; 1 0 1; 0 1 0] (von Neumann neighbourhood)
    Z+      %     2D convolution, keeping size. Gives a matrix which is 1 for the
            %     neighbours of the value that was replaced by its square root
    *       %     Multiply. This replaces the value 1 by the actual values of
            %     the neighbours
    t       %     Duplicate
    XzX<    %     Minimum of non-zero entries
    =       %     Equals, element-wise. This gives a matrix which is true at the
            %     position of the maximum neighbour, and zero otherwise
    *       %     Multiply, element-wise. This gives a matrix which contains the
            %     maximum neighbour, and has all other entries equal to zero
    J       %     Push the maximum integer root, which was previously stored
    q       %     Subtract 1
    *       %     Multiply element-wise. This gives a matrix which contains the
            %     maximum neighbour times (maximum integer root minus 1)
    +       %     Add. This replaces the maximum neighbour by the desired value,
            %     that is, the previously found maximum integer square root
            %     times the neighbour value
    w       %     Swap
  }         %   Else. This means there was no integer square root, so no more
            %   iterations are neeeded
  **        %   Multiply element-wise twice. Right before this the top of the
            %   stack contains a zero. Below there are the latest matrix with
            %   square roots and two copies of the latest matrix of integers,
            %   one of which needs to be displayed as final result. The two
            %   multiplications leave the stack containing a matrix of zeros
            %   and the final result below
            % End (implicit). The top of the stack is consumed. It may be a
            % positive number, which is truthy, or a matrix of zeros, which is
            % falsy. If truthy a new iteration is run. If falsy the loop exits
            % Display (implicit)
```
[Answer]
# JavaScript (ES6), ~~271~~ ~~259~~ ~~250~~ 245 bytes
```
m=>{for(l=m.length;I=J=Q=-1;){for(i=0;i<l;i++)for(j=0;j<l;j++)!((q=m[i][j]**.5)%1)&&q>Q&&(I=i,J=j,Q=q);if(I<0)break;d=[[I-1,J],[I+1,J],[I,J-1],[I,J+1]];D=d.map(([x,y])=>(m[x]||0)[y]||1/0);[x,y]=d[D.indexOf(Math.min(...D))];m[x][y]*=Q;m[I][J]=Q}}
```
Thanks to [Luis felipe De jesus Munoz](https://codegolf.stackexchange.com/users/78039/luis-felipe-de-jesus-munoz) for −14 bytes!
Explanation:
```
m => { // m = input matrix
  // l = side length of square matrix
  // I, J = i, j of largest square in matrix (initialized to -1 every iteration)
  // Q = square root of largest square in matrix
  for (l = m.length; (I = J = Q = -1); ) {
    // for each row,
    for (i = 0; i < l; i++)
      // for each column,
      for (j = 0; j < l; j++)
        // if sqrt of m[i][j] (assigned to q) has no decimal part,
        // (i.e. if m[i][j] is a perfect square and q is its square root,)
        !((q = m[i][j] ** 0.5) % 1) &&
          // and if this q is greater than any previously seen q this iteration,
          q > Q &&
          // assign this element to be the largest square in matrix.
          ((I = i), (J = j), (Q = q));
    // if we did not find a largest square in matrix, break loop.
    if (I < 0) break;
    // d = [i, j] pairs for each neighbor of largest square in matrix
    d = [[I - 1, J], [I + 1, J], [I, J - 1], [I, J + 1]];
    // D = value for each neighbor in d, or Infinity if value does not exist
    D = d.map(([x, y]) => (m[x] || 0)[y] || 1 / 0);
    // x = i, y = j of smallest adjacent neighbor of largest square
    [x, y] = d[D.indexOf(Math.min(...D))];
    // multiply smallest adjacent neighbor by square root of largest square
    m[x][y] *= Q;
    // set largest square to its square root
    m[I][J] = Q;
  } // repeat until no remaining squares in matrix
  // no return necessary; input matrix is modified.
};
```
[Answer]
# [C# (Visual C# Interactive Compiler)](http://www.mono-project.com/docs/about-mono/releases/5.0.0/#csc), 220 bytes
```
n=>l=>{for(int g;n.Any(x=>Math.Sqrt(x)%1==0);n[n.Select((a,b)=>(x:Math.Abs(b/l-g/l)+Math.Abs(b%l-g%l)==1?a:1<<30,y:b)).OrderBy(x=>x).First().y]*=n[g])n[g=n.IndexOf(n.Max(x=>Math.Sqrt(x)%1==0?x:0))]=(int)Math.Sqrt(n[g]);}
```
[Try it online!](https://tio.run/##bVJRb5swEH7nV3hIlezVdXFCGYSYKpvUqVOrVk2lPSAeSOqkaMgwQ1aSKL89O5tmmaK@mLvv7vvuu0vmzcW8KfY3KzUf3xVNOy5Um9DJvC0qZeNkIfZKJKVItotKY4DQMlZsota4E8l93r6y6W/d4o6ccSE8EqtUsaks5bzFOKczIhLcjWzfZNbg2WV5sbwsyfkROQPkrCRC8Ot8xMfjoUfXoxkh7EG/SP3VzukIuyl002LC1tlnodJlRuARit2qF9k9LLBi93n3oaXrbuQRkgnjnRyrViPe7WPn7bUoJb5V7FHKX5igTwJdcIK2DkJ/co02SKAnmb/cFUpiEgP6UxettKl7q@pVi0Zo6@1cija2jA2rpuYtCZCnrS7U8rmaaJ2vcd@zwDXB5anaw6oFuYOam7rnjeWyH1WhsEtdWp/cVsm3LUQ7wr7ralW/X4vBmcmhE4CPlLpDfSmSJcsJOXcz9/2NHWfnOPj4h0DmdieL9IJQAcv9tWwo7MueZF3mc9gphWEuOeaZzeP/jtsTpnVZtMbZP@Mwkz3mupGEPVfGS399LduVVghvKDr5STfsW7UCBNp2@zTlAY0ymvp0cJVlTppG1IeUH9JgcEWHgUGigA4HvgU5ha7ItvmUw3cYQD9EphgaMLAiwAj4IY7oIASa59kuYHDPs2Q/otw3880YkIE87KUAooYIVLBjx/ABt92hYUY2jXqDoGzHGdnofRWTfwl9y/BD6wMIvg@m@kFW1o6xbJClV4OoN2W2MCuDhd4YpyEwzSFA/y8 "C# (Visual C# Interactive Compiler) – Try It Online")
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), 224 bytes
```
(l=#;While[(c=Length)[m=Select[Join@@l,IntegerQ[Sqrt@#]&]]>0,t=##&@@#&@@SortBy[Select[(g=#&@@Position[l,f=Max@m])+#&/@{{1,0},{0,1},{-1,0},{0,-1}},Min@#>0&&Max@#<=c@l&],l[[##]]&@@#&];l[[##&@@g]]=(n=Sqrt@f);l[[t]]=l[[t]]n];l)&
```
[Try it online!](https://tio.run/##NY5Ba8QgEIX/SkGQhM5SdY1N2LpIby1d2JLDHsRDCK4bMIamHlpCfntq0vYwM8437/Hsm3izfRO7tlmcXDIv0eFy67zVWSvfbHDxlute1tbbNurXoQtKeXgJ0To7vuv6Y4wKGWzMkUCUCGGl1qqHMT5/6z9b5uQKz8NnF7shaA9XeWq@VG/ye4Qf1DRRIDNMBGjqu/9lR@cZTikRHQnGqwE9yVZ5bMBrjZAxW5o5bFt6O2NkFuT2qWu@4pjI7whJluPlPHYh3imnp6mCvaDwWHKgnAMveQqljALnFAQrgFfAisRYIYASktRQUihYlVi6cRAc9iy5xSYCCmVy0kpANc9mWX4A "Wolfram Language (Mathematica) – Try It Online")
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), 157 bytes
```
a=>g=(l,m=n=i=j=0)=>a.map((o,k)=>m>o||o**.5%1||[m=o,i=k])|m&&a.map((o,k)=>n*n<o*n|((i/l|0)-(k/l|0))**2+(i%l-k%l)**2-1||[n=o,j=k])|[a[i]=m**=.5,a[j]=m*n]|g(l)
```
[Try it online!](https://tio.run/##VVDLboMwELznK7gkselCMTEulur8Qb8AoQilhAJ@RA3qyf9O166iJifvzs7Mznrqfrrb@Xu8Lpl1n/16UWunjoMiGoyyalSTKqg6drnproQ4mLExR@e9S9O82jLvG6McjGpuqTe73RPRpvbdpdYTMr5qX9CMzPGlaVq@kHGrs3mrQ5MFH4s@U/RpumZslUlTlVfQNVOobesHoum69LclUYlJ1DEhOlS57u2wfEHAsLvobvnADKfAOFFILsRQVEJydvbmdJ9rNxCTT260ZA/JnlK62QRb0jRMgGyh4VBWbUvvqASOIHsGRVnBQQRcCjiU/GHEABUySjgwfA8CtVj9U@owEtEW1YLdawlljeKieOCWlYAwwRkmiG6sZMA4hzq4y9jKvzQojX4MX/l0Q0Dfah51vI7rUMY57sZwXEI0j8uiB5pDVcbvkDFsuBKDRBIwqFEZbg9b1l8 "JavaScript (Node.js) – Try It Online")
-14 bytes thanks the @Arnauld who also wrote a nice test harness :)
Anonymous function that takes a 1-dimensional array as input and a length parameter specifying number if columns/rows.
Curried input is specified as `f(array)(length)`.
```
// a: 1-dimensional array of values
// g: recursive function that explodes once per recursive call
// l: number of columns, user specified
// m: max square value
// n: min neighbor
// i: index of max square
// j: index of min neighbor
a=>g=(l,m=n=i=j=0)=>
  // use .map() to iterate and find largest square
  a.map((o,k)=>
    // check size of element
    m>o||
    // check if element is a square
    o**.5%1||
    // new max square found, update local variables
    [m=o,i=k])|
  // after first .map() is complete, continue iff a square is found
  // run .map() again to find smallest neighbor
  m&&a.map((o,k)=>
    // check size of element
    n*n<o*n|
    // check relative position of element
    ((i/l|0)-(k/l|0))**2+(i%l-k%l)**2-1||
    // a new smallest neighbor found, update local variables
    [n=o,j=k])|
  // update matrix in-place, largest square is reduced,
  // smallest neighbor is increased
  [a[i]=m**=.5,a[j]=m*n]|
  // make recursive call to explode again
  g(l)
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~70~~ 67 bytes
```
’dL½$}©+Ø.,U$;N$¤%®‘¤<®Ạ$Ƈæ.®‘ịÐṂḢ;ḷ;€ị½¥×÷ƭ⁹Ḣ¤¦Ṫ}¥ƒ
×Æ²$MḢçɗ⁸Ẹ?ƊÐL
```
[Try it online!](https://tio.run/##PZCxS8NAFMb3/B1x8lFy6TXmiOBml@rmICGbLtJZECwkHSzoUlxaQUWkrSjaoUObVLq8g4B/xt0/Et9drFPuffd@3/ddLs673auq0unDWQc3bg/fd@W4ASdudOziZAfnOh3jZB/nav3ilgM5a1hJfd/JoSr6Kn@NVL6KdP@TJNzgVI7kqvzSWUFXOMGZKj56OC3vHbq4wYV7RLp8@xnpLFfr/KC8lcNO1Y50@ojLa1xe4lKnT9GhHKj8WRVZuWi7jhyQv8nQ2eq0qmInjlkAIoGYg99KEiBBAKeZ/c@B34JmYCQRQNPntcqA9oRd5MDo2wyIoJO9DY0aWB9iArY9C/BD4jyvXiOGeZ7FuQDGTQmTREY0h39mpIFBCaZONon5zK6HBhV2FHVJ8raBxldsH2SEvZBbhIe2ChGcU686yvraHIuTL7R8UdcyDzHvpg51NQYhkeZvUICT/AI "Jelly – Try It Online")
I’m sure this can be done much more briefly, but I found this harder than it first appeared. Explanation to follow once I’ve tried to golf it better. 
A full program that takes a list of integers corresponding to the square matrix and returns a list of integers representing the final exploded matrix.l
[Answer]
# Java 8, ~~299~~ ~~297~~ ~~289~~ 279 bytes
```
m->{for(int l=m.length,i,j,I,J,d,M,t,x,y;;m[x][y]*=d){for(i=l*l,I=J=d=0;i-->0;)d=Math.sqrt(t=m[i/l][i%l])%1==0&t>d*d?(int)Math.sqrt(m[I=i/l][J=i%l]):d;if(d<1)break;for(M=-1>>>1,m[x=I][y=J]=d,t=4;t-->0;)try{M=m[i=I+t/2-t/3*2][j=t<1?J-1:1/t+J]<M?m[x=i][y=j]:M;}finally{continue;}}}
```
-18 bytes thanks to *@ceilingcat*.
Modifies the input-matrix instead of returning a new one to save bytes.
[Try it online.](https://tio.run/##hVRRb6JAEH7vr9iY9AK6KIuUE@naZ03oXdJHwsNWsF0L6OHo1RB@uzeLWpuebE0QWGa@75tvZncpdsJaJm@HeSY2GxIKWVQ3hMgC0nIh5il5VK@E7FYyIXMD16M4ikluBrhc3@DfBgTIOXkkBeHkkFuTarEqVSDJeN7P0uIFXqmkSzqlM5rQkAJ9p/sgyKP3ONrHXZ6YxwyedTM65TOecDuQljWxAzPhoYDX/uZPCQbwPJKDLI7kbRabt4xz@wdMkm7yoNjMS2AeTXkTOONN6DgJ5MJI7pn5XKbiLVBsIbfYZDJhFGXwKQrhs5gnFLgbwJEayn0VKko@7cHAsWAw7DpxtORwzx5mFhuzAfRm8X34oCCkgljG4zCoF7IQWbav5qsCZLFNg7quD4Gyar19ztCqk2ONpTkabjxBKYsXtFWYR7ch3YBRpH/Jye@qYh4lfk2br//9KuJS566um6ZcyyY@JW57NtNne84dJUOvNZ/5Hh06ro6foXrq@joJWANhrQFDjyoVTEcyQhCF4ulKRake08Ywz6fOCOXato4NFTHbRkamaYtPmat6024eUWVhHKrXsCECVboI2uhgs1rRlCqHNawKsTUOcVScuutaix40fh0r9XV@IJKK/DlyG3Z3pHEYmV2XUe9cu6aipnLFjpWpDt85vtbv0wSogcRx@cZ3HLsR6lADfKnu86nW7NGm2MvJd9yiT/sNpHl/tYX@GncvZIXRmRbrLYxJ52TSukwB9r/VVyM/rRX9@cfzNYhfW/gW40redeGfk7/qV4fgTpREjM8rl7XdWJgftn1lMzqk0y/TdSrAcC1j1@t0zNMxb5hmD9/pWXy7VCVWXfXhHw)
**Explanation:**
```
m->{                          // Method with integer-matrix input and no return-type
  for(int l=m.length,         //  Dimension-length `l` of the matrix
      i,j,I,J,d,M,t,x,y;      //  Temp integers
      ;                       //  Loop indefinitely:
       m[x][y]*=d){           //    After every iteration: multiply `x,y`'s value with `d`
    for(I=J=d=0,              //   (Re)set `I`, `J`, and `d` all to 0
        i=l*l;i-->0;)         //   Loop over all the cells:
      d=                      //    Set `d` to:
        Math.sqrt(t=m[i/l][i%l])%1==0?
                              //     If the current value is a perfect square
        *t>d*d?               //     And if it's larger than `d` squared:
          (int)Math.sqrt(m[I=i/l][J=i%l])
                              //      Set `I,J` to the current coordinate
                              //      And `d` to the square-root of the current value
        :d;                   //     Else: leave `d` the same
    if(d<1)                   //   If `d` is still 0 after the nested loop
                              //   (which means there are no more square-numbers)
      break;                  //    Stop the infinite loop
    for(M=-1>>>1,             //   (Re)set `M` to Integer.MAX_VALUE
        m[x=I][y=J]=d,        //   Replace the value at `I,J` with `d`
        t=4;t-->0;)           //   Loop `t` in the range (4, 0]:
      try{M=                  //    Set `M` to:
            m[i=I+t/2-t/3*2]  //     If `t` is 3:
                              //      Go to the row above
                              //     Else-if `t` is 2:
                              //      Go to the row below
                              //     Else (`t` is 0 or 1):
                              //      Stay in the current row
                              //     (and save this row in `i`)
             [j=t<1?J-1:1/t+J]//     If `t` is 0:
                              //      Go to the column left
                              //     Else-if `t` is 1:
                              //      Go to the column right
                              //     Else (`t` is 2 or 3):
                              //      Stay in the current column
                              //     (and save this column in `j`)
             <M?              //     And if the value in this cell is smaller than `M`:
                m[x=i][y=j]   //      Set `x,y` to `i,j`
                              //      And `M` to the current value in `i,j`
               :M;            //     Else: leave `M` the same
      }finally{continue;}}}   //    Catch and ignore IndexOutOfBoundsExceptions
```
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), 141 bytes
```
f=(a,I,J,r)=>a.map((x,i)=>x.map((y,j)=>(1+J?(i-I)**2+(j-J)**2-1|r<y:y**.5%1||r>y)||(P=i,Q=j,r=y)))|1+J?f(a,a[P][Q]*=a[I][J]**=.5):r&&f(a,P,Q)
```
[Try it online!](https://tio.run/##VVBNi8IwEL37K3pZSeK0mJhmG9m4Z3vSc8gh@LG0WCutLBb6391JRFahkHnvzXsz09r/@n7XVZdrem73h/vR4Ec8rKGEjpqVzxp/IeQGFYLbAwxQIyB8Vn6TKl1TxsSM1GkZipSP3dewHBjL8g8@jt1qoONINqaCramhMwOldAzWI07xduPs1jHj7drZ0jFmspwuu@k0qBvY0vv10F8TkzSJWSXkSBoKya499@3pkJ3aH8R0Mgk9xFquQDuwEkTuHH2yGiSS/J1UIoeFCrxWsBDyReKADh0tEji@C4VerP5biiCpGItuxZ@1BlGgeT5/6RW5gqCghhvENC44cCmhCOk6Qv3YBq0xj@Or324I7Gcho08WcRzapMTZuJzUEMPjsJiB4ZCL@Dt0XDZciYvEJuBQoDPcHqbc/wA "JavaScript (Node.js) – Try It Online")
Take input 2D array
]