Code Submission Evaluation System Login

CSES - HIIT Open 2018

HIIT Open 2018

Contest start:2018-05-26 11:00:00
Contest end:2018-05-26 16:00:00

Task list | Submit code | Submissions | Messages | Scoreboard | Statistics


History
2018-05-26 14:55:29
2018-05-26 14:44:45
2018-05-26 14:32:08
Task:Grid
Sender:Sloths
Submission time:2018-05-26 14:55:29
Status:READY
Result:TIME LIMIT EXCEEDED

Show test data

Code

n = int(input())
grid = []
for i in range(n):
    row = input()
    grid.append(row)
    
def countColors(grid, n):
    color = 0
    #Columns:
    for column_number in range(n):
        if grid[0][column_number] == '1':
            for row in grid:
                if row[column_number] == '0':
                    if grid[0] != '1'*n:
                        return -1
                    break
            else:
                color += 1

    if color == n:
        return color
    #Rows:
    for row in grid:
        if row == '1'*n:
            color += 1
        else:
            for i in range(n):
                if row[i] == '1':
                    for j in range(n):
                        if grid[j][i] == '0':
                            return -1

    if color == 0:
        for row in grid:
            if row != '0'*n:
                return -1
    return color

print(countColors(grid, n))