[Question]
  [
## The Idea
We've done matrix spirals before, and full rotations, and even [diagonal
rotations](https://codegolf.stackexchange.com/questions/63755/rotate-the-anti-diagonals),
but not, as far as I can find, *snake rotations*!
What is a snake rotation?
Imagine the rows of a matrix snaking back and forth, with dividers between
them like the dividers of long queue:
```
    +--------------+
      1  2  3  4  5|
    +------------  |
    |10  9  8  7  6|
    |  +-----------+
    |11 12 13 14 15|
    +------------  |
     20 19 18 17 16|
    +--------------+
```
Now imagine rotating these items by 2. Each item advances, like people moving
in a line, and the items at the end spill out and return to the beginning:
```
    +--------------+
-->  19 20  1  2  3|
    +------------  |
    | 8  7  6  5  4|
    |  +-----------+
    | 9 10 11 12 13|
    +------------  |
<--  18 17 16 15 14|
    +--------------+
```
If there are an odd number of rows it will exit from the right, but still wrap
to the beginning. For example, here's a 3 rotation:
```
    +--------------+
      1  2  3  4  5|
    +------------  |
    |10  9  8  7  6|
    |  +-----------+
    |11 12 13 14 15
    +--------------+
    +--------------+
-->  13 14 15  1  2|
    +------------  |
    | 7  6  5  4  3|
    |  +-----------+
    | 8  9 10 11 12  -->
    +--------------+
```
A negative rotation will take you backwards. Here's a -2 rotation:
```
    +--------------+
<--   3  4  5  6  7|
    +------------  |
    |12 11 10  9  8|
    |  +-----------+
    |13 14 15  1  2  <--
    +--------------+
```
## The Challenge
Your function or program will take 2 inputs, in any convenient format:
* A matrix
* A integer (positive or negative) indicating how many places to rotate it.
It will return:
* The rotated matrix
Notes:
* Code golf. Fewest bytes wins.
* Matrixes need not be square, but will contain at least 2 rows and 2 columns
* Positive integers will rotate row 1 toward the right
* Negative integers will rotate row 1 toward the left
* You may reverse the meaning of positive / negative rotation numbers, if convenient
* The rotation number can be larger than the number of items. In that case, it
will wrap. That is, it will be equivalent to the number modulo the number of
items.
* The matrix will contain only integers, but it may contain any integers,
including repeats
## Test Cases
Format:
* Matrix
* Rotation number
* Expected return value
---
```
4 5
6 7
1
6 4
7 5
```
---
```
2  3  4  5
6  7  8  9
10 11 12 13
-3
5  9  8  7
12 11 10 6
13 2  3  4 
```
---
```
8 8 7 7
5 5 6 6
10
5 5 8 8
6 6 7 7
```
  
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 10 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
```
UÐeẎṙṁ⁸UÐe
```
A dyadic Link accepting the marix on the left and the rotation integer on the right (uses the reverse meaning of positive / negative)
**[Try it online!](https://tio.run/##y0rNyan8/z/08ITUh7v6Hu6c@XBn46PGHSD@////o6ONdBSMdRRMdBRMY3UUos10FMx1FCx0FCxBPEMDHQVDQyAGKjI0jo39bwwA "Jelly – Try It Online")**
### How?
```
UÐeẎṙṁ⁸UÐe - Link: matrix of integers, M; integer, R
 Ðe        - apply to even indices of M:
U          -   reverse each
   Ẏ       - tighten
    ṙ      - rotate left by R
     ṁ     - mould like:
      ⁸    -   chain's left argument, M
        Ðe - apply to even indices:
       U   -   reverse each
```
[Answer]
# [R](https://www.r-project.org/), ~~121~~ ~~110~~ 101 bytes
```
function(m,n,o=t(m)){o[,j]=o[i<-nrow(o):1,j<-c(F,T)];o[(seq(o)+n-1)%%sum(1|o)+1]=o;o[,j]=o[i,j];t(o)}
```
[Try it online!](https://tio.run/##TYxNCoMwEIX3PUU3hQmdgBONStRtT9CduCihgkISqpEW2p7dDhRE3mKY7/1Ma3@s5dov3sYheHDoMTQRnBDv0OLYNaEdaumn8IQgDOFYSwsXvIquCi3M9wfjs5ckTqd5cUAffolb1dbmU0VOfdce3C1OwwsskNFIicmRyJBGlRjKBWaoeRmVOGzRzBSoWIxph5WhFFMuMJfpzrBQYokFS7Ny5FX1j1Ei1h8 "R – Try It Online")
### Walkthrough
```
function(m,n) {           # Input: m - matrix, n - shift
  o <- t(m)               # Transpose the matrix, since R works in column-major order
                          # while our snake goes in row-major order
  i <- nrow(o):1          # Take row indices in reverse
  j <- c(F,T)             # Take even column indices (FALSE, TRUE, FALSE, TRUE, ...)
  o[,j] <- o[i,j]         # "Normalize" the matrix by reversing every second column
  o[(seq(o)+n-1) %%       # Perform the shift: add n-1 to indices,
    length(o)+1] <- o     # Modulo sequence length, and +1 again
  o[,j] <- o[i,j]         # Reverse even columns again to return to snake form
  t(o)                    # Transpose the matrix back to orginal shape and return
}
```
[Answer]
# [Python 3.8 (pre-releasSSSse)](https://docs.python.org/3.8/), 119 bytes
```
lambda m,r,n=-1:[[m[(k:=(j+(s:=r+i)//w)%h)][::n**k][s%w]for i in range(w:=len(m[0]))][::n**j]for j in range(h:=len(m))]
```
An unnamed function accepting `matrix, rotation` which yields the new matrix.  
Uses the opposite rotation sign.
**[Try it online!](https://tio.run/##Rc7bToQwEAbga/sUk5gN7W730F0P2KQ@hLe1FxhBykJLSl00xmfHAdxw8SedyTedab9j6d0pbcNQqNehzpq39wwaHrhTWyG1bjQ9S0WrDe2kChvL9vuerUpmtJRuvT4b3a16U/gAFqyDkLmPnPZS1bmjjT4YdpXVhKoFlf8IxdBkMdgvUKAJaBAc4Ig5Ye4w94ZjWxzw@YRJMY@Yh7mNWqAWqAVqMWpDgo9ZtN7hn9uUkJB3n3XEoqDzLg5XwcgttMG6SGe01IyMJ8/dF99Pp0@FJDezSDgku8pbR5Mf@Xz8TXY4gQvohcE4e1lm8APGhj8 "Python 3.8 (pre-release) – Try It Online")**
### How?
We set `n=-1` upfront to save on parentheses later and take the matrix as `m` and the rotation as `r`.
A new matrix is constructed with the same dimensions as `m` - with a width of `w` (`w:=len(m[0])`) and a height of `h` (`h:=len(m)`).
Every other row of this matrix is reversed (`[::n**j]`).
The values are looked up by calculating their row and column in the original, `m` using the current elements row, `i`, and column, `j`...
We set `s` to `r+i` and `k` to `(j+s//w)%h`. `k` is the row of the original to access for our current element.
In order to easily access odd indexed rows from the right we reverse such rows before accessing their elements (with `[:n**k]`), this means the element of interest is at `s%w`.
[Answer]
# [J](http://jsoftware.com/), ~~41~~ ~~30~~ 21 bytes
-11 bytes thanks to Jonah!
-9 bytes thanks to FrownyFrog & ngn !
```
$@]t@$(|.,@t=.|.@]/\)
```
[Try it online!](https://tio.run/##y/qvpKeepmBrpaCuoKNgoGAFxLp6Cs5BPm7/VRxiSxxUNGr0dBxKbPVq9Bxi9WM0/2tyKXClJmfkKyjEGyqkKZgoaCtk6hkpGEEE1dWhkgrGQEkj7Uw9YwUTVKl4QwOQlIKJioWChYI5EJoCoZmC2X8A "J – Try It Online")
Reversed `+/-` 
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), 102 bytes
Takes input as `(matrix)(integer)`. The meaning of the sign of the integer is inverted.
```
m=>n=>(g=m=>m.map(r=>r.sort(_=>~m,m=~m)))(m.map(r=>r.map(_=>a[(l+n++%l)%l]),l=(a=g(m).flat()).length))
```
[Try it online!](https://tio.run/##rZDLboNADEX3fIU3kWzhjDIQSLIwP4JQNUqBtppHBCjL/DoBkrRVJXaVvLD8OPfaX@Zq@nP3eRm2PrzXYyOjk8JLga1MiVPOXLCTolN96AZ8k@Lm2MnNERH@6s7J1DQl2tjH8cbSxlbEVtBIi45UY82ARMrWvh0@iMZz8H2wtbKhxQbLCKAEzZAwpAx7hqziuaZ3DCeGI8OBIX/WNOuEdcp6zzqroopwmxJF/4tMVog/IMgnxENerwz/1V5WFu3Tt5mX9kJau@P4Mnx4gjKeI58veDjYTU@9Aw "JavaScript (Node.js) – Try It Online")
### Helper function
The helper function \$g\$ is used to 'snakify' or 'unsnakify' a matrix by reversing rows at odd indices.
```
g = m =>        // m[] = input matrix
  m.map(r =>    // for each row r[] in m[]:
    r.sort(_ => //   sort r[]:
      ~m,       //     using either 0 (don't reverse) or -1 (reverse)
      m = ~m    //     and toggling m before each iteration
                //     (on the 1st iteration: m[] is coerced to 0, so it yields -1)
    )           //   end of sort()
  )             // end of map()
```
### Main function
```
m => n =>                    // m[] = matrix, n = integer
  g(                         // invoke g on the final result
    m.map(r =>               //   for each row r[] in m[]:
      r.map(_ =>             //     for each entry in r[]:
        a[(l + n++ % l) % l] //       get the rotated value from a[]; increment n
      ),                     //     end of inner map()
      l = (                  //     l is the length of a[]:
        a = g(m).flat()      //       a[] is the flatten result of g(m)
      ).length               //       (e.g. [[1,2],[3,4]] -> [[1,2],[4,3]] -> [1,2,4,3])
    )                        //   end of outer map()
  )                          // end of call to g
```
[Answer]
# Pyth, 20 bytes
```
L.e_W%k2bbyclQ.>syQE
```
Try it online [here](https://pyth.herokuapp.com/?code=L.e_W%25k2bbyclQ.%3EsyQE&input=%5B%5B2%2C%20%203%2C%20%204%2C%20%205%5D%2C%5B6%2C%20%207%2C%20%208%2C%20%209%5D%2C%5B10%2C%2011%2C%2012%2C%2013%5D%5D%0A-3&debug=0).
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 16 bytes
```
εNFR]˜²._¹gäεNFR
```
[Try it online!](https://tio.run/##yy9OTMpM/f//3FY/t6DY03MObdKLP7Qz/fASsMD//9HRRjoKxjoKJjoKprE6CtFmOgrmOgoWOgqWIJ6hgY6CoSEQAxUZGsfGchkDAA "05AB1E – Try It Online")
Thanks to [Emigna](https://codegolf.stackexchange.com/users/47066/emigna) for -5. Unfortunately, I can't see how to golf the redundant part out. :(
[Answer]
# [Python 3](https://docs.python.org/3/), 94 bytes
```
lambda m,n:g(roll(g(m),n))
g=lambda b:[b[i][::(-1)**i]for i in r_[:len(b)]]
from numpy import*
```
[Try it online!](https://tio.run/##LY7NasMwEITP0VPsrZKZQBTHPxX4SVRR7LR2BJZkhEIIfXjXinPb@WZnmOWZbsGX69h9rXPvhp@eHLyaeAzzzCfuBLwQbOre5qD0oK3RSvGjFEVhzRgiWbKe4rdW86/ngzCGjTE48ne3PMm6JcRUrOnaaca1vqAyYES6RmPyJQUypzOoBF1Ab59qUANqQZ87kCdICXmGLF/BY7knW7RotrLXU4UKNeq9@SQMY3mh6xN8Xpmuih0yieGR9WYoWqL1iRfjx99GBTvsWqz/ "Python 3 – Try It Online")
Used the odd-row-reversal from [Jonathan Allan's answer](https://codegolf.stackexchange.com/a/183181/81203).
```
lambda m,n:g(roll(g(m),n))  #reverse odd rows, shift elements, then reverse odd rows again.
g=lambda b:[b[i][::(-1)**i] #reverse odd rows
    for i in r_[:len(b)]]   #r_[:x] = range(x)
from numpy import*          #roll() and r_[]
```
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 36 bytes
```
ＦＥθ⎇﹪κ²⮌ιιＦι⊞υκＩＥ⪪Ｅυ§υ⁻κηＬ§θ⁰⎇﹪κ²⮌ιι
```
[Try it online!](https://tio.run/##jY7NCoMwEITvPsUeE4jQaH/xVHoqVJC2t@AhaFqDEm2i0j59uiq99/DBMDs7TFFJW7Sy8f7RWiCp7MiLwV1ZI@2HpG05NC2pGUSUwVWNyjpFNGpNKYX5RVPIBleRgUFNkyCz2vTkJF0/l926Ri8K78f@bEr1nmSqzeCm4gqLGFyUefYV@QVwwmr2/xtCE@@FEBEDiJE1sslZACC2KHfIHjksFl8x4BzBOI/zyQzj3Idj8wU "Charcoal – Try It Online") Link is to verbose version of code. Explanation:
```
Ｅθ⎇﹪κ²⮌ιι
```
Reverse alternate rows of the input.
```
Ｆ...Ｆι⊞υκ
```
Flatten the array.
```
Ｅυ§υ⁻κη
```
Rotate the flattened array.
```
⪪...Ｌ§θ⁰
```
Split the array back into rows.
```
Ｅ...⎇﹪κ²⮌ιι
```
Reverse alternate rows.
```
Ｉ...
```
Convert each entry to string and output in the default output format which is one number per line with rows double-spaced. (Formatting with a separator would cost the length of the separator.)
[Answer]
# [Japt](https://github.com/ETHproductions/japt), 28 bytes
```
mÏ%2©XÔªX
c éV òUÎl
W©UªßV1V
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&flags=LVE&code=bc8lMqlY1KpYCmMg6VYg8lXObApXqVWq31YxVg&input=W1s0LCA1XSwKIFs2LCA3XV0KMQ)
Port of [Arnauld's answer](https://codegolf.stackexchange.com/a/183166/8340). The biggest challenge was creating a reusable function. In particular, there is a helper function to reverse every other row. The approach that I am taking is to make a recursive call and depending on whether a variable is set.
Transpiled JS:
```
// U: first input argument (matrix)
// m: map it through a function
U = U.m(function(X, Y, Z) {
  // if the row index is even, don't alter it
  // if the row index is odd, reverse it (w)
  return Y % 2 && X.w() || X
});
V = U
  // flatten the matrix
  .c()
  // shift by the amount specified in second argument
  .é(V)
  // partition back to matrix
  .ò(
    // the number of columns should be the same as input
    U.g().l()
  );
// if W is specified, return the result from the first line
W && U ||
  // otherwise, make a recursive call with the shifted matrix
  rp(V, 1, V)
```
[Answer]
# [APL (Dyalog Classic)](https://www.dyalog.com/), 20 bytes
```
↑∘t⊢∘⍴⍴⌽∘∊∘(t←⊢∘⌽\↓)
```
[Try it online!](https://tio.run/##NY09CsJAEIV7TzFddgoxm7ib6DksbYISCQYixkbESgn@RRQRbPUCNtoINh5lLhJnV4RldvZ97@2LRmm9P43SbFDvpVGeJ72K9ucko@LgVjFPKo60ukxoc@OLyoc5u7fZVxueYmI8P7h7d6k4YVV1WJtR@aT1dWzxIm5Q@Wo72dCh7dKJoyR1WGDLeF773KXgFtEEhUJDgAgd89bQRBGwWPMt98CHvwdCaKGQLkgJ0gPpI9qMghajgJFnkQuadx9sFk2Va/8Kjcv4FCjQbPrHFYPQVGjLvw "APL (Dyalog Classic) – Try It Online")
[Answer]
# [C# (Visual C# Interactive Compiler)](http://www.mono-project.com/docs/about-mono/releases/5.0.0/#csc), 141 bytes
```
a=>n=>{for(dynamic l=a.Length,w=a.GetLength(1),i=l,j,b=a.Clone();i-->0;)a[(j=(i+n%l+l)%l)/w,j/w%2<1?j%w:w-j%w-1]=b[i/w,i/w%2<1?i%w:w-i%w-1];}
```
[Try it online!](https://tio.run/##bVFNa4NAEL37K5aAsItj4ybNR6NrKYX2knsPksPGmmYWu4FoK0X87XbUtEkgl@XNvDdvPjYt/LTA9uXLphHaMoENPKUlHmwXxfFOtVrFVsX17nDk7z9Wf2LKcqXv1pn9KPdQEXzNyiHiUgCqHAxsKf2cH2zGRYi@Hweh0Ak3iqNn3dzLhZuLcQVmXLmTSD4at1pVPr2@3KhtgkThicKewp4KmzZ0vvWRlVlRFkwxm1XJhtUO1yuCbFigdhir72HWQAfmsCDQgF3RcDeEE5jCpRiW8DAEMgApQU5ATk8O/vSmxZJqFn0bCmYwgznM/3oGVNGEDl0v0@med8NzDVZQ/bCFYJ3HjmvBrQg7SIcmc4YqCDG6vG5At/S8oeCsM6Qz0fUvhOasY@ztiGXGdYJgNt6Ijfo2jDXOP7nG/qecIXmVatpf "C# (Visual C# Interactive Compiler) – Try It Online")
-5 bytes total thanks to @someone!
Anonymous function that performs an in-place modification to the input matrix.
An single loop iterates over the cells. You can scan from top to bottom and left to right using the following formulas:
* `row=i/w`
* `col=i%w`
Where `i` is a loop counter and `w` is the number of columns. This is varies slightly when scanning in a snake pattern.
* `row=i/w`
* `col=i%w` (0th, 2nd, 4th, etc. row)
* `col=w-i%w-1` (1st, 3nd, 5th, etc. row)
Another thing to note is that the `%` in C# does not convert to a positive value like it does in some other languages. A couple extra bytes are needed to account for this.
```
// a: input matrix
// n: number of cells to rotate
a=>n=>{
  for(
    // l: total number of cells
    // w: number of columns
    // i: loop index
    // j: offset index
    // b: copy of input matrix
    dynamic
      l=a.Length,
      w=a.GetLength(1),
      i=l,j,
      b=a.Clone();
    // iterate from i down to 0
    i-->0;
  )
    // calculate the offset `j` and use
    // the above formulas to index
    // into `a` for setting a value
    a[
      (j=(i+n%l+l)%l)/w,
      j/w%2<1?j%w:w-j%w-1
    ]=
    // use the un-offset index `i` and
    // the above formulas to read a
    // value from the input matrix
    b[
      i/w,
      i/w%2<1?i%w:w-i%w-1
    ];
}
```
]