#include <stdio.h>

int main(void) {
	int n = 4, m = 4;
	int arr[4][4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
	
	int top = 0, bottom = n-1, right = m-1, left = 0, i=0, j=0;
	
	printf("%d ", arr[i][j]);
	
	while(1){
		if(top>bottom || left>right) break;
		
		
		if(bottom==top){
			for(j=left; j<=right; j++)
				printf("%d ", arr[top][j]);
			break;
		}
		
		if(right==left){
			for(i=top; i<=bottom; i++)
				printf("%d ", arr[left][i]);
			break;
		}
		
		int first = 1;
		while(j<=right){
			if(first){
				j++;
				first = 0;
				continue;
			}
			
			printf("%d ", arr[i][j]);
			j++;
		}
		j--; right--;
		
		first = 1;
		while(i<=bottom){
			if(first){
				i++;
				first = 0;
				continue;
			}			
			
			printf("%d ", arr[i][j]);
			i++;
		}
		i--; bottom--;

		first = 1;
		while(j>=left){
			if(first){
				j--;
				first = 0;
				continue;
			}
			
			printf("%d ", arr[i][j]);
			j--;
		}
		j++; left++;
		
		first = 1;
		while(i>=top+1){
			if(first){
				i--;
				first = 0;
				continue;
			}
			
			printf("%d ", arr[i][j]);
			i--;
		}
		i++; top++;
	}
	
	return 0;
}
