Geeksforgeeks

Timer is Running

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

Author: Yash Kodesia 1

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

jainshubham52535 | 529 |

Pathak1899 | 436 |

yashdip123 | 380 |

kirtidee18 | 377 |

jots62 | 351 |

kirtidee18 | 1057 |

ykaushik | 752 |

aggarwalvatsal5 | 741 |

harbilassingh | 576 |

pmr_3689 | 570 |

blackshadows | 5456 |

Ibrahim Nash | 5450 |

akhayrutdinov | 5111 |

mb1973 | 4976 |

Quandray | 4827 |

Login to report an issue on this page.