When you write "a[k]", do you mean a[i;k] ?
If not, I don't understand the problem.
If yes, your algorithm finds the maximum horizontal finishing continuous sum. And not the maximum sum of a rectangle.
A brute algorithm could be :
A continuous rectangle is defined by 4 integers : 1 =< i =< k =< m and 1 =< j =< l =< n.
(i,k) and (j,l) are the coordonates of 2 opposite corners of the rectangle.
Code
S = 0
For i = 1 to m
For j = 1 to n
For k = i to m
For l = j to n
T = sum in the rectangle (i;k)-(j;l) <- this part requires another, easy, loop
S = max (S;T)
end
end
end
end
Display S
??
To be continued...