Matrix Sum

Easy~10 min

Given a 2D matrix of integers matrix, return the sum of all elements.

Iterate through every row and column to accumulate the total.

Examples

Example 1
Input: matrix = [[1,2],[3,4]]
Output: 10
Explanation: 1 + 2 + 3 + 4 = 10.
Example 2
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: 45
Explanation: The sum of all elements 1 through 9 is 45.
Example 3
Input: matrix = [[-1,2],[-3,4]]
Output: 2
Explanation: -1 + 2 + (-3) + 4 = 2.

Constraints

  • 1 <= matrix.length, matrix[i].length <= 100
  • -1000 <= matrix[i][j] <= 1000
Code
Ctrl+EnterRun|Ctrl+⇧+EnterSubmit
Output

Run your code to see results

Use Cmd+Enter to run