n = int(input())
l = []
for i in range(n):
    l.extend(list(map(int, input().split())))
i = j = 0
for i in range(n):
    for j in range(n):
        if i > j and l[i*n+j]:
            print('NO')
            break
    if j < n -1:
        break
if i == j and i == n-1:
    print('YES')

发现else可以解决跳出多层循环:
n = int(input())
l = []
for i in range(n):
    l.extend(list(map(int, input().split())))
for i in range(n):
    for j in range(n):
        if i > j and l[i*n+j]:
            print('NO')
            break
    else:
        continue
    break
else:
    print('YES')