public class RubiksMatrix
public static void Main()
int[] dimensions = Console.ReadLine().Split().Select(int.Parse).ToArray();
int rowsCount = dimensions[0];
int colsCount = dimensions[1];
int commandsCount = int.Parse(Console.ReadLine());
int[][] matrix = new int[rowsCount][];
for (int rowsIndex = 0; rowsIndex < matrix.Length; rowsIndex++)
matrix[rowsIndex] = new int[colsCount];
for (int coIndex = 0; coIndex < matrix[rowsIndex].Length; coIndex++)
matrix[rowsIndex][coIndex] = num;
for (int i = 0; i < commandsCount; i++)
string[] cmdArgs = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
string direction = cmdArgs[1];
int rotations = int.Parse(cmdArgs[2]);
col = int.Parse(cmdArgs[0]);
RotateUp(matrix, col, rotations);
col = int.Parse(cmdArgs[0]);
RotateDown(matrix, col, rotations);
row = int.Parse(cmdArgs[0]);
RotateRight(matrix, row, rotations);
row = int.Parse(cmdArgs[0]);
RotateLeft(matrix, row, rotations);
private static void RearrangeMatrix(int[][] matrix)
for (int rowIndex = 0; rowIndex < matrix.Length; rowIndex++)
for (int colIndex = 0; colIndex < matrix[rowIndex].Length; colIndex++)
if (matrix[rowIndex][colIndex] != correctNum)
int wrongNum = matrix[rowIndex][colIndex];
for (int searchedRow = rowIndex; searchedRow < matrix.Length; searchedRow++)
for (int searchedCol = 0; searchedCol < matrix[rowIndex].Length; searchedCol++)
if (matrix[searchedRow][searchedCol].Equals(correctNum))
matrix[rowIndex][colIndex] = matrix[searchedRow][searchedCol];
matrix[searchedRow][searchedCol] = wrongNum;
Console.WriteLine($"Swap ({rowIndex}, {colIndex}) with ({searchedRow}, {searchedCol})");
Console.WriteLine("No swap required");
private static void RotateUp(int[][] matrix, int col, int rotations)
for (int i = 0; i < rotations % matrix.Length; i++)
int firstElement = matrix[0][col];
for (int rowIndex = 0; rowIndex < matrix.Length - 1; rowIndex++)
matrix[rowIndex][col] = matrix[rowIndex + 1][col];
matrix[matrix.Length - 1][col] = firstElement;
private static void RotateDown(int[][] matrix, int col, int rotations)
for (int i = 0; i < rotations % matrix.Length; i++)
int lastElement = matrix[matrix.Length - 1][col];
for (int rowIndex = matrix.Length - 1; rowIndex > 0; rowIndex--)
matrix[rowIndex][col] = matrix[rowIndex - 1][col];
matrix[0][col] = lastElement;
private static void RotateRight(int[][] matrix, int row, int rotations)
for (int i = 0; i < rotations % matrix[row].Length; i++)
int lastElement = matrix[row][matrix[row].Length - 1];
for (int colIndex = matrix[row].Length - 1; colIndex > 0; colIndex--)
matrix[row][colIndex] = matrix[row][colIndex - 1];
matrix[row][0] = lastElement;
private static void RotateLeft(int[][] matrix, int row, int rotations)
for (int i = 0; i < rotations % matrix[row].Length; i++)
int firstElement = matrix[row][0];
for (int colIndex = 0; colIndex < matrix[row].Length - 1; colIndex++)
matrix[row][colIndex] = matrix[row][colIndex + 1];
matrix[row][matrix[row].Length - 1] = firstElement;