#line 2 "cp-library/src/cp-template.hpp" #include using namespace std; using ll = long long; using ld = long double; using uint = unsigned int; using ull = unsigned long long; using i128 = __int128_t; template < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; } template < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; } #line 2 "cp-library/src/utility/rep_itr.hpp" template < class T > struct itr { T i, d; constexpr itr(const T i) noexcept : i(i), d(1) {} constexpr itr(const T i, const T d) noexcept : i(i), d(d) {} void operator++() noexcept { i += d; } constexpr int operator*() const noexcept { return i; } constexpr bool operator!=(const itr x) const noexcept { return d > 0 ? i < x.i : i > x.i; } }; template < class T > struct rep { const itr< T > s, t; constexpr rep(const T t) noexcept : s(0), t(t) {} constexpr rep(const T s, const T t) noexcept : s(s), t(t) {} constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {} constexpr auto begin() const noexcept { return s; } constexpr auto end() const noexcept { return t; } }; template < class T > struct revrep { const itr < T > s, t; constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {} constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {} constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {} constexpr auto begin() const noexcept { return s; } constexpr auto end() const noexcept { return t; } }; #line 2 "cp-library/src/utility/io.hpp" namespace scanner { struct sca { template < class T > operator T() { T s; cin >> s; return s; } }; struct vec { int n; vec(int n) : n(n) {} template < class T > operator vector< T >() { vector< T > v(n); for(T& x : v) cin >> x; return v; } }; struct mat { int h,w; mat(int h, int w) : h(h), w(w) {} template < class T > operator vector< vector< T > >() { vector m(h, vector< T >(w)); for(vector< T >& v : m) for(T& x : v) cin >> x; return m; } }; struct speedup { speedup() { cin.tie(0); ios::sync_with_stdio(0); } } su; } scanner::sca in() { return scanner::sca(); } scanner::vec in(int n) { return scanner::vec(n); } scanner::mat in(int h, int w) { return scanner::mat(h, w); } namespace printer { void precision(int d) { cout << fixed << setprecision(d); } void flush() { cout.flush(); } } int print() { cout << '\n'; return 0; } template < class head, class... tail > int print(head&& h, tail&&... t) { cout << h; if(sizeof...(tail)) cout << ' '; return print(forward(t)...); } template < class T > int print(vector< T > a, char sep = ' ') { int n = a.size(); for(int i : rep(n)) cout << a[i] << (i != n - 1 ? sep : '\n'); return 0; } template < class T > int print(vector< vector< T > > a) { if(a.empty()) return 0; int h = a.size(), w = a[0].size(); for(int i : rep(h)) for(int j : rep(w)) cout << a[i][j] << (j != w - 1 ? ' ' : '\n'); return 0; } #line 2 "cp-library/src/utility/key_val.hpp" template < class K, class V > struct key_val { K key; V val; key_val() {} key_val(K key, V val) : key(key), val(val) {} }; #line 2 "cp-library/src/utility/vec_op.hpp" template < class T > key_val< int, T > max_of(const vector< T >& a) { int i = max_element(a.begin(), a.end()) - a.begin(); return {i, a[i]}; } template < class T > key_val< int, T > min_of(const vector< T >& a) { int i = min_element(a.begin(), a.end()) - a.begin(); return {i, a[i]}; } template < class T > T sum_of(const vector< T >& a) { T sum = 0; for(const T x : a) sum += x; return sum; } template < class T > vector freq_of(const vector< T >& a, T L, T R) { vector res(R - L); for(const T x : a) res[x - L]++; return res; } template < class T > vector freq_of(const vector< T >& a, T R) { return freq_of(a, T(0), R); } template < class T > struct prefix_sum { vector< T > s; prefix_sum(const vector< T >& a) : s(a) { s.insert(s.begin(), T(0)); for(int i : rep(a.size())) s[i + 1] += s[i]; } // [L, R) T sum(int L, int R) { return s[R] - s[L]; } }; #line 2 "B.cpp" int main() { int H = in(), W = in(); vector> A = in(H, W); vector sy(H, 0), sx(W, 0); vector p(H + W - 1), m(H + W - 1); for(int x : rep(H)) for(int y : rep(W)) { sy[x] += A[x][y]; sx[y] += A[x][y]; p[y - x + H - 1] += A[x][y]; m[x + y] += A[x][y]; } int ans = 0; for(int i : rep(H)) for(int j : rep(H)) if(i != j) chmax(ans, sy[i] + sy[j]); for(int i : rep(W)) for(int j : rep(W)) if(i != j) chmax(ans, sx[i] + sx[j]); for(int i : rep(H + W - 1)) for(int j : rep(H + W - 1)) if(i != j) chmax(ans, p[i] + p[j]); for(int i : rep(H + W - 1)) for(int j : rep(H + W - 1)) if(i != j) chmax(ans, m[i] + m[j]); auto f = [&](int x, int y) { if(0 <= x and x < H and 0 <= y and y < W) return A[x][y]; return 0; }; for(int x : rep(H)) for(int y : rep(W)) chmax(ans, sy[x] + sx[y] - A[x][y]); for(int x : rep(H)) for(int c : rep(H + W - 1)) { int y = x + c - H + 1; chmax(ans, sy[x] + p[c] - f(x, y)); y = c - x; chmax(ans, sy[x] + m[c] - f(x, y)); } for(int y : rep(W)) for(int c : rep(H + W - 1)) { int x = y - c + H - 1; chmax(ans, sx[y] + p[c] - f(x, y)); x = c - y; chmax(ans, sx[y] + m[c] - f(x, y)); } for(int c0 : rep(H + W - 1)) for(int c1 : rep(H + W - 1)) { int y = (c0 + c1 - H + 1) / 2; int x = c1 - y; if((c0 + c1 - H + 1) % 2 == 0) { chmax(ans, p[c0] + m[c1] - f(x, y)); } else { chmax(ans, p[c0] + m[c1]); } } print(ans); }