You don't need any custom function to calculate the $divisor of the $matrix, using array_map() and array_sum() does the trick:
<?php
$matrix = array
(
array(-1, -1, -1),
array(-1, 16, -1),
array(-1, -1, -1),
);
$divisor = array_sum(array_map('array_sum', $matrix)); // 8
?>