Given a matrix of order nxm, composed of only 0's and 1's, find the number of 1's in the matrix that are surrounded by an even number (>0) of 0's. The surrounding of a cell in the matrix is defined as the elements above, below, on left, on right as well as the 4 diagonal elements around the cell of the matrix. Hence, the surrounding of any matrix elements is composed of at max 8 elements. Find the number of such 1's
 
Example 1:
Input: matrix = {{1, 0, 0}, {1, 1, 0}, 
{0, 1, 0}}
Output: 1
Explanation: 1 that occurs in the 1st row 
and 1st column, has 3 surrounding elements
0,1 and 1. The occurrence of zero is odd. 
1 that occurs in 2nd row and 1st column has
5 surrounding elements 1,0,1,1 and 0. The 
occurrence of zero is even. 1 that occurs 
in 2nd row and 2nd column has 8 surrounding 
elements. The occurrence of 0 is odd. 
Similarly, for the 1 that occurs in 3rd row 
and 2nd column, the occurrence of zero in 
it's 5 surrounding elements is odd. 
Hence, the output is 1.
Example 2:
Input: matrix = {{1}}
Output: 0
Explanation: There is only 1 element in the 
matrix. Hence, it has no surroundings, so 
it's count for even 0's is 0 for the whole 
matrix. 0 is even but we want occurrence of 
a zero in the surrounding at least once. 
Hence, output is 0.
 
Your Task:
You don't need to read or print anything. Your task is to complete the function Count() which takes the matrix as input parameter and returns the number of 1's which are surrounded by even number of 0's.
 
Expected Time Complexity: O(n * m)
Expected Space Complexity: O(1)
 
Constraints:
1 <= n, m <= 100

Difficulty: EASY
Tags: Matrices

Data structures
Skill types: Data structures
Expected Time Complexity: O(n * m)
Expected Auxiliary Space: O(1)

Starter code:
#User function Template for python3

class Solution:
	def Count(self, matrix):
		# Code here

Solutions:
class Solution:

	def Count(self, arr):
		res = 0
		n = len(arr)
		m = len(arr[0])
		for i in range(n):
			for j in range(m):
				if arr[i][j] == 1:
					count = 0
					if i != 0 and arr[i - 1][j] == 0:
						count += 1
					if i != 0 and j != 0 and (arr[i - 1][j - 1] == 0):
						count += 1
					if i != 0 and j != m - 1 and (arr[i - 1][j + 1] == 0):
						count += 1
					if i != n - 1 and arr[i + 1][j] == 0:
						count += 1
					if i != n - 1 and j != 0 and (arr[i + 1][j - 1] == 0):
						count += 1
					if i != n - 1 and j != m - 1 and (arr[i + 1][j + 1] == 0):
						count += 1
					if j != 0 and arr[i][j - 1] == 0:
						count += 1
					if j != m - 1 and arr[i][j + 1] == 0:
						count += 1
					if count % 2 == 0 and count > 0:
						res += 1
		return res


class Solution:

	def Count(self, matrix):
		n = len(matrix)
		m = len(matrix[0])
		count = 0
		for i in range(n):
			for j in range(m):
				c_count = 0
				if matrix[i][j] == 1:
					top = [i - 1, j]
					down = [i + 1, j]
					left = [i, j - 1]
					right = [i, j + 1]
					top_left = [i - 1, j - 1]
					top_right = [i - 1, j + 1]
					down_left = [i + 1, j - 1]
					down_right = [i + 1, j + 1]
					if top[0] >= 0:
						if matrix[top[0]][top[1]] == 0:
							c_count += 1
					if down[0] < n:
						if matrix[down[0]][down[1]] == 0:
							c_count += 1
					if left[1] >= 0:
						if matrix[left[0]][left[1]] == 0:
							c_count += 1
					if right[1] < m:
						if matrix[right[0]][right[1]] == 0:
							c_count += 1
					if top_left[0] >= 0 and top_left[1] >= 0:
						if matrix[top_left[0]][top_left[1]] == 0:
							c_count += 1
					if top_right[0] >= 0 and top_right[1] < m:
						if matrix[top_right[0]][top_right[1]] == 0:
							c_count += 1
					if down_left[0] < n and down_left[1] >= 0:
						if matrix[down_left[0]][down_left[1]] == 0:
							c_count += 1
					if down_right[0] < n and down_right[1] < m:
						if matrix[down_right[0]][down_right[1]] == 0:
							c_count += 1
					if c_count % 2 == 0 and c_count != 0:
						count += 1
		return count


