[Question]
  [
Imagine an arsonist walking around the town and picking its victims according to a very specific pattern (Or, alternatively, *Imagine a bee flying around the garden and picking its flowers to pollenize according to a very specific pattern*). Let's say the town is an **N × N** matrix, where **N** is an integer higher than or equal to **2**. The arsonist starts from the upper-left corner and successively sets the house **M** spots in front of them (where **M** is the number of the house they're currently in), while changing the direction it's moving in after each fire, in the order East ⟶ South ⟶ West ⟶ North ⟶ East ⟶ South... and so on. The *lullaby* of the arsonist is the value of **M** that makes them exit the town (i.e. the last house they visit before stopping the abomination). This is way easier to understand with an example. Take the following matrix for instance:
```
3 2 3 2 7
3 1 4 1 6
2 5 3 1 1
4 4 3 2 4
1 1 1 1 1
```
* We start in the upper-left corner, so **M = 3** (`X` marks the current and previous positions of the arsonist):
```
X 2 3 2 7
3 1 4 1 6
2 5 3 1 1
4 4 3 2 4
1 1 1 1 1
```
* According to the known order, it first goes east **M (3)** spots and lands on a **2** so **M** changes accordingly:
```
X 2 3 X 7
3 1 4 1 6
2 5 3 1 1
4 4 3 2 4
1 1 1 1 1
```
* Then it goes south **2** spots and **M** is now **1**:
```
X 2 3 X 7
3 1 4 1 6
2 5 3 X 1
4 4 3 2 4
1 1 1 1 1
```
* Now it moves **1** spot to the West and **M** becomes **3**:
```
X 2 3 X 7
3 1 4 1 6
2 5 X X 1
4 4 3 2 4
1 1 1 1 1
```
* After it moves **3** spots north, it exits the town! Therefore, **3** is this arsonist's lullaby:
```
    X
X 2 3 X 7
3 1 4 1 6
2 5 X X 1
4 4 3 2 4
1 1 1 1 1
```
Given an **N × N** matrix (you can optionally take **N** as input too), find the lullaby of the arsonist. I've written a program with which you can generate more test cases and visualise the path of the arsonist: [Try it online!](https://tio.run/##jVLRitswEHy2vmIxlLM4n0kuaQumvre@lb4U2kJqihIrWK0iGVnhbOi/pyvLsnNtwgUjsKTd2dHMNL2ttVqdTuLQaGOh7VtCKr6HHVM/n5n8nQjLjVAV73IQyqZwYNYI3EjR4q7baW2q8aqfNxQenmCrtcxJpI3gyjIrtAIoAEu42Uyw8AbWJYkqhGNqx7HAT9h46HLjUbFE7OEcqigg/shaG@OIyHB7NAomlPuRDHwAyVXiISmJuLyA8kUfbX0Fprsd5ht/SWZk8DDDPRWwuNL8GdV/waG70k2IZ4FCbZwHSKpJBvmlUDxrGylsQimFvTbDEdrhbM1aWwmVGc6qhI5l8Q8V05IMjjg8r2caFEnHN6WBXklm24rAsAgPdfSGv9ZhJYsUFghOnmsh@YU8hSiFEIX80PymNNyUqv@FPsvMSPu@mCR@NR/drS1TFkIIXu2YA9BdaJkfhrOXJPI6Z6xpuKqS5B8FKSENlqPBn45Ssm2fx@kVIdHbP/BVtEeGYfJs9B5szaFhts5dQAhxUUKfehclPxl5BjyEKtGAu@9301Tsyn5poYZsSnbYVgy6PIaz09YaNJ7SwAsTezqt4BHcek9WsIQ1rnfkEd6C2y3JGk/c7ZosYfz@Ag "Python 3 – Try It Online")
* You may assume that the arsonist *does* have a lullaby (that is, it can actually get out of the matrix).
* The matrix will only contain positive integers less than or equal to **9** (digits), for simplicity. Solutions that handle any positive integer are completely welcome.
* Note that the arsonist *can* land on a spot they've already burned, in case the sense they're moving in is different from the first time. In such a scenario, just take the value of that element and move again as usual.
* You can compete in any [programming language](https://codegolf.meta.stackexchange.com/a/2073/59487) and can take input and provide output through any [standard method](https://codegolf.meta.stackexchange.com/questions/2447/default-for-code-golf-input-output-methods), while taking note that [these loopholes](https://codegolf.meta.stackexchange.com/questions/1061/loopholes-that-are-forbidden-by-default) are forbidden by default. This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so the shortest submission (in bytes) for *every language* wins.
## Test cases
```
-------------
9 2 3
1 7 2
8 7 6
Lullaby: 9
-------------
2 1 2 1
3 1 1 2
1 2 2 1
1 1 1 3
Lullaby: 2
-------------
3 2 3 2 7
3 1 4 1 6
2 5 3 1 1
4 4 3 2 4
1 1 1 1 1
Lullaby: 3
-------------
1 2 1 2 1 2
1 2 1 2 1 2
1 2 1 2 1 2
1 2 1 2 1 2
1 2 1 2 1 2
1 2 1 2 1 2
Lullaby: 2
-------------
3 2 1 2 1 1 1
2 3 2 3 2 1 1
2 1 1 1 3 1 2
3 1 1 1 1 1 1
4 5 2 3 1 1 1
1 2 1 2 1 2 2
1 2 2 3 2 1 2
Lullaby: 3
-------------
```
The matrices in a different format:
```
[[9, 2, 3], [1, 7, 2], [8, 7, 6]]
[[2, 1, 2, 1], [3, 1, 1, 2], [1, 2, 2, 1], [1, 1, 1, 3]]
[[3, 2, 3, 2, 7], [3, 1, 4, 1, 6], [2, 5, 3, 1, 1], [4, 4, 3, 2, 4], [1, 1, 1, 1, 1]]
[[1, 2, 1, 2, 1, 2], [1, 2, 1, 2, 1, 2], [1, 2, 1, 2, 1, 2], [1, 2, 1, 2, 1, 2], [1, 2, 1, 2, 1, 2], [1, 2, 1, 2, 1, 2]]
[[3, 2, 1, 2, 1, 1, 1], [2, 3, 2, 3, 2, 1, 1], [2, 1, 1, 1, 3, 1, 2], [3, 1, 1, 1, 1, 1, 1], [4, 5, 2, 3, 1, 1, 1], [1, 2, 1, 2, 1, 2, 2], [1, 2, 2, 3, 2, 1, 2]]
```
The fifth test case is [very interesting to visualise](https://tio.run/##jVJdi9swEHy2fsViKGdxPpOP64s531vfSl8KbSE1RYkVrKsiGVnhbOh/T1eWZefahAvBwSvvzo5mpultrdX6dBKHRhsLbd8SUvE97Jj69crk70RYboSqeJeDUDaFA7NGYCFFi1W309pU46d@Lig8PMNWa5mTSBvBlWVWaAVQALZws5lg4QM8liSqEI6pHccGv2HjocuNR8UWsYdzqKKA@BNrbYwrIsPt0SiYUO5HMvAEkqvEQ1IScXkB5as@2voKTHc7zHf@lszI4GGGey5gcWX4C6r/hkN3ZZoQzwKF2jgPkFSTDPJLoXjWNlLYhFIKe22GI7TD2Zq1thIqM5xVCR3b4p8qpiUZHHF4Xs80KJKOd0oDvZLMthWBYREu6ugNb63DShYpLBCcvNZC8gt5ClEKIQr5oflNabgpVf8LfZaZkfZ9MUn8bj66W0emLIQQvDsxB6C7MDJfDHcvSeR1zljTcFUlyT8KUkIabEeDPx@lZNs@j9MrQqK3f@CbaI8Mw@TZ6D3YmkPDbJ27gBDiooQ@9S5KfjPyDHgIVaIBdz/upq04lb1ooYZsSnbYVgy6PIaz09YaNJ7SwAsTezqtYQXL4cEfWYGr/ZmrhlOssYOsx8p3PsLHodNXywllhZ3D/4iy@gs).
  
[Answer]
# [MATL](https://github.com/lmendo/MATL), 32 bytes
```
JQ6*`G5thYay&Zj3$)wyJ2@-^*+8M]b&
```
[Try it online!](https://tio.run/##y00syfn/3yvQTCvB3bQkIzKxUi0qy1hFs7zSy8hBN05L28I3Nknt//9oYwUjBRA2twZShgomQGxmDeSbgrmG1kARE7ACE2sQHwJjAQ) Or [verify all test cases](https://tio.run/##y00syfmfEOFZ8d8r0EwrwdO0JCMysVItKstYRbO80svIQTdOS9vCNzZJ7X@ES1lFyP9oSx0FIx0FY2sFQx0FcyDbWsECzDCL5YoGShiCpQ2tFYzBbEOwCoggRNwQJm4M1GAMMQxMmsP0mIBJM2uQoKkOzCBrkLgJTLEJkkEgBDQLajOctFaglQjc4XAhiAPhXoHLQgQRXoaZaKyD6nyY/0xh2uGC6HajhifCGbEA).
### How it works
The input matrix is padded with a frame of five zeros, so for example
```
3 2 3 2 7
3 1 4 1 6
2 5 3 1 1
4 4 3 2 4
1 1 1 1 1
```
becomes
```
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 3 2 3 2 7 0 0 0 0 0
0 0 0 0 0 3 1 4 1 6 0 0 0 0 0
0 0 0 0 0 2 5 3 1 1 0 0 0 0 0
0 0 0 0 0 4 4 3 2 4 0 0 0 0 0
0 0 0 0 0 1 1 1 1 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
```
The frame of zeros is used to detect when the ~~arsonist~~ bee has exited the matrix. The extension with five zeros ensures that a *modular* displacement of length up to `9` in any direction from any of the nonzero entries will correctly land in a zero without wrapping around to some non-zero entry.
In matrix coordinates, the bee starts at entry `(6,6)` of the extended matrix. It reads that entry, and updates the coordinates as required, applying a (modular) displacement of the read length in the corresponding direction. This is repeated in a loop until the read value is `0`. The entry that was read before that one (i.e. the last non-zero entry) is the output.
The coordinates are actually stored as a complex number, so for example `(6,6)` becomes `6+6j`. This way the four cyclic directions can be realized as powers of the imaginary unit. The corresponding power (`j`, `1`, `-j` or `-1`) is multiplied by the read entry to obtain the complex displacement which is used for updating the coordinates.
The successively read values are kept on the stack. When the loop is exited, the stack contains all non-zero read values in order, then the last read value which is `0`, then the latest complex coordinates. So the third-top element is the required output.
[Answer]
# JavaScript (ES6), ~~70~~ 68 bytes
```
m=>(g=d=>(n=(m[y]||0)[x])?g(--d&3,x-=d%2*(y+=--d%2*n,L=n)):L)(x=y=0)
```
[Try it online!](https://tio.run/##tVFLDoIwEN17CjaaVouhlo@ajF6AGzRdGFCi0WLUGEi8O5ZC@QWXbibT9968@fRyeB@e0eN8f9kyjY/FCYob7FACsYoS0I3n4vNxMM8E3ifItuMZI5kN8XQ1R/kCFKAySUKQGG9DjDLIwcFFlMpnej0ur2mCTojzDbFWxGKCWJwSK1CvMl3r1BcC48mwQsmpLqKlkukXresqwnDUcGzUiFWtdQxaL1dHvwQU4WkFrR1dTVclbq@FVox1qWdtYjvmH7Hf6zYis1JzgoY3cHu@thPr7tu7i2dMOvBwruEvsd7ExRc "JavaScript (Node.js) – Try It Online")
### Commented
```
m => (                        // given m = input matrix
  g = d =>                    // g = recursive function taking the direction d
    (n = (m[y] || 0)[x]) ?    // let n be the content of the current cell; if it's defined:
      g(                      //   do a recursive call:
        --d & 3,              //     with the next direction (0 = E, 3 = S, 2 = W, 1 = N)
        x -=                  //     update x by subtracting ...
          d % 2 * (           //       ... ((d - 1) % 2) * n
            y += --d % 2 * n, //       and update y by adding ((d - 2) % 2) * n
            L = n             //       save n into L
          )                   //     end of x update
      )                       //   end of recursive call
    :                         // else:
      L                       //   stop recursion and return L
)(x = y = 0)                  // initial call to g() with x = y = d = 0
```
Given that the sign of the modulo in JS is that of the dividend, the direction is updated this way:
```
 d | d' = --d&3 | dx = -(d%2)  | dy = --d%2 | direction
