#include <iostream>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
int arr[n][m];
for (int i = 0 ; i < n ; ++i) {
for (int j = 0 ; j < m ; ++j) {
cin >> arr[i][j];
}
}
int max_sum = arr[0][0], temp_sum = 0;
for (int row_len = 1 ; row_len <= n ; row_len++) {
for (int col_len = 1 ; col_len <= m ; col_len++) {
for (int i = 0 ; i < n ; ++i) {
if ( i + row_len > n ) continue;
for (int j = 0 ; j < m ; ++j) {
if ( j + col_len > m ) continue;
temp_sum = 0;
for (int ii = i ; ii < i + row_len ; ++ii) {
for (int jj = j ; jj < j + col_len ; ++jj) {
temp_sum += arr[ii][jj];
cout << arr[ii][jj] << endl;
}
}
max_sum = temp_sum > max_sum ? temp_sum : max_sum;
}
}
}
}
cout << max_sum;
return 0;
}
// 2020/4/10
// 1. 循环的边界问题 开始 i + row > 0 错写成了 i + row >= 0, 导致1*1 的矩形过不了
// 2. 循环变量的索引问题, 内部循环当然是加内部循环的索引, temp_sum += arr[ii][jj] 开始错写成了 temp_sum += arr[i][j]
// 3. break和continue,这里开始错写成了break,这是不对的,否则检查到第一行超了后后面所有行都不再作为起始点进行搜索
又重新写了一遍,熟了一些
#include <iostream>
using namespace std;
int arr[50][50];
int main()
{
int n, m;
cin >> n >> m;
for (int i = 0 ; i < n ; ++i) {
for (int j = 0 ; j < m ; ++j) {
cin >> arr[i][j];
}
}
int tempsum = 0;
int maxsum = arr[0][0];
for (int i = 0 ; i < n ; ++i) {
for (int j = 0 ; j < m ; ++j) {
// the outer 2 loops decide the anchor point
// now decide the size of the window size
for (int rowlen = 0 ; ; ++rowlen) {
if (rowlen + i >= n) break;
for (int collen = 0 ; ; ++collen) {
if (collen + j >= m) break;
// now decided the window size length
// begin to calculate the sum of the matrix elements
for (int ii = i ; ii <= i + rowlen ; ++ii) {
for (int jj = j ; jj <= j + collen ; ++jj) {
tempsum += arr[ii][jj];
}
}
if (tempsum > maxsum) {
maxsum = tempsum;
}
tempsum = 0;
}
}
}
}
cout << maxsum;
return 0;
}