[Question]
  [
In chess, the queen piece can move arbitrarily far in each cardinal and intercardinal direction. What does this mean? Well, I'll show you with an ASCII drawing:
```
\..|../
.\.|./.
..\|/..
---Q---
../|\..
./.|.\.
/..|..\
```
It means the queen (notated as `Q`) can move along these lines (notated as `\`, `|`, `/`, and `-`), and cannot reach the other spaces (notated as `.`). The `|`s extend from the queen vertically, the `-`s horizontally, and the `\/`s diagonally.
However, I've quickly lost interest in chess and am more fixated on the diagram itself;
How many different symbols are contained within the 3x3 region centered on any given square? One way to visualize this would be to replace the symbols with this value, shown below:
```
2232322
2234322
3356533
2465642
3356533
2234322
2232322
```
Why? Well, for example:
The queen square has value 5 because the 3x3 region centered on the queen looks like this:
```
\|/
-Q-
/|\
```
Where we can see 5 distinct symbols: `\|/-Q`.
The top left square has value 2 because the 3x3 region around that square looks like this:
```
 \.
 .\
```
Where we can see 2 distinct symbols: `\.`. Anything beyond the board is not a symbol, so those space characters I've included for visualization purposes don't count.
## Challenge:
Given the queen's position on an MxN board, output the number of unique symbols contained within the 3x3 region around each square, based on the queen movement diagram which would be generated. This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so shortest wins :)
## Rules:
* Standard I/O rules apply, any format to represent the grid size and the queen's position is fine in whatever order / format, and output can be plain text or a list of strings or whatever.
* Since any given cell can only see up to 6 different symbols, digits on a line do not need to be delimited in any way. Lines, however, must be clearly delimited, as the board is not necessarily square.
* 0 indexing is allowed.
* Input will always be valid; Board will always be at least 1 unit wide and at least 1 unit tall, and the queen's position will always be on the board.
## Examples:
Formatted as
```
(board width, board height) (queen x, queen y)
image of board
output
```
Queen x and queen y given as 1 indexed, x being leftmost and y being topmost, output being 1 indexed as well. Your I/O format may differ from this.
Note that your program does not need to use the symbols `Q\|/-.`, nor does it need to generate an image of the board at all. Only the number grid should be output.
```
(1, 1) (1, 1)
Q
1
(3, 3) (2, 2)
\|/
-Q-
/|\
454
555
454
(6, 3) (2, 2)
\|/...
-Q----
/|\...
455322
556422
455322
(2, 7) (1, 5)
|.
|.
|.
|/
Q-
|\
|.
22
22
33
55
55
55
33
(7, 2) (5, 1)
----Q--
.../|\.
2235553
2235553
(10, 10) (10, 10)
\........|
.\.......|
..\......|
...\.....|
....\....|
.....\...|
......\..|
.......\.|
........\|
---------Q
2221111122
2222111122
2222211122
1222221122
1122222122
1112222222
1111222232
1111122233
2222223355
2222222354
(7, 7) (4, 4)
\..|../
.\.|./.
..\|/..
---Q---
../|\..
./.|.\.
/..|..\
2232322
2234322
3356533
2465642
3356533
2234322
2232322
```
  
[Answer]
# [MATL](https://github.com/lmendo/MATL), ~~52~~ ~~49~~ ~~48~~ 45 bytes
```
9,i:i-]!=E2M+~+4KGY(3HGZ(3M*+^4Y6Z+9_YA!gsIGe
```
Input order is: board width, queen x, board height, queen y.
[Try it online!](https://tio.run/##y00syfn/31In0ypTN1bR1tXIV7tO28TbPVLD2MM9SsPYV0s7ziTSLErbMj7SUTG92NM99f9/My4jLmMuIwA) Or [verify all test cases](https://tio.run/##y00syfmf8N9SJ9MqUzdW0dbVyFe7TtvE2z1Sw9jDPUrD2FdLO84k0ixK2zI@0lExvdjTPfW/QXKES1ZURch/Qy4I5DLmMgJjLjMYwwgobM5lygUmQBwuQwMEAoqagDEA).
The code first generates the board using the numbers 1, 9, …, 59049 (first six powers of 9) instead of the symbols `.`, `/`, …, `Q`. Then 2-dimensional convolution with a 3×3 matrix containing ones is performed. This gives the sum of the 3×3 block centered in each cell. The desired result is the number of non-zero digits in the base-9 expansion of the sum for each cell.
Base 9 is sufficient (rather than base 10) because if the same character (`.`) appears 9 times in a 3×3 block the base-9 expansion will contain, due to carry, a digit 1 and the rest 0 (instead of a digit 9 and the rest 0 if base 10 were used), but the final result is 1 anyway.
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 61 bytes
```
ＮθＮηＵＯθη.ＪＮＮＰ*⁺θηQＪ⁰¦⁰ＴθηＦηＦθ«Ｊκι≔⊞ＯＫＭＫＫδ⊞υΣＥδ∧Ｌλ⁼μ⌕δλ»⎚⪪⪫υωθ
```
[Try it online!](https://tio.run/##VY9BTsMwEEXXzSlGWU2Qi1ohsSCrChWJitCg9gJuYxKrju04NiwQZzfjAFLqjfVmnr@@zx13Z8NVjM/aBv8a@pNwOBRlNueOeH9SRrc4MOgY5Lc5jXaht0eDc7NgcIVkVUF5aZ3UHh9uGNQqjFNK2tXTOH@bpa0YrIiOTva/Wpm9GwfUAaZ7KOArW/zJFwaShMVmHGWrsQ5jt7fCcW8c1kJcKmOcSKUSUBsGTdKTh4HBIfRYcYsNg41u8EXo1neoSNsOgasRewZPkjYkqGI6ZfadPSrB6W//9Q9WSY87I3XK/KTXA3kx3sMdrGEdlx/qBw "Charcoal – Try It Online") Link is to verbose version of code. 0-indexed. Explanation:
```
ＮθＮηＵＯθη.
```
Input the dimensions of the board and draw it.
```
ＪＮＮＰ*⁺θηQ
```
Draw the Queen's lasers and then the Queen herself.
```
Ｊ⁰¦⁰Ｔθη
```
Trim the lasers to size.
```
ＦηＦθ«
```
Loop over each square.
```
Ｊκι≔⊞ＯＫＭＫＫδ
```
Get the cell and its eight neighbours.
```
⊞υΣＥδ∧Ｌλ⁼μ⌕δλ
```
Save the count of distinct nontrivial neighbours.
```
»⎚⪪⪫υωθ
```
Clear the canvas and output the counts.
[Answer]
# [APL(Dyalog Unicode)](https://dyalog.com), 52 bytes [SBCS](https://github.com/abrudz/SBCS)
```
{2 2↓{≢∪0~⍨,⍵}⌺3 3⊢(-2+⍵)↑2+(≠⍥|⊃×⍥×,2+,⍳=)/¨⍺∘-¨⍳⍵}
```
[Try it on APLgolf!](https://razetime.github.io/APLgolf/?h=e9Q31dP/UdsEAwA&c=qzZSMHrUNrn6UeeiRx2rDOoe9a7QedS7tfZRzy5jBeNHXYs0dI20gQKaj9omGmlrPOpc8Kh3ac2jrubD04GMw9N1jLSB6jfbauofWvGod9ejjhm6IMZmkBkA&f=SylKLFd41DZBoVpdPzBGt0ZPPdpQu9pWv@ZR71arw9P1D08HMh51txhpGzzq3fyodwWQW3sISO561DFDF8QACm6NreUqSS0uARuk86h36aOuJqBSBR2FFJDxUBGFNK7ijMSC1GKwMg1DBUNNBQ1jBWMwaQYkzRWMgKSRgjmQNDRQMDQAi5lrchXkF2eWZObnoeg0gqoGkaZgEUMFUySdJgommgq6CoZcXBrqSfmJRSnqCuqFpampeUAa5CwQXZRaXJpToq75qHfVo7aJCGtAnjm0QgHiXAA&i=AwA&r=tryapl&l=apl-dyalog&m=dfn&n=f)
Takes 0-indexed `qy qx` as left argument, and `height width` as the right argument.  
Actually drawing the ascii art is a bit simpler (can be seen in the footer).
[Answer]
# [Python 3](https://docs.python.org/3/), 149 bytes
```
lambda w,h,x,y:[[len({(I==J,I==-J,I==0,J==0)for I in[i-1,i,i+1]for J in[j-1,j,j+1]if~x<I<w-x>~y<J<h-y})for i in range(-x,w-x)]for j in range(-y,h-y)]
```
[Try it online!](https://tio.run/##fY1BboMwEEX3nGKUFW7HUiBpK0XQPVyBZkEUuxilDnKQglUlVydjDIrVRRczI733v93ZvjnrzSjzr/FU/xyONVyxwQHtrqpOQse/cZHnJdLi015jSYvJs4EClK4UT1Chek32DpUOtYRabAkpeR@yIrvy4fNuszJruL1NVUU5MLX@FjEfkDyb6m2ALVKa7cejkNCLSx@/1GwXAbiccTm5EIDOKE0Bg3ARXb5asWhhLIqmMiQIfoA/L5vlhoCbdJZpKN//kymBj@DZt1A6kS7wz5/JGsGPl/6GTTfbubn1cnwA "Python 3 – Try It Online")
The position of queen is 0-indexed.
Maybe someone may golf many bytes from it, but I'm not sure how to do that.
[Answer]
# Python3, 468 bytes
```
R=range
U=lambda b,x,y:-1<x<len(b)>-1<y<len(b[0])
def V(b,m,x,y):
 if r:=U(b,x,y)==1>b[x][y]:b[x][y]=m
 return r
def l(x,y,Q):
 b=eval('[0]*y,'*x);b[Q[0]][Q[1]]=5;B=eval(str(b));q=[(6,*Q,1,0),(7,*Q,0,1),(8,*Q,-1,1),(9,*Q,1,1)]
 while q:m,x,y,X,Y=q.pop(0);q+=[(m,x+i*X,y+i*Y,X,Y)for i in[-1,1]if V(b,m,x+i*X,y+i*Y)]
 return b,B
def f(X,Y,Q):
 b,B=l(X,Y,Q)
 for x in R(X):
  for y in R(Y):B[x][y]=len({b[J][K]for J in R(x-1,x+2)for K in R(y-1,y+2)if U(B,J,K)})
 return B
```
[Try it online!](https://tio.run/##pZLNTuswEIXX5Cm8iyedorgFCilmkWW7KlJRq8iLWKRglKap6b031hXPXsZJ@Nkh0Y0znjnz5czItTs876rxdW2Px3tp8@qpCJayzLf6MWcaG3TJUNw2t2VRcQ13FLsuzmIFwWOxYQ9c49YLIQmY2TCbyCVvO0FKcaezRmVOJf1XbgNmi8MfWzHbtpeclLjwzVoWf/OSh4SOHIZRA1OdLeim6BRKyctp2kleD5bcwHQvM36F0QIFxoB84sMYBYXXPhyKNr7pFAJUwP49m7Jg@6R1jCtcy/15vat5TLAB0Sg/MNEKHZ1rX4fNzjLDTJV5mjKfA3/JPLefSWPaTrXh1NpPhaks@2vAPK0hGrvnK19tE65LrCFJ@y35Ff/X2Uxlc@UVs07RkIdmMGo9zbuUo5SjFBlb8hRnOIc3@PSTHj/skyXyjzxGFgP9@ay2pjrwMDx/2ZmKb/PabxUNAAR9aRhGoxiCb4QxjpEwTJxAuDqRMMER8otTphjhpN3Dxa8Jgh5Z7B/WzQljkAna5w@A4zs)
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), ~~69~~ 68 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
θDI¹->©н‚ß®W.ý`D¹н‚ß¹н¹ß)vyN4%N8«.Λ}1A0.ΛS¶¡¶δ.ø¬g¶иšĆ2Fø€ü3}εε˜¶KÙg
```
Inputs as two loose pairs, in the order `[queen x,queen y]`, `[board width,board height]`.
[Try it online](https://tio.run/##yy9OTMpM/f//3A4Xz0M7de0Orbyw91HDrMPzD60L1zu8N8Hl0E6YAJBxaOfh@ZpllX4mqn4Wh1brnZtda@hoAKSCD207tPDQtnNb9A7vOLQm/dC2CzuOLjzSZuR2eMejpjWH9xjXntt6buvpOYe2eR@emf7/f7ShjmksV7SRjnksAA) or [verify all test cases](https://tio.run/##S0oszvifnFiiYKeQnJ@SqpdfnJiUmapgY6Pg6u/GpeSZV1BaUmyloGRve25H2IW9LqFcSo7JJaWJOQr5pSVAOaCUjtL/cztcIiN07Q6tvLD3UcOsw/MPrQvXO7w3JsElAioApCMOz9csq/QzUfWzOLRa79zsWkNHAyAVfGjboYWHtp3bond4x6E16Ye2XdhxdOGRNiO3wzseNa05vMe49tzWc1tPzzm0zfvwzPT/SnphtlxKAUWpJSWVugVFmXklqSlITvE6tFvn0DZ7LpDjucozMnNSFYpSE1MUMvO4UvK5FBT08wtK9CF@hFJo3rZRUAGrzUv9Hx1tqGMYqwMmY7mio410jIA8Yx1jJJ4ZlGeoYwrkGemYg3mmYH3mQBVgOQMdQwOQOWAaJGKiYwKWB6kGAA).
**Explanation:**
(let's call the inputs `[x,y]` and `[w,h]` in the explanation below)
*Step 1:* Use the Canvas builtin to draw the shape (with `0123a` instead of `|/-\Q`):
```
θ             # Push the last item of the first (implicit) input: y
D             # Duplicate it
 I            # Push the second input [w,h]
  ¹-          # Subtract the first input: [w-x,h-y]
    >         # Increase each by 1: [w-x+1,h-y+1]
     ©        # Store this in variable `®` (without popping)
      н       # Pop and push its first item: w-x+1
       ‚      # Pair it with the duplicated y
        ß     # Pop the pair, and push its minimum: min(y,w-x+1)