---+------------+--------------+------------+------------------
 0 |     3      | -(-1%2) = +1 | -2%2 =  0  | (+1,  0) = East
 3 |     2      | -( 2%2) =  0 |  1%2 = +1  | ( 0, +1) = South
 2 |     1      | -( 1%2) = -1 |  0%2 =  0  | (-1,  0) = West
 1 |     0      | -( 0%2) =  0 | -1%2 = -1  | ( 0, -1) = North
```
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), ~~25~~ 18 bytes
```
ＰＳ↶ＷＫＫ«≔ιθ×Ｉι¶↷»⎚θ
```
[Try it online!](https://tio.run/##JYyxDoIwFEVn3lc0ndoEh1dGJ8NkoglRN3FoSIUXa0FacDB@ey24nZuTe5pOj02vbYzHyQYaRnJB7N0whXNI3Aopt1DR3IeDuQeRxrsja5iojHkkyT6Q7byn1gnK2Sv5rFobF3oaL0rtgyCZM147Lle7tE7UdmvsC6U1elzwf0uFGK@c8xoUKoQCUQGqRIhYQBK3uJntDw "Charcoal – Try It Online") Link is to verbose version of code. Explanation:
```
ＰＳ
```
Print the input string, but don't move the print position.
```
↶
```
Rotate the pivot left, so that the print direction is now up.
```
ＷＫＫ«
```
Repeat while there is a character under the print position.
```
≔ιθ
```
Save the character in a variable.
```
×Ｉι¶
```
Cast the character to a number and print that many newlines. As the print direction is now up, this ends up printing horizontally. The upshot is that we've moved the print position in the desired direction by the amount given by the number under the print position.
```
↷»
```
Rotate the pivot so that the next newlines move the print position in the next direction clockwise for the next pass of the loop.
```
Ｆ⟦ωθ⟧¿ιι⎚
```
Unfortunately we still have the input cluttering our canvas, and even more unfortunately, if we clear the canvas we also clear our variable. So, this is a bit of a trick: a list of the empty string and the variable is looped over. On the first pass of the loop, the loop variable is empty, so the canvas and the loop variable and the result variable are all cleared. But the loop is not finished! On the second pass of the loop, we still get to access our variable carefully preserved in our loop list. It simply remains to print it.
```
⎚θ
```
Clear the canvas and print the saved variable. (Thanks to @ASCII-only for the fix to Charcoal.)
[Answer]
# [Python 2](https://docs.python.org/2/), ~~85~~ 84 bytes
```
a=input();x=y=e=0;d=1
while-1<y<len(a)>x>=0:v=a[y][x];x+=v*d;y+=e*v;d,e=-e,d
print v
```
[Try it online!](https://tio.run/##tVA9b4MwEJ3Dr/BGPogUB9JKcS5bunbpZnmg@KSgRoAsQu1fT20TA8ne5cn33t3zu2tMe62rfV/UEgmQOI77HMqqubfLFdNgAGHHJNDo91recEtP5nTDapmvzvoMu2MHOTeCa8H0Brq1ZGYDuO6YTBC2mMioUWXVkq63xoMF@VJ3PEaLVhmLC9RYEPd5ZJ8FNi25fH5clKqVU78V5j895/uE0IQ4FAnhqa8c4apBCBoNWipExF2rVQZ8n2Yzj2@OsMLBd9CHQ@blYSR7svQdzvWRZcQpxj9y0zojGSKPK456oKdzTM7pfJ@nvQ/BZEa/5ni9ejpL@Ac "Python 2 – Try It Online")
Tip o' the hat to [Mr. Xcoder](https://codegolf.stackexchange.com/users/59487/mr-xcoder) for 1 byte.
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), ~~50~~ ~~49~~ ~~46~~ ~~34~~ ~~33~~ 26 bytes
```
ＮθＥθＳＭθ↑ＷＫＫ«ＭＩι✳⊗Ｌυ⊞υι»⎚⊟υ
```
[Try it online](https://tio.run/##LY5BDoIwEEX3PUWX0wQXQiIqS9iYiGliPEDFCTTWtpQWF8az10Kc3Zv3Z/K7QbjOCBXjSdvgL@F1Rwcjqwh3UntohYUxo6u8@rTqgbFkWzPjIo43m@g9SIUUOOIzafohNM0aqcXkQbKMNtJh56XR0JhwV/iAM@reDxDY@nC54GFKnFGZ@EtqhcIB@xfhxi7RKsaCHPKCbMuc7Mtd3MzqBw)
Link is to the verbose version of the code
Input must be **N** on its own line, then the lines of the array on separate lines after that.
Any ways to knock off bytes are welcome and wanted, as I am not a good golfer in Charcoal!
-12 bytes thanks to @Neil!
-1 byte thanks to @ASCII-only!
-7 bytes thanks to @ASCII-only (changed a bug that made `Clear` reset variables)
[Answer]
# [Red](http://www.red-lang.org), 145 bytes
```
func[b][h:[0 1 0 -1 0]x: y: 1 i: 0
until[y: h/(i: i % 4 + 1) *(t: b/(y)/(x)) + y x: h/(i + 1) * t + x none = b/(y) or(x < 1 or(x > length? b))]t]
```
[Try it online!](https://tio.run/##pVDLbsMgELz7K@ZSCVpFNsZ5obb5h1wRFzd2bCkikUUk/PXuGmji9lpZa2aGYXZhaE7TsTlpk7Vqau/2S9dGd0oXECiwop/xCqMi2isU2d26/qKJdzkjoccLKrxBcLwyp1DnbOQ585yTOMJHXzLAEfCwV9vgI1pxHZjHO6UH8IlLY8@uO6Dm3Dgz3YbeOrTQeo8S0kALbFHSuqN1Y0z2dJSUQkV7kpAIrlmJmgia/HVCzplU23SmotoQLrFGyCBckTp7qkfGrC9TQtdYqeN/2d8Zox7niRNHNfJ0s5QjIZ5fmH8d/D980WfxQvLRefoG "Red – Try It Online")
## More readable:
```
f: func[b][
    h: [0 1 0 -1 0]                                ; step lengths (combined for x and y) 
    x: y: 1                                        ; starting coords (1,1)
    i: 0                                           ; step counter 
    until[
        y: h/(i: i % 4 + 1) * (t: b/(y)/(x)) + y   ; next y; t stores the lullaby
        x: h/(i + 1) * t + x                       ; next x
        none = b/(y) or (x < 1 or (x > length? b)) ; until x or y are not valid indices
    ]
    t                                              ; return the lullaby
]
```
[Answer]
# [Perl 6](https://github.com/nxadm/rakudo-pkg), 62 bytes
```
{$^w;$^m[0,{$_+$m[$_]*(1,$w,-1,-$w)[$++%4]}...{!$m[$_]}][*-2]}
```
[Try it online!](https://tio.run/##RY3dDoIwDIXv9xQ1qYafQdwYoEF9kWUQL@RKosELQpBnnysjkqVNe87pt/ejfxa2G@HQwtVOWA8V1p0@8gmbGDuNjYkCwXHgieAJDqHGON4rM6dpOu18YDY6SqSZ7ec@Qht8sQmhffUsuLAzSMiYgBIkO7lesBuHLOTkZeS5Kt0kQLkqmIQcaBNMOYVc5a7XR7f5diuWIt1zvEabzxNHLuyNoBxfwqox8adIl1z6SpH0VxlW9gc "Perl 6 – Try It Online")
Takes the matrix as flat list and width.
[Answer]
# [Clean](https://clean.cs.ru.nl), 141 bytes
```
import StdEnv
d=[0,1,1,0,0,-1,-1,0:d]
$m[x,y]n[a,b:l]#r=size m
#u=x+a*n
#v=y+b*n
|0>u||0>v||u>=r||v>=r=n= $m[u,v]m.[u,v]l
?m= $m[0,0]m.[0,0]d
```
[Try it online!](https://tio.run/##VU9Nb4MwDL3nV0TKTqu7EthnpbSX7TBph0k9Ig4plCkSCRNNEAz462MGStvJll/8nDy/xNlBml7nicsOVEtleqW/88LSnU3eTEkSEXrAMTyMJR/SWycRudFhBXVkQgn7dRaxQhzVDyoQ5kS1kLeGsFLUiz0eWm/jWixl27qNKNq2xCqMoKjhoIz03QgZ2eqRw00DN0DS76wsLFmtGJqzhaqooE3zAj4EHTQcnsBHfEZ87DpyfYk1LADqA@VzHbID2mA3Tc7zmeZzBtOzgQ6u6LNIcw/0Yda4sPz/Qn@UOLHX@/zRa@pMbFVu0O2WiEs7/aH/jdNMfh375ftH/1obqVU8NZ@ZtGle6D8 "Clean – Try It Online")
Defines the function `? :: {#{#Int}} -> Int`, taking an unboxed array of unboxed arrays of integers and returning the result.
[Answer]
# Java 8, 121 bytes
```
m->{int l=m.length,x=0,y=0,f=0,r=0;for(;x*y>=0&x<l&y<l;x+=f<1?r:f==2?-r:0,y+=f==1?r:f>2?-r:0,f=++f%4)r=m[y][x];return r;}
```
[Try it online.](https://tio.run/##3VRNb4JAEL37K/ZSA3Ul8qG24uovqBePhANFsFhYzbJaCNnfToeFxPbm9FjgkTDMy7x5M9lTdIump8NnG@dRWZK3KOPNiJCMy0SkUZyQXfepAyQ24B2EQUgK04eoAsBTykhmMdkRThhpi@mm6ZJzVlh5wo/yg1ZsRmtAChBs5qdnYfjVc71hs3G1zsf1OverCUvX9lasUsac7VSsgAIhxnRsM4RSNpmkT54pWBHUYVCFvkjkVXAifNX6vZzL9T0HOYOq2zk7kAK6MvZSZPwI4iOzb2lflzIprPNVWhf4JXNucCs2ePJFhj6b5pU61FVU5z9wNTZdUgeR/wL5C6VM7edjkhxqgygbUcQFho2S1VXA1bB1DRfXitu5C1gim/EACwTHoXOqTUBwPKjSafPQJnR1UDboefZAzui/s9Db1BNxk@53sGfjeMPSIxt0qX2/URs51zqxvB@G/uEQcH@PQo1U@w0)
Alternative with same **121 bytes** byte-count:
```
m->{int l=m.length,x=0,y=0,f=0,r=0;try{for(;;x+=f<1?r:f==2?-r:0,y+=f==1?r:f>2?-r:0,f=++f%4)r=m[y][x];}finally{return r;}}
```
Uses try-finally instead of checking if the `x,y`-coordinate is still within bounds.
[Try it online.](https://tio.run/##3VRNb4JAEL33V@ylCcSVyIfaSld/Qb14JBy2yFosrGZZrYTsb6fDQtL25vRY4JEwzMu8eTPZI7/y6XH/0WUlr2vyygvZPhBSSJ0rwbOcbPtPGyCZA@8kTVJSuTFEDQCeWnNdZGRLJGGkq6brtk8uWeWVuTzod3pjM9oABECxWaxV04qTcuL4NmHixd@olWAs2EzVChIhxJiNrceQYJOJeIxcxaqkSZNbGhtRSF6WTatyfVGSqNiYLh7knC9vJcgZVV1PxZ5U0JWz06qQBxDP3aGlXVPrvPJOF@2d4ZcupSO9zJH5Jxn7bNtnGtDQUJt/x9X6dEkDRP4T5C@Mca2f90kKqA@ifESREBg@SlZfAVfDtzVCXCth7y5giWwmAiwQnIDOqTUBwYmgSq8tQpvQ10HZYOc5ADmj/85Cb9NAxE162MGBjeONS49sMKT@943ayLnVieX9MPQPh0D4exTmwXRf)
**Explanation:**
```
m->{                   // Method with integer-matrix parameter and integer return-type
  int l=m.length,      //  Dimensions of the matrix
      x=0,y=0,         //  x,y coordinate, starting at [0,0]
      f=0,             //  Direction-flag, starting at 0 (east)
      r=0;             //  Result-integer
  for(;x*y>=0&x<l&y<l  //  Loop as long as the x,y coordinates are still within bounds
      ;                //    After every iteration:
       x+=f<1?         //     If the direction is east:
           r           //      Increase the `x` coordinate by `r`
          :f==2?       //     Else-if the direction is west:
           -r          //      Decrease the `x` coordinate by `r`
          :            //     Else (it's north/south):
           0,          //      Leave the `x` coordinate the same
       y+=f==1?        //     If the direction is south:
           r           //      Increase the `y` coordinate by `r`
          :f>2?        //     Else-if the direction is north:
           -r          //      Decrease the `y` coordinate by `r`
          :            //     Else:
           0,          //      Leave the `y` coordinate the same
       f=++f%4)        //     Go to the next direction (0→1→2→3→0)
    r=m[y][x];         //   Set `r` to the value of the current cell
  return r;}           //  Return the last `r` before we went out of bounds
```
[Answer]
# [Perl 5](https://www.perl.org/), 92 bytes
```
sub b{1while eval join'&&',map{/./;map"(\$$_$&=".'$n=$_[$y][$x])'.$',x,'y'}'+<@_','->=0';$n}
```
[Try it online!](https://tio.run/##PYzbCoJAEIbvfYpFBkdpPeeVGV511xOYyAqChumSWYr46m3rgRhmhu/n4@fFsw6E6Pqc5JP7Kau6IMWb1eTeVg1qGtIH45Nt2aH8qn4DyECLVAuhiSBLYEwTGFIDLUA6UBxxxsMpzpCieY4cDKGZBe@7MmY0iS/pPHVsJLkeM0P4xCPuunIUjyy8ZQutqWRpKP5Om3kkwWpu5P5bPGmud2/xvi1/VW3TCfMaWI7rCJPxHw "Perl 5 – Try It Online")
## How?
The set of nested maps and the join produce this:
```
($x+=$n=$_[$y][$x])<@_&&($y+=$n=$_[$y][$x])<@_&&($x-=$n=$_[$y][$x])>=0&&($y-=$n=$_[$y][$x])>=0
```
which is then evaluated to determine if the loop ends. Because the Boolean is evaluated left to right, the value of `$n` actually changes (up to) four times during the evaluation. Because Boolean logic short-circuits in Perl, the value of `$n` is the lullaby when the loop is exited.
[Answer]
# [Python 3](https://docs.python.org/3/), ~~85~~ 84 bytes
xcoder: -1 (I never remember the +~ trick)
```
def f(x):
 r=c=0
 while-1<r:d=x[r][c];r,c=len(x)-c+~d,r;x=[*zip(*x)][::-1]
 return d
```
[Try it online!](https://tio.run/##pVJNT4NAEL3vr5j0AqttIx@2inLwYmLSf4AcEBZLJAtZ1pTa6F@vMwttKRovTrIwH2/fvNndeqvXlfT2@0zkkNstDxioMA2vGGzWRSlmzr0KsrCNVByl8Z2apmEpJOJm6eVXNlV3bRhdfBS1fdHyOAqCmRMjgdDvSkK216LRDYRgWRabDY3dggsec2AJLrvB74Kx1XtZJi/bAG5HWBccRDvMwz96jCKKHRN7g53uaKdHXXAtzV4f1wLZrsEwMR8zVPV7JsqduLwRl9HQrX/5f6vtUKSkU97lHNZnjXK3P4mDZh8ncqHPDXsdz8r70Xs8Hd0QXmXM8kpBWUgBhQRzffNGK7xfPm/qstBUamx6JWhFbqCISJRuNoVe2xaSWX2ZrFaF1Dah@DHX4ovATofQOKL8jWw1pEpw24GsE2PzCN8bP2ciUwilx8yOGRpLVRuaqj1RnhTmFro7RHxanI1Lz1K0tUi1yGCXfE7htdKIRQd20eTx4Wk1mU6qt2Q7iSMVhkk85Dif7tS6nSd1LWRmR9RDcqNQkr7hfDHffwM "Python 3 – Try It Online")
Instead of moving in different directions (E,S,W,N), this solution always moves east and rotates the grid counter clockwise after every move. After rotating, what was the last column is now the first row, so if the row index is less than zero it means we ran off the board. 
[Answer]
# [Retina](https://github.com/m-ender/retina/wiki/The-Language), 161 bytes
```
.
 $.%`*_#$&*
(?<=(.+¶)+|^)
A ¶A$#1*
~(K`{`^X\1YZ¶1S$4¶1XYZ¶2$4$2$4$3¶2XYZ¶3S¶3XY\1Z¶S
X
(_$*)(A_$*)
Y
( _$*)
Z
(?=\n\D$*\2\b.$*\3#(_+))
)`S
$$4$$2$$3
L$0`_+
$.0
```
[Try it online!](https://tio.run/##HY2/CsJADMb3PEWhUe4PHE2ubhYpuOnWpS1HewoOLh3ETfGx@gB9sZprQr7vlxCS1@P9nG60rg4ydLtoxhz3BtTpWClnl1nb76Chzpa5xpwM/NQlfuLQBur6ZaYGS9E2MWOJqbzgNvCNVNsFEm6gBTWi0apOCh2obINeXlVhCmc0gcPdiflcjVZr0LEBlINyFT1csYijBXTFunomJiJgL5lcGk8MnraA8sAJgdIes3ja4z8 "Retina – Try It Online")
]