• Source
    1. #include<bits/stdc++.h>
    2.  
    3. using namespace std;
    4.  
    5. int cumulative_sum[105][105];
    6. int grid[105][105];
    7.  
    8. int main()
    9. {
    10. int row,column,i,j,k;
    11.  
    12. while(scanf("%d",&row)==1)
    13. {
    14. column=row;
    15.  
    16. for(i=1; i<=row; i++)
    17. {
    18. for(j=1; j<=column; j++)
    19. {
    20. scanf("%d",&grid[i][j]);
    21. }
    22. }
    23.  
    24. for(j=1; j<=column; j++)
    25. {
    26. for(i=1; i<=row; i++)
    27. {
    28. cumulative_sum[i][j]=grid[i][j]+cumulative_sum[i-1][j];
    29. }
    30. }
    31.  
    32. int sub,sum,max_sum=0;
    33.  
    34. for(k=0; k<row; k++)
    35. {
    36.  
    37. for(i=k+1; i<=row; i++)
    38. {
    39.  
    40. sum=0;
    41.  
    42. for(j=1; j<=column; j++)
    43. {
    44. sub = cumulative_sum[i][j]-cumulative_sum[k][j];
    45.  
    46. sum += sub;
    47.  
    48. max_sum = max(sum,max_sum);
    49.  
    50. if(sum<0)
    51. {
    52. sum=0;
    53. }
    54.  
    55. }
    56. }
    57. }
    58.  
    59. printf("%d\n",max_sum);
    60.  
    61. memset(cumulative_sum,0,sizeof(cumulative_sum));
    62.  
    63. }
    64.  
    65. return 0;
    66. }
    67.