心累,一直越界,要判断不为空,print end参数避免换行
打完收工。

m,n = list(map(int,(input().split(' '))))
ans = []
for i in range(m):
    ans.append(list(map(int,(input().split(' ')))))
#ans = [[1,2],[3,4]]
#ans = [[1,2,3],[4,5,6],[7,8,9]]
while ans and ans[0]:
    for row in ans:
        print(row.pop(0),end=' ')
    for i in ans[-1]:
        print(i,end=' ')
    ans.pop(-1)
    n =len(ans)
    for row in range(n):
        if ans[0]:
            print(ans[n-row-1].pop(-1),end=' ')
        else:
            break
    if ans and ans[0]:
        for i in ans[0][::-1]:
            print(i,end=' ')
        ans.pop(0)