Given a non null integer matrix Grid of dimensions NxM.Calculate the sum of its elements.
Example 1:
Input:
N=2,M=3
Grid=
[[1,0,1],
[-8,9,-2]]
Output:
1
Explanation:
The sum of all elements of the matrix is 
(1+0+1-8+9-2)=1.
Example 2:
Input:
N=3,M=5
Grid=
[[1,0,1,0,1],
[0,1,0,1,0],
[-1,-1,-1,-1,-1]]
Output:
0
Explanation:
The sum of all elements of the matrix are
(1+0+1+0+1+0+1+0+1+0-1-1-1-1-1)=0.
Your Task:
You don't need to read input or print anything.Your task is to complete the function sumOfMatrix() which takes two integers N ,M and a 2D array Grid as input parameters and returns the sum of all the elements of the Grid.
Expected Time Complexity:O(N*M)
Expected Auxillary Space:O(1)
Constraints:
1<=N,M<=1000
-1000<=Grid[i][j]<=1000

Source: geeksforgeeks | Difficulty: EASY
Tags: Matrices

Data structures

Starter code: #User function Template for python3



class Solution:

    def sumOfMatrix(self,N,M,Grid):

        #code here

Solutions:
class Solution:

	def sumOfMatrix(self, N, M, Grid):
		sum = 0
		for i in Grid:
			for j in i:
				sum += j
		return sum


class Solution:

	def sumOfMatrix(self, N, M, Grid):
		s = 0
		for i in Grid:
			for ele in i:
				s = s + ele
		return s


class Solution:

	def sumOfMatrix(self, N, M, Grid):
		s = 0
		for i in range(N):
			for j in range(M):
				s += Grid[i][j]
		return s


class Solution:

	def sumOfMatrix(self, n, M, G):
		s = 0
		for i in range(n):
			s += sum(G[i])
		return s


class Solution:

	def sumOfMatrix(self, N, M, Grid):
		return sum(map(sum, Grid))


class Solution:

	def sumOfMatrix(self, N, M, k):
		c = []
		for i in range(N):
			for j in range(M):
				c.append(k[i][j])
		return sum(c)


class Solution:

	def sumOfMatrix(self, N, M, Grid):
		n = 0
		for i in Grid:
			a = sum(i)
			n += a
		return n


class Solution:

	def sumOfMatrix(self, n, m, grid):
		count = 0
		for i in range(n):
			for j in range(m):
				count += grid[i][j]
		return count


class Solution:

	def sumOfMatrix(self, N, M, Grid):
		acum = 0
		for f in Grid:
			for c in f:
				acum += c
		return acum


class Solution:

	def sumOfMatrix(self, N, M, Grid):
		temp = 0
		for lst in Grid:
			for val in lst:
				temp += val
		return temp


class Solution:

	def sumOfMatrix(self, N, M, Grid):
		sum = 0
		for x in range(N):
			for y in range(M):
				sum += Grid[x][y]
		return sum


class Solution:

	def sumOfMatrix(self, N, M, Grid):
		res = 0
		for i in range(N):
			res += sum(Grid[i])
		return res


class Solution:

	def sumOfMatrix(self, n, m, grid):
		sum = 0
		for i in grid:
			for j in i:
				sum += j
		return sum


class Solution:

	def sumOfMatrix(self, N, M, Grid):
		sum = 0
		for row in Grid:
			for element in row:
				sum += element
		return sum


class Solution:

	def sumOfMatrix(self, N, M, Grid):
		ans = []
		sum = 0
		for row in Grid:
			for i in row:
				ans.append(i)
				sum += i
		return sum


class Solution:

	def sumOfMatrix(self, N, M, Grid):
		total = 0
		for I in range(N):
			for J in range(M):
				total += Grid[I][J]
		return total


class Solution:

	def sumOfMatrix(self, N, M, Grid):
		sum_of_elements = 0
		for r in range(N):
			for c in range(M):
				sum_of_elements += Grid[r][c]
		return sum_of_elements


class Solution:

	def sumOfMatrix(self, N, M, Grid):
		l = []
		for i in range(N):
			x = sum(Grid[i])
			l.append(x)
		return sum(l)


class Solution:

	def sumOfMatrix(self, N, M, Grid):
		sum1 = 0
		for I in range(N):
			sum1 += sum(Grid[I])
		return sum1


class Solution:

	def sumOfMatrix(self, N, M, Grid):
		l = []
		for i in range(len(Grid)):
			for j in range(len(Grid[0])):
				l.append(Grid[i][j])
		return sum(l)


import numpy as np

class Solution:

	def sumOfMatrix(self, N, M, Grid):
		sum = 0
		for inner_list in Grid:
			for inner_list_element in inner_list:
				sum += inner_list_element
		return sum


class Solution:

	def sumOfMatrix(self, N, M, Grid):
		a = 0
		for i in range(0, N):
			for j in range(0, M):
				a += int(Grid[i][j])
		return a


class Solution:

	def sumOfMatrix(self, N, M, Grid):
		s = 0
		for r in Grid:
			s += sum(r)
		return s


class Solution:

	def sumOfMatrix(self, N, M, Grid):
		count = 0
		for each in Grid:
			count += sum(each)
		return count


class Solution:

	def sumOfMatrix(self, N, M, Grid):
		soma = 0
		for (i, row) in enumerate(Grid):
			for (j, value) in enumerate(row):
				soma += value
		return soma