#include #include #include #include using namespace std; int a[1005][1005]; int n,m; int dp[1005][1005]; void input() { 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][2] = a[1][1] * a[1][2]; dp[2][1] = a[1][1] * a[2][1]; for(int i = 1 ; i <= n ; i ++) { for(int j = 1 ; j <= m ;j ++) { if(i + j <= 3) continue; if((i+j)%2==0) { dp[i][j] = min(dp[i-1][j],dp[i][j-1]); } else { if(i == 1) { dp[i][j] = dp[i][j-1] + a[i][j-1]*a[i][j]; } else if(j == 1) { dp[i][j] = dp[i-1][j] + a[i-1][j]*a[i][j]; } else { dp[i][j] = min(dp[i-1][j]+a[i-1][j]*a[i][j],dp[i][j-1]+a[i][j-1]*a[i][j]); } } } } printf("%d\n",dp[n][m]); } void solve() { } int main(void) { //freopen("a.in","r",stdin); while(~scanf("%d %d",&n,&m)) { input(); solve(); } }