# BestCoder Round #63 (div.2)1003 matrix

#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m;
int a[1005][1005];
int dp[1005][1005];
int main()
{
// freopen("cin.txt","r",stdin);
while(~scanf("%d%d",&n,&m))
{
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
scanf("%d",&a[i][j]);
// if(i==0||j==0) dp[i][j]=a[i][j];
}
}
int tmp=0;
int count=0;
for(int i=1;i<=n;i++)
{

for(int j=1;j<=m;j++)
{
if(i==0&&j==0) continue;
if((i+j)%2!=0){
if(j==1) dp[i][j]=dp[i-1][j]+a[i-1][j]*a[i][j];
else if(i==1) dp[i][j]=dp[i][j-1]+a[i][j-1]*a[i][j];
else
dp[i][j]=min(dp[i][j-1]+a[i][j-1]*a[i][j],dp[i-1][j]+a[i-1][j]*a[i][j]);
}
else {
if(i==1&&j!=1) dp[i][j]=dp[i][j-1];
else if(i!=1&&j==1) dp[i][j]=dp[i-1][j];
else
dp[i][j]=min(dp[i-1][j],dp[i][j-1]);

}
}
}
/** for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
printf("%d  ",dp[i][j]);
}
printf("\n");
}**/
printf("%d\n",dp[n][m]);
}
return 0;
}


12-09 364

11-22 803

11-21 183

11-22 294

08-08 296

02-17 159

02-20 92

11-23 252

11-21 444

11-22 498