using System;
class MatrixBoundary
{
static void PrintMatrixBoundaries(int[,] matrix, int rows, int cols) {
// Print first row
for (int j = 0; j < cols; j++)
Console.Write(matrix[0, j] + " ");
// Print last column
for (int i = 1; i < rows; i++)
Console.Write(matrix[i, cols - 1] + " ");
// Print last row in reverse
for (int j = cols - 2; j >= 0; j--)
Console.Write(matrix[rows - 1, j] + " ");
// Print first column upwards
for (int i = rows - 2; i > 0; i--)
Console.Write(matrix[i, 0] + " ");
}
static void Main()
{
int[,] matrix = {
{ 1, 2, 3, 4, 5 },
{ 6, 7, 8, 9, 10 },
{11, 12, 13, 14, 15 },
{16, 17, 18, 19, 20 }
};
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
PrintMatrixBoundaries(matrix, rows, cols);
}
}
/*
run:
1 2 3 4 5 10 15 20 19 18 17 16 11 6
*/