#include using namespace std; #ifdef LOCAL #include "debug.h" #else #define DEBUG(...) #endif int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n, m; cin >> n >> m; vector a(n, vector(m, 0LL)); vector c(m, 0LL); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> a[i][j]; c[j] += a[i][j]; } } vector b(n, 0LL); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { b[i] += a[i][j] * c[j]; } } vector ord(n); iota(begin(ord), end(ord), 0); sort(begin(ord), end(ord), [&](int i, int j) { return b[i] > b[j]; }); vector x(m, 0LL), y(m, 0LL); for (int t = 0; t < n; ++t) { if (t % 2 == 0) { for (int j = 0; j < m; ++j) { x[j] += a[ord[t]][j]; } } else { for (int j = 0; j < m; ++j) { y[j] += a[ord[t]][j]; } } } auto res = accumulate(begin(x), end(x), 0LL, [](auto s, auto e) { return s += e * e; }); res -= accumulate(begin(y), end(y), 0LL, [](auto s, auto e) { return s += e * e; }); cout << res << '\n'; }