®             # Push pair `®` again: [w-x+1,h-y+1]
 W            # Push its minimum (without popping): min(w-x+1,h-y+1)
  .ý          # Add it to the middle of the pair: [w-x+1,min(w-x+1,h-y+1),h-y+1]
    `         # Pop the triplet and push all three separated to the stack
D             # Duplicate the top h-y+1
 ¹н           # Push the first item of the first input: x
   ‚          # Pair them together: [h-y+1,x]
    ß         # Pop and push the minimum: min(h-y+1,x)
¹н            # Push the first item of the first input: x
¹ß            # Push the minimum of the first input: min(x,y)
)             # Wrap all eight values on the stack into a list
v             # For-each over these values:
 y            #  Push the current value
  N           #  Push the 0-based loop-index
   4%         #  Modulo-4
     N        #  Push the 0-based loop-index again
      8«      #  Append an 8
        .Λ    #  Use the (modifiable) Canvas builtin with these three options
}             # Close the loop
    .Λ        # Use the (modifiable) Canvas builtin again with the options:
 1A0          #  1,"abcdefghijklmnopqrstuvwxyz",0
```
[See this 05AB1E tip of mine for an in-depth explanation of the Canvas builtin.](https://codegolf.stackexchange.com/a/175520/52210) But in short, these are the steps it does:
1. Draw \$y\$ amount of `'0'` in direction ↑ (`0`), then reset back to the origin with 'direction' `8`.
2. Draw \$\min(y,w-x+1)\$ amount of `'1'` in direction ↗ (`1`), and reset back to the origin again with `8`.
3. Draw \$w-x+1\$ amount of `'2'` in direction → (`2`), and reset back to the origin again with `8`.
4. Draw \$\min(w-x+1,h-y+1)\$ amount of `'3'` in direction ↘ (`3`), and reset back to the origin again with `8`.
5. Draw \$h-y+1\$ amount of `'0'` in direction ↓ (`4`), and reset back to the origin again with `8`.
6. Draw \$\min(h-y+1,x)\$ amount of `'1'` in direction ↙ (`5`), and reset back to the origin again with `8`.
7. Draw \$x\$ amount of `'2'` in direction ← (`6`), and reset back to the origin again with `8`.
8. Draw \$\min(x,y)\$ amount of `'3'` in direction ↖ (`7`), and reset back to the origin again with `8`.
9. Draw \$1\$ character from string `"abcdefghijklmnopqrstuvwxyz"` (thus `'a'`) in direction ↑ (`0`).
[Try just step 1 online.](https://tio.run/##yy9OTMpM/f//3A4Xz0M7de0Orbyw91HDrMPzD60L1zu8N8Hl0E6YAJBxaOfh@ZpllX4mqn4Wh1brnZtda@hoAKT@/4821DGN5Yo20jGPBQA)
*Step 2:* Add a no-op border (of newline characters) like a painting frame, and then get all overlapping 3x3 blocks:
```
S             # Convert it to a list of flattened characters
 ¶¡           # Split on newlines
    δ         # Map over each row of characters:
   ¶ .ø       #  Surround it with a leading/trailing newline character
       ¬      # Push the first line (without popping the matrix)
        g     # Pop and push its length
          и   # Create a list of that many
         ¶    # newline characters
           š  # Prepend it as first row
            Ć # Enclose; appending its own head
2F            # Loop 2 times:
  ø           #  Zip/transpose; swapping rows/columns
   €          #  Map over each row:
    ü3        #   Convert it to a list of overlapping triplets
}             # Close the loop
```
[Try just the first two steps online.](https://tio.run/##yy9OTMpM/f//3A4Xz0M7de0Orbyw91HDrMPzD60L1zu8N8Hl0E6YAJBxaOfh@ZpllX4mqn4Wh1brnZtda@hoAKSCD207tPDQtnNb9A7vOLQm/dC2CzuOLjzSZuR2eMejpjWH9xjX/v8fbahjGssVbaRjHgsA)
*Step 3:* Remove all painting-frame newline characters, and count how many unique characters each 3x3 block contains, which is our resulting matrix:
```
εε            # Nested map over each 3x3 block:
  ˜           #   Flatten it to a list of characters
   ¶K         #   Remove all newline characters
     Ù        #   Uniquify it
      g       #   Pop and push its length
              # (after which the resulting matrix is output implicitly as result)
```
[Answer]
# JavaScript (ES10), 153 bytes
The position is 0-indexed. The method is similar to [the one used by @tsh](https://codegolf.stackexchange.com/a/251022/58563).
```
(w,h,x,y)=>(F=X=>Y<h?new Set([-!!X++,0,X<w].flatMap(p=>[-!!Y,0,Y<h-1].map(q=>[(H=X+~x+p)==(q+=Y-y),H==-q]+!H+!q))).size+[`
`[X-w]]+F(X<w?X:!++Y):'')(Y=0)
```
[Try it online!](https://tio.run/##dc0/b8IwEAXwnU@BJ3y6cxQDKgJxYUNZOnWxFVkiok6hCvnTRA106FdP3bGiSDf93tO79/wz744f56ZXVf3qx4JHOdCJrnQDTuSeDSd2e9pVfpi@@F5mSgiDSDGZ7eCiosz757yRDSe/iQ0e2kq76BK0DSpTNvh9xQaYZYts1Q0oZVatQ5GiaAEg6s5fHrPD5JAZNTiHexnWd2YjEC1sZjOQlmMYj3XV1aWPyvpNFlKTDu9igMlfX9AiJPrOnx74nFZhZ3nnK5rT8p99HVO4Na0Bxh8 "JavaScript (Node.js) – Try It Online")
[Answer]
# [PARI/GP](https://pari.math.u-bordeaux.fr), 109 bytes
```
f(w,h,x,y)=matrix(h,w,i,j,#Set([[k-y==s=l-x,s==t=y-k,!s,!t]|n<-[0..8],(k=i-n%3)>=0&&k<h&&(l=j-n\3)>=0&&l<w]))
```
[Attempt This Online!](https://ato.pxeger.com/run?1=VZDBasMwDIZfxe1osEEuyVLWjkV9iR09M8xYFjduMIlLEuhj9LZLL2PPtLeZknmHwi_Q90tCQp_f3rT29cNfr1-nUMrdz7HkPVQwwCjwaEJrB15BDxYOcPf8HrhStRwRO3RygA4x4ChrWHSwCPrcFFKl6_VOA6_RymaViz2mSVIXVZJwhwfZvETLFb0WIi69GO_dyA2Te-Zb2wRKlwRL8fTHnr8Z53gJzAgBTKkMGCklacIcWD4b2UQPN3QPbDt3biailIxNnMvInuKRFKvbeTjXOt72_5hf)
A port of [@tsh's Python answer](https://codegolf.stackexchange.com/a/251022/9288). The position is 0-indexed.
]