#include using namespace std; int main() { int N,M; cin >> N; cin >> M; int** matrix = new int*[M]; for (int ii = 0; ii < M; ii++) { matrix[ii] = new int[N]; for (int jj = 0; jj < N; jj++) { cin >> matrix[ii][jj]; } } int x = 0; int y = 0; int dir = 0; int top = 0, bottom = N - 1, left = 0, right = M - 1; bool done = false; while (!done) { cout << matrix[y][x]; cout << " "; switch (dir) { case 0: if (x == right) { dir++; y++; top++; } else x++; break; case 1: if (y == bottom) { dir++; x--; right--; } else y++; break; case 2: if (x == left) { dir++; y--; bottom--; } else x--; break; case 3: if (y == top) { dir=0; x++; left++; } else y--; break; } if (x > right || x < left || y < top || y > bottom) { done = true; } } }