Sobes.tech
Junior — Senior

Counting the number of zeros in a sorted square matrix

livecode

Task condition

Implement the function count_zeros(), which takes a square matrix filled only with the characters 0 and 1. In each row and column, the elements are arranged in non-decreasing order (all zeros are on the left/top, and ones are on the right/bottom). The function should return the total number of zeros present in the entire matrix.

Example

matrix = [
    [0, 0, 1, 1],
    [0, 1, 1, 1],
    [1, 1, 1, 1],
    [1, 1, 1, 1]
]
print(count_zeros(matrix))  # output: 3