#C14869. Maximum Hourglass Sum
Maximum Hourglass Sum
Maximum Hourglass Sum
You are given a 2D matrix of integers. Your task is to find the maximum hourglass sum in the matrix.
An hourglass in the matrix is defined by the pattern:
\(a\ b\ c\)
\(\quad d\)
\(e\ f\ g\)
That is, if you consider a 3x3 submatrix starting at index \((i, j)\), the hourglass sum is given by:
\(S = a + b + c + d + e + f + g\)
If the matrix has fewer than 3 rows or 3 columns, no hourglass can be formed and the answer should be 0.
The input begins with two integers that denote the number of rows and columns of the matrix, followed by the matrix elements.
inputFormat
The first line of input contains two space-separated integers (n) and (m), representing the number of rows and columns of the matrix respectively. This is followed by (n) lines, each containing (m) space-separated integers representing the matrix elements.
outputFormat
Output a single integer: the maximum hourglass sum found in the matrix. If no hourglass can be formed, output 0.## sample
5 5
1 2 3 0 0
0 0 0 0 0
1 1 1 0 0
0 0 2 4 4
0 0 0 2 0
10