#include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef pair pii; typedef long long ull; typedef long long ll; typedef vector vi; #define xx first #define yy second #define max min const int mod = int(1e9) + 7, INF = 0x3fffffff, maxn = 1e3 + 41; int dp[maxn][maxn], v[maxn][maxn], n, m; int dfs(int x, int y) { int temp = INF; if (x > n || y > m) return 0; if (dp[x][y]) return dp[x][y]; if (x + 1 <= n) { int z = dfs(x + 2, y); int zz = dfs(x + 1, y + 1); int tt; if (!z && !zz) tt = 0; else if (z && !zz) tt = z; else if (!z && zz) tt = zz; else tt = min(z, zz); temp = v[x][y] * v[x + 1][y] + tt; } if (y + 1 <= m) { int z = dfs(x + 1, y + 1); int zz = dfs(x, y + 2); int tt; if (!z && !zz) tt = 0; else if (z && !zz) tt = z; else if (!z && zz) tt = zz; else tt = min(z, zz); temp = min(temp, v[x][y] * v[x][y + 1] + tt); } return dp[x][y] = temp; } int main(void) { while (~scanf("%d%d", &n, &m)) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { scanf("%d", &v[i][j]); dp[i][j] = 0; } } printf("%d\n", dfs(1, 1)); /*for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cout << dp[i][j] << " "; } cout << endl; }*/ } return 0; }