#include #include using namespace std; int main() { int N, M; cin >> N >> M; int smallestAtWall = 10000; int s1 = 10000; int s2 = 10000; int s3 = 10000; long long sum = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { int temp; cin >> temp; sum += temp; if (i == 0 || j == 0 || (j == M-1) || (i == N-1)) { if (!(i == 0 && j == 0) && !(i == N-1 && j == M-1)) { 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; } } } } if (M%2 || N%2) { cout << sum << endl; } else { if (s1 + s2 < smallestAtWall) { cout << (sum - s1 - s2) << endl; } else { cout << (sum - smallestAtWall) << endl; } } }