#include <iostream> #include <vector> using namespace std; int main() { int N, M; cin >> N >> M; int smallestAtWall = 10000; int minSum = 10000; int s1 = 10000; int s2 = 10000; int s3 = 10000; vector<vector<int>> map(N, vector<int>(M)); long long sum = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { int temp; cin >> temp; map[i][j] = temp; sum += temp; if (i == 0 || j == 0 || i == N-1 || j == M-1) { if ((i == 0 && j == 0) || (i == N-1 && j == M-1)) continue; smallestAtWall = min(smallestAtWall, temp); } else { if (temp < s1) { s3 = s2; s2 = s1; s1 = temp; } else if (temp < s2) { s3 = s2; s2 = temp; } else if (temp < s3) { s3 = temp; } } } } for (int i = 2; i < N; i+=2) { for (int j = 2; j < M; j+=2) { if (map[i][j] + map[i][j-1] < minSum) minSum = map[i][j] + map[i][j-1]; if (map[i][j] + map[i-1][j] < minSum) minSum = map[i][j] + map[i-1][j]; } } if (M%2 || N%2) { cout << sum << endl; } else { if (s1 + s2 + s3 < minSum && s1 + s2 + s3 < smallestAtWall) { cout << (sum - s1 - s2 - s3) << endl; } else if (minSum < smallestAtWall) { cout << (sum - minSum) << endl; } else { cout << (sum - smallestAtWall) << endl; } } }