```
public void rotate(int[][] matrix) {
int m = matrix.length;
int p, q;
int x, y, temp;
for (int i = 0; i < m/2; i++) { // from outer square to inner square
for (int j = i; j < m - i - 1; j++) { // read top line of the square
x = i;
y = j;
p = matrix[x][y];
// rotate
for (int k = 0; k < 4; k++) {
q = matrix[y][m-x-1];
matrix[y][m-x-1] = p;
p = q;
temp = y;
y = m - x - 1;
x = temp;
}
}
}
}
```