注意初值和边界处理即可
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
int dp[27][1007];
int a[27][1007];
int main ( )
{
int t,n,m;
scanf ( "%d" , &t );
while ( t-- )
{
scanf ( "%d%d" , &n , &m );
for ( int i = 1 ; i <= n ; i++ )
for ( int j = 1 ; j <= m ; j++ )
scanf ( "%d" , &a[i][j] );
memset ( dp , -0x3f , sizeof ( dp ) );
dp[1][1] = a[1][1];
for ( int i = 1 ; i <= n ; i++ )
for ( int j = 1 ; j <= m ; j++ )
{
if ( i != 1 )
dp[i][j] = max ( dp[i-1][j] + a[i][j] , dp[i][j] );
if ( j != 1 )
dp[i][j] = max ( dp[i][j-1] + a[i][j] , dp[i][j] );
int k = 2;
while ( k*j <= m )
{
dp[i][k*j] = max ( dp[i][j] + a[i][k*j] , dp[i][k*j] );
k++;
}
}
printf ( "%d\n" , dp[n][m] );
}
}