#include <bits/stdc++.h>
using namespace std;
void printSpiralAnti(int **arr, int m, int n)
{
int startRow = 0;
int startCol = 0;
int endRow = m - 1;
int endCol = n - 1;
while (startRow <= endRow && startCol <= endCol)
{
for (int i = startRow; i <= endRow; i++)
{
cout << arr[i][startCol] << ", ";
}
startCol++;
for (int i = startCol; i <= endCol; i++)
{
cout << arr[endRow][i] << ", ";
}
endRow--;
if (endCol > startCol)
{
for (int i = endRow; i >= startRow; i--)
{
cout << arr[i][endCol] << ", ";
}
endCol--;
}
if (endRow > startRow)
{
for (int i = endCol; i >= startCol; i--)
{
cout << arr[startRow][i] << ", ";
}
startRow++;
}
}
cout << "END";
}
int main()
{
int m, n;
cin >> m >> n;
int **arr = new int *[n];
for (int i = 0; i < m; i++)
{
arr[i] = new int[n];
}
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
cin >> arr[i][j];
}
}
printSpiralAnti(arr, m, n);
return 0;
}