#include #include using namespace std; int main() { int N, M; cin >> N >> M; int smallestAtWall = 10000; int minSum = 10000; vector> map(N, vector(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); } } } 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 (minSum < smallestAtWall) { cout << (sum - minSum) << endl; } else { cout << (sum - smallestAtWall) << endl; } } }