[Question]
  [
# Introduction
Aron Nimzowitsch was a leading chess master and a influential chess writer.
In his book 'My System', the first chapter deals about the importance of the center and why you should dominate it. The simple reason is that your pieces have more possible direct next moves when being in the center which again gives the player more power.
This gets very clear when looking at different positions of a knight and its potential next moves (shown in pink) on an empty board:
[![enter image description here](https://i.stack.imgur.com/InzGQ.png)](https://i.stack.imgur.com/InzGQ.png)
# Objective
Evaluate the number of potential direct next moves of a knight on an empty board based on its position.
# Input Specs
The position of the knight.
First the x (column) and then the y (row). `0 0` is the left bottom corner.
For simplicity, I changed the labels of a chess board to numbers only. For our examples and test cases we use a 0-based index, you are free to use a 1-based index though.
You can use any type of possible input formats, an array, function arguments, etc.
# Output Specs
The number of potential direct next moves for a knight on an empty board.
# Test Cases
```
3 4 => 8
4 6 => 6
7 7 => 2
1 0 => 3
```
*Test cases are employing a 0-based index.* The full grid of values is:
```
2 3 4 4 4 4 3 2
3 4 6 6 6 6 4 3
4 6 8 8 8 8 6 4
4 6 8 8 8 8 6 4
4 6 8 8 8 8 6 4
4 6 8 8 8 8 6 4
3 4 6 6 6 6 4 3
2 3 4 4 4 4 3 2
```
  
[Answer]
# [Python 2](https://docs.python.org/2/), 35 bytes
```
lambda x,y:50/(8+x*x/7-x+y*y/7-y)-4
```
[Try it online!](https://tio.run/##K6gsycjPM/qfZhvzPycxNyklUaFCp9LK1EBfw0K7QqtC31y3QrtSqxJIV2rqmvwPsi1KzEtP1bDQ5ErLL1KoUMjMUwiyKijKzCtRV1DXy8rPzNMoLinSSNMAGqOpqQBSVAlWpPkfAA "Python 2 – Try It Online")
---
**[Python 2](https://docs.python.org/2/), 39 bytes**
```
lambda x,y:50/(8-x*(7-x)/5-y*(7-y)/5)-4
```
[Try it online!](https://tio.run/##K6gsycjPM/qfZhvzPycxNyklUaFCp9LK1EBfw0K3QkvDXLdCU99UtxLEqgSyNHVN/gfZFiXmpadqWGhypeUXKVQoZOYpBFkVFGXmlagrqOtl5WfmaRSXFGmkaQCN0tRUACmqBCvS/A8A "Python 2 – Try It Online")
Takes inputs 0-indexed.
The expression `x*(7-x)/5` takes the coordinate values `0..7` to
```
[0, 1, 2, 2, 2, 2, 1, 0]
```
(`min(x,7-x,2)` does the same, but is longer.) Summing this for `x` and `y` gives the right pattern but with the wrong numbers
```
0 1 2 2 2 2 1 0
1 2 3 3 3 3 2 1
2 3 4 4 4 4 3 2
2 3 4 4 4 4 3 2
2 3 4 4 4 4 3 2
2 3 4 4 4 4 3 2
1 2 3 3 3 3 2 1
0 1 2 2 2 2 1 0
```
(See [Neil's solution](https://codegolf.stackexchange.com/a/83945/20260) for better reasoning as to why this gives about the right pattern.)
Finally, the mapping `a -> 50/(8-a)-4` with floor-division gives the right values
```
2 3 4 4 4 4 3 2
3 4 6 6 6 6 4 3
4 6 8 8 8 8 6 4
4 6 8 8 8 8 6 4
4 6 8 8 8 8 6 4
4 6 8 8 8 8 6 4
3 4 6 6 6 6 4 3
2 3 4 4 4 4 3 2
```
An alternative equally-long solution with 1-indexed inputs:
```
lambda x,y:(x*(9-x)/6+y*(9-y)/6)**2/6+2
```
[Answer]
# [MATL](https://github.com/lmendo/MATL), ~~17~~ ~~14~~ ~~13~~ 12 bytes
*Thanks to @Neil for 1 byte off!*
```
8:HZ^ZP5X^=s
```
Input is 1-based.
[**Try it online!**](http://matl.tryitonline.net/#code=ODpIWl5aUDVYXj1z&input=WzUgN10)
### Explanation
This computes the Euclidean distance from the input to each of the 64 positions in the chessboard, and finds how many of those values equal the square root of 5.
Since the coordinates are integer values, we can be sure that the two floating-point values representing the square root of 5 (that computed from the coordinates and that computed directly) are indeed the same.
```
8:      % Push array [1 2 ... 8]
H       % Push 2
Z^      % Cartesian power. Gives 2D array [1 1; 1 2; ... 1 8; 2 1; ... 8 8]     
ZP      % Implicit input. Compute Euclidean distances, considering each row as a point
5X^     % Square root of 5
=s      % Compute how many squared distances equal sqrt(5). Implicit display
```
[Answer]
# Mathematica ~~63~~ 43 bytes
With 20 bytes saved thanks to suggestions by Martin Ender!
```
EdgeCount[8~KnightTourGraph~8,#+1+8#2/<->_]&
```
The above finds the number of squares that are 1 hop away from the given cell on the complete knights tour graph.
---
`g=KnightTourGraph[8,8,VertexLabels->"Name",Axes->True]`
displays the complete knight's tour graph, with vertex names and coordinates. Note that Mathematica defaults to one-based indexing for the coordinates.
[![graph](https://i.stack.imgur.com/1vUvr.png)](https://i.stack.imgur.com/1vUvr.png)
---
[Answer]
## JavaScript (ES6), 38 bytes
```
(x,y)=>+"23468"[((7-x)*x+(7-y)*y)/5|0]
```
Takes 0-indexed inputs. Explanation: Look at the squares of the distances to the centre:
```
24.5 18.5 14.5 12.5 12.5 14.5 18.5 24.5
18.5 12.5  8.5  6.5  6.5  8.5 12.5 18.5
14.5  8.5  4.5  2.5  2.5  4.5  8.5 14.5
12.5  6.5  2.5  0.5  0.5  2.5  6.5 12.5
12.5  6.5  2.5  0.5  0.5  2.5  6.5 12.5
14.5  8.5  4.5  2.5  2.5  4.5  8.5 14.5
18.5 12.5  8.5  6.5  6.5  8.5 12.5 18.5
24.5 18.5 14.5 12.5 12.5 14.5 18.5 24.5
```
The number of reachable squares falls into five bands:
```
8    0-5
6    5-10
4   10-15
3   15-20
2   20-25
```
I actually calculate 24.5 - (3.5 - x) \*\* 2 - (3.5 - y) \*\* 2 = (7 - x) \* x + (7 - y) \* y as it's a shorter calculation, but all it does is reverse the order of the bands.
[Answer]
# Jelly, 10 bytes
```
8ṗ2_³²S€ċ5
```
1-indexed. Takes a single argument of the form `[x,y]`. [Try it here.](http://jelly.tryitonline.net/#code=OFLhuZcyX8KzwrJT4oKsxIs1&input=&args=WzYsNl0)
```
8ṗ2          Cartesian square [[1,1],[1,2]…[8,8]]
   _³        Subtract the input
     ²S€     Compute the norm of each vector
        ċ5   Count fives
```
Dennis saved a byte!
[Answer]
## Mathematica, ~~44~~ 40 bytes
I've currently got three solutions at the same byte count:
```
2[3,4,6,8][[Tr@⌊3.2-.8Abs[#-4.5]⌋]]&
Tr@⌈.85(4-Abs[#-4.5])⌉/.{5->6,6->8}&
⌊Tr@⌈.85(4-Abs[#-4.5])⌉^1.1608⌋&
```
All of those are unnamed functions that take pair of coordinates like `{3, 4}`, which are 1-based.
I tried coming up with a somewhat explicit formula. The general pattern on the entire board looks like this:
[![enter image description here](https://i.stack.imgur.com/jSi6s.png)](https://i.stack.imgur.com/jSi6s.png)
The actual values of those colours (from lightest to darkest) are `2, 3, 4, 6, 8`. That is:
```
2 3 4 4 4 4 3 2
3 4 6 6 6 6 4 3
4 6 8 8 8 8 6 4
4 6 8 8 8 8 6 4
4 6 8 8 8 8 6 4
4 6 8 8 8 8 6 4
3 4 6 6 6 6 4 3
2 3 4 4 4 4 3 2
```
We first exploit the symmetry by shifting the origin to the centre, taking the absolute value and subtracting the result from `4`. This gives us coordinates `0.5` to `3.5` increasing from each corner. In order to make the centre coordinates the same we need to map `0.5` and `1.5` to different values and `2.5` and `3.5` to the same value. This is easily done by multiplying by `0.8` (gives`{0.4, 1.2, 2., 2.8}`) and flooring the result. So now we've got `{0, 1, 2, 2}` as distances from the centre. If we add up the coordinates in each cell, we get this table:
```
0 1 2 2 2 2 1 0
1 2 3 3 3 3 2 1
2 3 4 4 4 4 3 2
2 3 4 4 4 4 3 2
2 3 4 4 4 4 3 2
2 3 4 4 4 4 3 2
1 2 3 3 3 3 2 1
0 1 2 2 2 2 1 0
```
This has unique values for all the different possible results, so we simply use it as an index into `2[3,4,6,8]`.
In the second version we use ceiling instead floor. This way, `2`, `3` and `4` are already correct, but we get `5` and `6` instead of `6` and `8`, so we correct those manually with a substitution rule.
Finally, in the third version, we extend `5` and `6` upwards to `6` and `8` by means of exponentiation, followed by another floor operation.
[Answer]
## APL, 21 chars
```
{+/,5=+/¨×⍨(⍳8 8)-⊂⍵}
```
In English:
* `(⍳8 8)`: 8x8 rank-2 array containing the coordinates of all the cells;
* `+/¨×⍨(⍳8 8)-⊂⍵`: square of the euclidean distances of the given cell with respect to every cell on the board;
* `5=`: matrix of 0/1, where the 1s appear at squared distances equal 5;
* `+/,`: sum the flattened matrix
Test (in origin 1):
```
    f←{+/,5=+/¨×⍨(⍳8 8)-⊂⍵}
    f¨1+(3 4)(4 6)(7 7)(1 0)
8 6 2 3
```
In this form: 
```
f←{+/,5=+/¨×⍨(⍳⍺)-⊂⍵}
```
the left argument can specify the dimensions of the board. Hence `8 8 f` will work for the standard square chessboard. But on a larger and rectangular board, the test cases would give different results. For instance, on a 12x10 board:
```
    g←(10 12)∘f
    g¨1+(3 4)(4 6)(7 7)(1 0)
8 8 8 3
```
[Answer]
# Java - ~~160~~ 150 bytes
```
int m(int r,int c){int m=0,i,j;for(i=0;i<3;i+=2)for(j=0;j<3;j+=2){m+=r+i>0&r+i<9&c+2*j>1&c+2*j<11?1:0;m+=r+2*i>1&r+2*i<11&c+j>0&c+j<9?1:0;}return m;}
```
Ungolfed:
```
public static int m(int r, int c) {
    int m=0;
    for(int i=-1;i<2;i+=2)
        for(int j=-1;j<2;j+=2){
            m += r+i>-1 && r+i<8 && c+2*j>0 && c+2*j<8 ? 1:0;
            m += r+2*i>0 && r+2*i<8 && c+j>1 && c+j<8 ? 1:0;
        }
    return m;
}
```
The ungolfed code is identical except for changing the bounds of the for loop to save 4 bytes. Works by iterating through each possible move and performing a bounds check (>0 and <8). Uses the fact that the offsets are (1, 2), (2, 1), (-1, 2), (-2, 1), etc. and is able to check 2 moves for each value of i and j.
Edit: 10 bytes saved thanks to suggestions by Leaky Nun and u902383.
[Answer]
# C, 44 Bytes
```
f(x,y){return "23468"[((7-x)*x+(7-y)*y)/5];}
```
But this is better:
```
f(x,y){return "2344443234666643468888644688886446888864468888643466664323444432"[x*8+y];}
```
[Answer]
## Haskell, ~~49~~ 48 bytes
```
w=[0..7]
x%y=sum[1|a<-w,b<-w,(a-x)^2+(b-y)^2==5]
```
[Answer]
# Java, 81 characters (113 bytes)
```
int r(int a,int b){return "⍄䐲㑦晃䚈衤䚈衤䚈衤䚈衤㑦晃⍄䐲".codePointAt(a*2+b/4)>>(3-b%4)*4&15;}
```
Encode the whole result table as unicode table and then get appropriate bytes doing bitwise operations.
You can see it online here: <https://ideone.com/K9BojC>
[Answer]
# Python, 94 bytes
```
lambda x,y,a=[2,1,-1,-2,-2,-1,1,2]:list((9>x+a[i]>0)&(9>y+a[5-i]>0)for i in range(8)).count(1)
```
Uses 1 based indexing.
Demo at <https://repl.it/C6gV>.
[Answer]
# Pyth - ~~33~~ 15 bytes
*Thanks to @LeakyNun for reducing my size by half.*
Rearranging the maps and `V`'s will probably lemme golf a little off.
```
/sM*Fm^R2-Rd8Q5
```
[Test Suite](http://pyth.herokuapp.com/?code=%2FsM%2aFm%5ER2-Rd8Q5&test_suite=1&test_suite_input=3%2C+4%0A4%2C+6%0A7%2C+7%0A1%2C+0&debug=0).
[Answer]
# [APL (Dyalog Unicode)](https://www.dyalog.com/), 15 [bytes](https://codegolf.meta.stackexchange.com/a/9429/43319 "When can APL characters be counted as 1 byte each?")[SBCS](https://github.com/abrudz/SBCS ".dyalog files using a single byte character set")
```
≢⍸2=|×.-∘⎕¨⍳8 8
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT///qKNdIe3/o85Fj3p3GNnWHJ6up/uoY8ajvqmHVjzq3WyhYAFS8Z8zjctYwYQLSJkomIEocwVzEGWoYAgA "APL (Dyalog Unicode) – Try It Online")
[Answer]
# [J](http://jsoftware.com/), 23 bytes
```
1#.1#.5=[:+&*://]-/i.@8
```
[Try it online!](https://tio.run/##y/qvpKeepmBrpaCuoKNgoGAFxLp6Cs5BPm7/DZX1gMjUNtpKW03LSl8/Vlc/U8/B4r8mV2pyRr5CGlCpgQKMbQiECLYRjGkEhP8B "J – Try It Online")
Homage to Lynn's method, converted to J
[Answer]
## Actually, 18 bytes
```
`;7-2km`MΣ8-:50\¬¬
```
[Try it online!](http://actually.tryitonline.net/#code=YDs3LTJrbWBNzqM4LTo1MFzCrMKs&input=Myw0)
This implements the same formula that many other answers have been using: `50/(8-x*(7-x)//5+y*(7-y))//5)-4`. Input is taken as a list: `[x,y]` (or any iterable literal in Python, like `(x,y)` or `x,y`).
Explanation:
```
`;7-2km`MΣ8-:50\¬¬
`;7-2km`M           for each value in input:
 ;7-                  make a copy, subtract from 7
    2                 push 2
     km               minimum of the three values (x, 7-x, 2)
         Σ          sum
          8-        subtract from 8
            :50\    integer divide 50 by the value
                ¬¬  subtract 2 twice
```
[Answer]
# [Perl 6](https://github.com/nxadm/rakudo-pkg), 44 bytes
```
{+grep {5==[+] $_>>²},((^8 X, ^8)XZ-(@_,))}
```
[Try it online!](https://tio.run/##K0gtyjH7n1upoJZm@79aO70otUCh2tTWNlo7VkEl3s7u0KZaHQ2NOAuFCB2FOAvNiChdDYd4HU3N2v/WXGn5RQo2xgomQGimYA6EhgoGdgq6dgoqiToKKkkK1VycxYmVCkpQrq2dQnWaBoSjWatkzVX7HwA "Perl 6 – Try It Online")
[Answer]
# [Forth (gforth)](http://www.complang.tuwien.ac.at/forth/gforth/Docs-html/), 49 bytes
```
: g 9 over - 6 */ ;
: f g swap g + dup 6 */ 2 + ;
```
[Try it online!](https://tio.run/##VYxLCoAwDET3OcWslSq6EGxPI9b6QUipVY9fo125mjcTXhyHuKjZvZGSxowefE0BCh2KGoY0nKzHPXiJEvb0@dJKMWLE6YgE0RpYFvghsGKTB9XXdmaPMVAmMp@bHg "Forth (gforth) – Try It Online")
Port of [xnor's 1-based solution](https://codegolf.stackexchange.com/a/83944/78410). `f` takes `x y` (1-based) on the stack and computes `(x(9-x)/6 + y(9-y)/6)**2/6+2`. Uses the built-in `*/` to do a multiplication followed by division at once and save a space.
```
: g ( x -- x(9-x)/6 )
  9 over -  \ ( x 9-x )
  6 */      \ x*(9-x)/6
;
: f ( x y -- ans )
  g swap g    \ apply g to y, then to x
  + dup 6 */  \ (gx+gy)*(gx+gy)/6
  2 +
;
```
]