find all color in image python

>>> from PIL import Image
>>> im = Image.open('polar-bear-cub.jpg')
>>> from collections import defaultdict
>>> by_color = defaultdict(int)
>>> for pixel in im.getdata():
...     by_color[pixel] += 1
>>> by_color
defaultdict(, {(11, 24, 41): 8, (53, 52, 58): 8, (142, 147, 117): 1, (121, 111, 119): 1, (234, 228, 216): 4

Here is what the above code is Doing:
1. We’re using the Image module from the PIL library to open the image.
2. We’re using the collections module to create a defaultdict.
3. We’re iterating over the image’s pixels and counting how many times each color occurs.
4. We’re printing the results.

The output of the above code is a dictionary where the keys are the colors and the values are the number of times that color occurs.

Let’s say we want to find the most common color in the image. We can do that by sorting the dictionary by value:

>>> sorted(by_color.items(), key=lambda x: x[1], reverse=True)
[((11, 24, 41), 8), ((53, 52, 58), 8), ((234, 228, 216), 4), ((142, 147, 117), 1), ((121, 111, 119), 1)]

The sorted() function takes a list of tuples and sorts them by the second element in each tuple. The key=lambda x: x[1] tells sorted() to use the value as the key. The reverse=True tells sorted() to sort the list in reverse order.

The output is a list of tuples sorted by value in reverse order. The first tuple is the most common color, and the last tuple is the least common color.

In this case, the most common color is (11, 24, 41), which is a dark blue. The least common color is (121, 111, 119), which is a very light blue.

If we only want the color, we can use a list comprehension:

>>> [x[0] for x in sorted(by_color.items(), key=lambda x: x[1], reverse=True)]
[(11, 24, 41), (53, 52, 58), (234, 228, 216), (142, 147, 117), (121, 111, 119)]

The output is a list of the most common colors, sorted from most common to least common.

Task
You are given a square image of NxN pixels. Each pixel is either black or white. Your task is to find the number of black pixels.

Input Format
The first line contains an integer N, the number of pixels in a row (and in a column).
N lines follow, each containing N characters. Each character is either 0 or 1, where 0 represents a white pixel and 1 represents a black pixel.

Constraints
1≤N≤100
Output Format
Print the number of black pixels.

Sample Input
5
00000
00000
00001
00010
00010

Sample Output
3

Explanation
There are 3 black pixels in the 5×5 image.