Given a binary matrix  having n rows and m columns, your task is to find the sum of coverage of all zeros in the matrix where coverage for a particular 0 is defined as total number of ones around a zero in left, right, up and bottom directions.
 
Example 1:
Input: matrix = {{0, 1, 0},
{0, 1, 1}, {0, 0, 0}}
Output: 6
Example 2:
Input: matrix = {{0, 1}}
Output: 1
 
Your Task:
You don't need to read or print anyhting. Your task is to complete the function FindCoverage() which takes the matrix as input parameter and returns sum of coverage of all zeros in the matrix.
 
Expected Time Complexity: O(n * m)
Expected Space Complexity: O(1)
 
Constraints:
1 <= n, m <= 100

Source: geeksforgeeks | Difficulty: EASY
Tags: Matrices

Data structures

Starter code: #User function Template for python3

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

Solutions:
class Solution:

	def FindCoverage(self, matrix):
		coverageTotal = 0
		for i in range(len(matrix)):
			for j in range(len(matrix[0])):
				if matrix[i][j] == 0:
					if j - 1 >= 0 and matrix[i][j - 1] == 1:
						coverageTotal += 1
					if j + 1 < len(matrix[0]) and matrix[i][j + 1] == 1:
						coverageTotal += 1
					if i - 1 >= 0 and matrix[i - 1][j] == 1:
						coverageTotal += 1
					if i + 1 < len(matrix) and matrix[i + 1][j] == 1:
						coverageTotal += 1
		return coverageTotal


class Solution:

	def FindCoverage(self, matrix):
		count = 0
		for i in range(n):
			for j in range(m):
				if matrix[i][j] == 0:
					if i - 1 >= 0:
						if matrix[i - 1][j] == 1:
							count += 1
					if j - 1 >= 0:
						if matrix[i][j - 1] == 1:
							count += 1
					if i + 1 <= n - 1:
						if matrix[i + 1][j] == 1:
							count += 1
					if j + 1 <= m - 1:
						if matrix[i][j + 1] == 1:
							count += 1
		return count


class Solution:

	def FindCoverage(self, matrix):
		n = len(matrix)
		m = len(matrix[0])
		count = 0
		for i in range(n):
			for j in range(m):
				if matrix[i][j] == 0:
					top = [i - 1, j]
					down = [i + 1, j]
					left = [i, j - 1]
					right = [i, j + 1]
					if top[0] >= 0:
						if matrix[top[0]][top[1]] == 1:
							count += 1
					if down[0] < n:
						if matrix[down[0]][down[1]] == 1:
							count += 1
					if left[1] >= 0:
						if matrix[left[0]][left[1]] == 1:
							count += 1
					if right[1] < m:
						if matrix[right[0]][right[1]] == 1:
							count += 1
		return count


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

class Solution:

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


class Solution:

	def FindCoverage(self, matrix):
		row = 0
		col = 0
		dir = 'right'
		N = len(matrix)
		M = len(matrix[0])
		count = 0
		while row < N:
			r = row
			c = col
			while c < M:
				if dir == 'right' and c != M - 1 and (matrix[r][c] == 0):
					if matrix[r][c + 1] == 1:
						count += 1
				dir = 'left'
				if dir == 'left' and c != 0 and (matrix[r][c] == 0):
					if matrix[r][c - 1] == 1:
						count += 1
				dir = 'up'
				if dir == 'up' and r != 0 and (matrix[r][c] == 0):
					if matrix[r - 1][c] == 1:
						count += 1
				dir = 'down'
				if dir == 'down' and r != N - 1 and (matrix[r][c] == 0):
					if matrix[r + 1][c] == 1:
						count += 1
				dir = 'right'
				c += 1
			row += 1
			col = 0
		return count


class Solution:

	def FindCoverage(self, matrix):
		m = len(matrix[0])
		n = len(matrix)
		c = 0
		for i in range(n):
			for j in range(m):
				if matrix[i][j] == 0:
					r1 = 0
					if i - 1 > -1:
						if matrix[i - 1][j] == 1:
							c += 1
					r2 = 0
					if i + 1 < n:
						if matrix[i + 1][j] == 1:
							c += 1
					r3 = 0
					if j - 1 > -1:
						if matrix[i][j - 1] == 1:
							c += 1
					r4 = 0
					if j + 1 < m:
						if matrix[i][j + 1] == 1:
							c += 1
		return c


class Solution:

	def FindCoverage(self, matrix):
		R = len(matrix)
		C = len(matrix[0])
		cnt = 0
		for i in range(R):
			for j in range(C):
				if matrix[i][j] == 0:
					if i - 1 >= 0:
						if matrix[i - 1][j] == 1:
							cnt += 1
					if j - 1 >= 0:
						if matrix[i][j - 1] == 1:
							cnt += 1
					if i + 1 < R:
						if matrix[i + 1][j] == 1:
							cnt += 1
					if j + 1 < C:
						if matrix[i][j + 1] == 1:
							cnt += 1
		return cnt


class Solution:

	def FindCoverage(self, m):
		c = 0
		n = len(m)
		p = len(m[0])
		s = {}
		for i in range(n):
			for j in range(p):
				if m[i][j] == 0:
					if i - 1 >= 0 and m[i - 1][j] == 1:
						c += 1
					if j - 1 >= 0 and m[i][j - 1] == 1:
						c += 1
					if i + 1 < n and m[i + 1][j] == 1:
						c += 1
					if j + 1 < p and m[i][j + 1] == 1:
						c += 1
		return c


class Solution:

	def FindCoverage(self, mat):
		count = 0
		(m, n) = (len(mat), len(mat[0]))
		for i in range(m):
			for j in range(n):
				if mat[i][j] == 0:
					if i - 1 >= 0 and mat[i - 1][j] == 1:
						count += 1
					if i + 1 < m and mat[i + 1][j] == 1:
						count += 1
					if j - 1 >= 0 and mat[i][j - 1] == 1:
						count += 1
					if j + 1 < n and mat[i][j + 1] == 1:
						count += 1
		return count


class Solution:

	def FindCoverage(self, mat):
		top = 0
		left = 0
		right = len(mat[0])
		bottom = len(mat)
		total = 0
		c = len(mat[0])
		r = len(mat)
		for k in range(c * r):
			i = k // c
			j = k % c
			if mat[i][j] == 0:
				if i - 1 >= top:
					if mat[i - 1][j] == 1:
						total += 1
				if i + 1 < bottom:
					if mat[i + 1][j] == 1:
						total += 1
				if j + 1 < right:
					if mat[i][j + 1] == 1:
						total += 1
				if j - 1 >= left:
					if mat[i][j - 1] == 1:
						total += 1
		return total


class Solution:

	def FindCoverage(self, matrix):
		n = len(matrix)
		m = len(matrix[0])
		res = 0
		for i in range(n):
			for j in range(m):
				if matrix[i][j] == 0:
					if i != 0 and matrix[i - 1][j] == 1:
						res += 1
					if i != n - 1 and matrix[i + 1][j] == 1:
						res += 1
					if j != 0 and matrix[i][j - 1] == 1:
						res += 1
					if j != m - 1 and matrix[i][j + 1] == 1:
						res += 1
		return res