def numofneighbour(mat, i, j):
	(R, C) = (len(mat), len(mat[0]))
	count = 0
	if i > 0 and (not mat[i - 1][j]):
		count += 1
	if j > 0 and (not mat[i][j - 1]):
		count += 1
	if i < R - 1 and (not mat[i + 1][j]):
		count += 1
	if j < C - 1 and (not mat[i][j + 1]):
		count += 1
	if i > 0 and j > 0 and (not mat[i - 1][j - 1]):
		count += 1
	if i > 0 and j < C - 1 and (not mat[i - 1][j + 1]):
		count += 1
	if i < R - 1 and j > 0 and (not mat[i + 1][j - 1]):
		count += 1
	if i < R - 1 and j < C - 1 and (not mat[i + 1][j + 1]):
		count += 1
	return count

def find(mat):
	(R, C) = (len(mat), len(mat[0]))
	perimeter = 0
	for i in range(0, R):
		for j in range(0, C):
			if mat[i][j]:
				temp = numofneighbour(mat, i, j)
				perimeter += 1 if temp > 0 and temp % 2 == 0 else 0
	return perimeter

class Solution:

	def Count(self, matrix):
		return find(matrix)


class Solution:

	def Count(self, matrix):
		output = 0
		n = len(matrix)
		m = len(matrix[0])
		for i in range(n):
			for j in range(m):
				if matrix[i][j]:
					count = 0
					count += i > 0 and matrix[i - 1][j] == 0
					count += i < n - 1 and matrix[i + 1][j] == 0
					count += j > 0 and matrix[i][j - 1] == 0
					count += j < m - 1 and matrix[i][j + 1] == 0
					count += i > 0 and j > 0 and (matrix[i - 1][j - 1] == 0)
					count += i > 0 and j < m - 1 and (matrix[i - 1][j + 1] == 0)
					count += i < n - 1 and j > 0 and (matrix[i + 1][j - 1] == 0)
					count += i < n - 1 and j < m - 1 and (matrix[i + 1][j + 1] == 0)
					output += count > 0 and count % 2 == 0
		return output


class Solution:

	def Count(self, matrix):
		n = len(matrix)
		m = len(matrix[0])
		c1 = 0
		for i in range(n):
			for j in range(m):
				if matrix[i][j] == 1:
					c = 0
					if i - 1 > -1:
						if matrix[i - 1][j] == 0:
							c += 1
						if j - 1 > -1:
							if matrix[i - 1][j - 1] == 0:
								c += 1
						if j + 1 < m:
							if matrix[i - 1][j + 1] == 0:
								c += 1
					if j - 1 > -1:
						if matrix[i][j - 1] == 0:
							c += 1
					if j + 1 < m:
						if matrix[i][j + 1] == 0:
							c += 1
					if i + 1 < n:
						if matrix[i + 1][j] == 0:
							c += 1
						if j - 1 > -1:
							if matrix[i + 1][j - 1] == 0:
								c += 1
						if j + 1 < m:
							if matrix[i + 1][j + 1] == 0:
								c += 1
					if c % 2 == 0 and c != 0:
						c1 += 1
		return c1


class Solution:

	def Count(self, matrix):
		res = []
		for i in range(len(matrix)):
			for j in range(len(matrix[0])):
				count = 0
				if matrix[i][j] == 1:
					if i >= 0 and i <= len(matrix) - 1 and (j >= 0) and (j + 1 <= len(matrix[0]) - 1):
						if matrix[i][j + 1] == 0:
							count = count + 1
					if i - 1 >= 0 and i <= len(matrix) - 1 and (j >= 0) and (j <= len(matrix[0]) - 1):
						if matrix[i - 1][j] == 0:
							count = count + 1
					if i >= 0 and i <= len(matrix) - 1 and (j - 1 >= 0) and (j <= len(matrix[0]) - 1):
						if matrix[i][j - 1] == 0:
							count = count + 1
					if i >= 0 and i + 1 <= len(matrix) - 1 and (j >= 0) and (j <= len(matrix[0]) - 1):
						if matrix[i + 1][j] == 0:
							count = count + 1
					if i >= 0 and i + 1 <= len(matrix) - 1 and (j >= 0) and (j + 1 <= len(matrix[0]) - 1):
						if matrix[i + 1][j + 1] == 0:
							count = count + 1
					if i - 1 >= 0 and i <= len(matrix) - 1 and (j >= 0) and (j + 1 <= len(matrix[0]) - 1):
						if matrix[i - 1][j + 1] == 0:
							count = count + 1
					if i - 1 >= 0 and i <= len(matrix) - 1 and (j - 1 >= 0) and (j <= len(matrix[0]) - 1):
						if matrix[i - 1][j - 1] == 0:
							count = count + 1
					if i >= 0 and i + 1 <= len(matrix) - 1 and (j - 1 >= 0) and (j <= len(matrix[0]) - 1):
						if matrix[i + 1][j - 1] == 0:
							count = count + 1
					res.append(count)
		count1 = 0
		for i in res:
			if i % 2 == 0 and i > 0:
				count1 = count1 + 1
		return count1