input: Write a function to calculate the average of the elements in a 2-dimensional array. If the array is empty, return 0.
  
  def averageMatrix(matrix):
      if not matrix:
          return 0
      total_sum = 0
      total_elements = 0
      for row in matrix:
          total_sum += sum(row)
          total_elements += len(row)
      return total_sum / total_elements
  
  assert averageMatrix([[3, 2, 6], [8, 5, 9], [4, 7, 1]]) == 4.777777777777778
  assert averageMatrix([]) == 0
  assert averageMatrix([[1, 2], [3, 4]]) == 2.5
  assert averageMatrix([[0]]) == 0
  assert averageMatrix([[5], [5]]) == 5
output: def averageMatrix(matrix):
      if not matrix:
          return 0
      total_sum = 0
      total_elements = 0
      for row in matrix:
          total_sum += sum(row)
          total_elements += len(row)
      return total_sum / total_elements