Consider the column and row numbers of a cell to be the coordinates of a point in a Cartesian plane. In your illustration, these could be the coordinates of the center of the cell. You need to transform the coordinates in order to rotate the set of cells in the plane. But your transformation also needs to increase the distance between centers of cells by a factor of $\sqrt{2}$, because as you can see in your figure, instead of moving just one unit right to get from $a_{11}$ to $a_{12}$, now it is one unit right and one unit down. So what you need is not just a rotation; it is a rotation with dilation.
The formula for doing a rotation of angle $\theta$ and dilation by factor $k$ around the point $(0,0)$ is
$$
\left( \begin{array}{c} x \\ y \end{array} \right)
\rightarrow
k \left( \begin{array}{cc}
\cos \theta & \sin \theta \\
-sin\theta & \cos\theta
\end{array} \right)
\left( \begin{array}{c} x \\ y \end{array} \right).
$$
In your case, for a $45$-degree rotation, $\theta$ is either $\pi/4$ or $-\pi/4$ (depending on the direction of rotation) and $k = \sqrt{2}$. It turns out to be $\pi/4$ for what you're doing, but you could figure out which angle is correct by trial and error. So $\sin\theta = \cos\theta = \sqrt{2}/2$, and if you plug all these numbers into the formula and distribute the factor over the $2\times2$ array, you get
$$
\left( \begin{array}{c} x \\ y \end{array} \right)
\rightarrow
\left( \begin{array}{cc}
1 & 1 \\
-1 & 1
\end{array} \right)
\left( \begin{array}{c} x \\ y \end{array} \right)
=
\left( \begin{array}{c} x + y \\ -x + y \end{array} \right).
$$
If you just apply that transformation, it says to copy the contents of cell $M[x][y]$ to the new location $RM[x+y][-x+y]$. This would solve your problem, except that it copies $M[1][1]$ to $RM[0][2]$, which we don't normally consider to be a valid matrix cell. In fact, it copies all the cells below $M[1][1]$ even further to the left: if the original matrix has $n$ rows, this rotation will copy the first column of $M$ to the $n$ “columns” to the left of the first column of $RM$. It also copies all cells to rows numbered $2$ or greater, and you want to put a non-zero cell on row $1$.
To fix these defects in the new array, you need to move everything one row up and $n$ columns to the right, that is, subtract one from the row number and add $n$ to the column number. So instead of $RM[x+y][-x+y]$, you want to copy cell $M[x][y]$ to cell $RM[x+y+1][-x+y+n]$. That's one way to derive your formula.
Or you could observe that the “rotation” of the matrix preserves collinearity and relative distances, so it must be a linear transformation, and work out the coefficients of two linear functions of $x$ and $y$ that move $a_{11}$ and $a_{12}$ where they need to go.
As for what to do with a rectangular matrix, notice the phrase “if the original matrix has $n$ rows” in the text above. The “rotation” formula does not depend on the number of columns; more columns (or fewer) will just cause the new matrix to extend farther (or less far) to the lower right where there are always positive row and column numbers. Just set $n$ in the formula to the number of rows you find in $M$, and you'll be OK.