# [R][1], <s>301</s> <s>287</s> <s>277</s> <s>274</s> <s>222</s> <s>217</s> <s>195</s> <s>186</s> <s>178</s> 173 bytes
Nothing particularly creative, including the zero buffering of the peripheral elements of the entry matrix, [an earlier version][2] 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)
z=y[i]=y[i]^.5;y[j]=y[j<-m[w(-y[m])]]*z
x=p(y[r,r])}
x}
[Try it on-line][5]
Using a sequence of numbers as its entry, and hence removing the call to a function, Nick Kennedy earlier managed [a 186 bytes version][3] of the algorithm as follows (with [-10 bytes by Robin][4]):
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][4]
[1]: https://www.r-project.org/
[2]: https://tio.run/##JY3RasMwDEXf/RXZQ0BulFK3adno9CXGg86h1AFroUmJna3fninbwxWcy@HqvvS0XB/sx/DFkPS3mmi6BX/bxks6t@Q/A7eKaXhESD9Gf2yPavg34CUJlaXRKlxhFQYRZKHI1ML8XsuCLFYGRxDGhLPWElUEmiBvchm4THZw0jDVHgxytReIFCoPjDULdBTtBHW20WmnipmyDe7vyPNztt0KndvMqkjUQ7aMziI7/VTpufQQL@M9JPBwOKHZ7dBg84amaXB/PElWejUaD1ovvw
[3]: https://tio.run/##FU5basMwELyK@mHYTawQJXZIKu8BegahEtd5WHG0kNhgiVJf3ZVhZ2AeDPue55HG1jXtxtdBn6czNT@OLzpQ39QMqFP4vELNEfpKfoTvTZllCvE3EnT0xTecBugmXw9vFyDkXMnnle9DCwFTF6cunXY0QlzFzHEWTG9Re3LrBqTKJTDxeoe5yhl1NA9LiSrpzQgyGm/R2hVE4xbf2WUy/RbNq5L7T04DL/unw7w/CLXdCiWKk1BFIXblIWFRRzX/Aw
[4]: https://tio.run/##FU5LCsIwFLxKXBTes40YrUVJs/ACXiBEWqPYSJNFWmiC2KvXdDED82EYvyyTmDqju51tA2/mRuiHcU8exKBbB8hT2L@gdRGGmm7CfXfKMob4jeJ2nUdIZNvRmwChcDXtX@49dhAw9XC@XVdwIyaI25gZlwU5KORWmFyDrynVwAqXH7CgHnmUHyUS1dTKCWiUVqFSW4jSrL5R62h6FqUvvPrxsBwrwvZ7wkh5IawsyeFUJazqzJY/
[5]: https://tio.run/##NY1dCsIwEITfcwp9KOxqIrT@gNg89AJeIESoEekKiSWtNGnp2WsrCDPDNzAwfqrl9Pw409LbQcChk11FptrZMrBGrsNtd0ySlNETShehQRxYlOZO7gHXgvs/BH4tcDEj2UHcxIRcElSjkVlJWwM@F8JAyl0unH/PE0TukfUyKtK/mJ8uUb2W8sqFVR2IqKxGrTc9C7KGqDz3GkcWxqkGW7aeAhg4ZUeenk98Pys7IF9liNMX