input: Write a function to calculate the average of elements in a 2-dimensional array. The function should return the average of all elements in the matrix.
  
  def averageMatrix(matrix):
      total_sum = 0
      num_elements = 0
      for row in matrix:
          for element in row:
              total_sum += element
              num_elements += 1
      return total_sum / num_elements
  
  a = [[3,2,6],
       [8,5,9],
       [4,7,1]]
  
  assert averageMatrix(a) == 4.77
  assert averageMatrix([[1,2],[3,4]]) == 2.5
  assert averageMatrix([[]]) == 0
  assert averageMatrix([]) == 0
output: def averageMatrix(matrix):
      total_sum = 0
      num_elements = 0
      for row in matrix:
          for element in row:
              total_sum += element
              num_elements += 1
      if num_elements == 0:
          return 0
      return total_sum / num_elements