[Question]
  [
In math a [magic square](https://en.wikipedia.org/wiki/Magic_square) is an N×N grid of numbers from 1 to N2 such that every row, column, and diagonal sums to the same total. For example here's a 3×3 magic square:
[![Wikipedia magic square example](https://i.stack.imgur.com/b3C10.png)](https://en.wikipedia.org/wiki/File:Magicsquareexample.svg)
In this challenge we'll extend the idea to magic *code* squares where, instead of numbers, each grid cell is any non-newline character. They can be be repeated and in any order.
And instead of sums, each row, column, and diagonal is viewed as an N-character single-line program whose output is a distinct number from 0 to 2N+1 (inclusive).
For example, if you had a 4×4 magic code square that looked like
```
ABCD
EFGH
IJKL
MNOP
```
it would need to output the numbers 0 through 9, perhaps like:
```
  ABCD-9
  EFGH-2
  IJKL-6
  MNOP-5
 /||||\
3 4180 7
```
That is, the ten embedded 4-character single-line programs that form the rows, columns, and diagonals of the grid each need to output a unique number from 0 through 9. In this case:
```
ABCD -> 9
EFGH -> 2
IJKL -> 6
MNOP -> 5
AFKP -> 7
DHLP -> 0
CGKO -> 8
BFJN -> 1
AEIM -> 4
DGJM -> 3
```
(Note that the diagonals are read from top to bottom. This is required.)
As another example, this 3×3 code square
```
d*P
0##
! d
```
would need to output 0 through 7 when its rows, column, and diagonals are run to make it magical, perhaps like:
```
  d*P-1
  0##-0
  ! d-6
 /|||\
5 432 7
```
# Scoring
The goal of this challenge is not necessarily to make the smallest or the biggest magic code square. In some programming languages very small magic code squares might be easy to make and in other languages very large ones might be.
**The challenge here is to create what you think is the best, most elegant, or most impressive magic code square or related set of magic code squares.** Hence this is a [popularity-contest](/questions/tagged/popularity-contest "show questions tagged 'popularity-contest'"), the answer with the highest number of votes wins.
* Your submission must include at least one valid magic code square, and may include many related ones if you have found them, or even an infinite neighborhood of them.
* Please list the N (or range of N's) you found at the top of your answer.
* For submitting multiple magic code squares in the same language but with very different implementations use multiple answers.
* Of course you are encouraged to share your process of searching for magic code squares and explain why yours is interesting! (Info about any [Parker](https://knowyourmeme.com/memes/the-parker-square) magic code squares might be funny.)
  
[Answer]
# [Keg](https://github.com/JonoCode9374/Keg), 4x4 magic square
```
0601
7.92
3._.
8654
```
[Try it online!](https://tio.run/##y05N///fwMzAkMtcz9KIy1gvXo/LwszU5P9/AA "Keg – Try It Online")
## Outputs
```
 0601 - 1
 7.92 - 7
 3._. - 3
 8654 - 4
/||||\
98652 0
```
I can't believe this actually works! Programs need the `-hr` flag to properly output the required result. 
[Test suite. Uncomment each line to run it. The `-in` flag is purely to make the test suite a nicer experience, as otherwise, the newlines would interfere with the programs.](https://tio.run/##Dce7DYAwDEDB3mu8Ola@TjJNKgQIiYL9JZPr7jlOd6LFRBK6zkwXii6lCMNapQqxl8EQTNWw/bkaTUhZK3lfVyXuTx1Mdw/X5@F@fw "Keg – Try It Online")
## Programs
```
0601 - 1 (Push 0, 6, 0 and 1,then print the top of stack (TOS)) 
7.92 - 7 (Push 7 and print it. This makes the rest of the program a NOP) 
3._. - 3 (Push 3 and print it. The _ will the try and pop from an empty stack, ending execution) 
8654 - 4 (Push 8, 6, 5 and 4 and then print TOS) 
0738 - 8 (Push 0, 7, 3 and 8 and then print TOS) 
6..6 - 6 (Push 6 and print it. The . will try and print from an empty stack, end execution) 
09_5 - 5 (Push 0 and 9, then pop the 9 and discard it then push 5. TOS is then printed) 
12.4 - 2 (Push 1, then 2 and print TOS. The rest of the program is NOPs after that.) 
0._4 - 0 (Like the program for 3, but with 0)
19.8 - 9 (Like the program for 2 but with 9)
```
[Answer]
# Polyglot, 10x10
```
0     14  
      5   
    16    
    7     
        18
        9 
20        
 10       
 12       
  13      
```
This yields the following row/column/diagonal programs (one per line):
```
0         
       01 
0     2   
         3
4         
      5   
  6       
    7     
    8     
        9 
 10       
      011 
 12       
  13      
0     14  
15        
    16    
  17      
        18
    19    
20        
       021
```
In many stack-based languages, including 05AB1E and Jelly, each of those programs outputs the corresponding integer.
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), ~~4x4~~ 5x5
```
8www4
w3Tαw
Uw₃4<
XθнĀ·
wTwºß
```
[Try it online.](https://tio.run/##yy9OTMpM/a/036K8vNyEq9w45NzGcq7Q8kdNzSY2XBHndlzYe6Th0Hau8pDyQ7sOz/@vdGjZoRV6TmFcSh75RZlV@XkliTlWCko6CjAQCSLKKu2VFB61TVJQsq/UC9PRjKnlUgpLLSrJTEZTDVT/qGlN8OEdXti0@CZm5imkZCam5@ch64NoaY3xcoFrAeuAakjMK8nURdEF1aCPquE/AA) (Loops over the rows; columns; main diagonal; and main anti-diagonal of the square, and will run it as 05AB1E code with `.V`, after which it will print the result and discard everything that remains on the stack for the next iteration with `)\`.)
### Explanation:
The `w` are no-ops, so I'll ignore them in the explanations below. In addition, the top of the stack is output implicitly for all of these programs as result.
**Horizontal:**
* 4. `8 4`: push \$8\$; push \$4\$.
* 7. `3Tα`: push \$3\$; push \$10\$; take the absolute difference between the two: \$\lvert3-10\rvert\$.
* 3. `U₃4<`: pop and store in variable `X` (stores an empty string in `X` with an empty stack); push \$95\$; push \$4\$; decrease the top by 1: \$4-1\$.
* 2. `XθнĀ·`: push \$1\$; pop and push its last digit: \$1\$; pop and push its first digit: \$1\$; Python-style truthify it: \$1\$; double it: \$2×1\$.
* 0. `Tºß`: push \$10\$; mirror it without overlap: \$1001\$; pop and push its smallest digit: \$0\$.
**Vertical:**
* 8. `8UX`: push \$8\$; pop and store it in variable `X`; push variable `X`.
* 10. `3θT`: push \$3\$; pop and push its last digit: \$3\$; push \$10\$.
* 9. `T₃н`: push \$10\$; push \$95\$; pop and push its first digit: \$9\$.
* 11. `α4Āº`: take the absolute difference (no-op with an empty stack); push \$4\$; Python-style truthify it: \$1\$; mirror it without overlap: \$11\$.
* 6. `4<·ß`: push \$4\$; decrease it by 1: \$4-1\$; double it: \$2×3\$; pop and push its smallest digit: \$6\$.
**Main diagonal / anti-diagonal:**
* 1. `83₃Āß`: push \$83\$; push \$95\$; Python-style truthify it: \$1\$; pop and push its smallest digit: \$1\$.
* 5. `4α₃θ`: push \$4\$; push the absolute difference (will use the single argument on the stack twice): \$\lvert4-4\rvert\$; push \$95\$; pop and push its last digit: \$5\$.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), A simple 3 by 3
Surely we can get a bigger one in Jelly, but if not...
```
5+2
+ḷ*
1+2
```
**[Try them online!](https://tio.run/##y0rNyan8///h7i1hj5rW/P//31TbiMtU25DL9OGO7UZcRlpGXIZAESMgz5BLG0hqgUltAA "Jelly – Try It Online")**  
(splits on new lines (`Ỵ`) and evaluates (`V`) each (`€`) as Jelly programs)
```
5+2 : 5 + 2 = 7
5+1 : 5 + 1 = 6
5ḷ2 : left(5, 2) = 5
2*2 : 2 ^ 2 = 4
1+2 : 1 + 2 = 3
2ḷ1 : left(2, 1) = 2
+ḷ* : left((0 + 0), 0) ^ 0 = 1
+ḷ+ : left((0 + 0), 0) + 0 = 0
```
]