#include #include #include namespace nono { template class Matrix { public: Matrix() = default; Matrix(int row, int column): row_(row), column_(column), data_(row, std::vector(column)) {} Matrix(int row, int column, T elem): row_(row), column_(column), data_(row, std::vector(column, elem)) {} explicit Matrix(const std::vector>& data) : row_(data.size()), column_(data.front().size()), data_(data) { for (int i = 0; i < row_; i++) {} } Matrix(std::initializer_list> init): data_(init.begin(), init.end()) { row_ = data_.size(); column_ = data_.front().size(); for (int i = 0; i < row_; i++) {} } const std::vector& operator[](const int pos) const { return data_[pos]; } std::vector& operator[](const int pos) { return data_[pos]; } Matrix& operator+=(const T rhs) { for (int i = 0; i < row_; i++) { for (int j = 0; j < column_; j++) { data_[i][j] += rhs; } } return *this; } Matrix& operator-=(const T rhs) { for (int i = 0; i < row_; i++) { for (int j = 0; j < column_; j++) { data_[i][j] -= rhs; } } return *this; } Matrix& operator*=(const T rhs) { for (int i = 0; i < row_; i++) { for (int j = 0; j < column_; j++) { data_[i][j] *= rhs; } } return *this; } Matrix& operator/=(const T rhs) { for (int i = 0; i < row_; i++) { for (int j = 0; j < column_; j++) { data_[i][j] /= rhs; } } return *this; } friend Matrix operator+(const Matrix& lhs, const T rhs) { return Matrix(lhs) += rhs; } friend Matrix operator+(const T lhs, const Matrix& rhs) { return Matrix(rhs) += lhs; } friend Matrix operator-(const Matrix& lhs, const T rhs) { return Matrix(lhs) -= rhs; } friend Matrix operator-(const T lhs, const Matrix& rhs) { return Matrix(rhs) -= lhs; } friend Matrix operator*(const Matrix& lhs, const T rhs) { return Matrix(lhs) *= rhs; } friend Matrix operator*(const T lhs, const Matrix& rhs) { return Matrix(rhs) *= lhs; } friend Matrix operator/(const Matrix& lhs, const T rhs) { return Matrix(lhs) /= rhs; } friend Matrix operator/(const T lhs, const Matrix& rhs) { return Matrix(rhs) /= lhs; } Matrix& operator+=(const Matrix& rhs) { for (int i = 0; i < row_; i++) { for (int j = 0; j < column_; j++) { data_[i][j] += rhs.data_[i][j]; } } return *this; } Matrix& operator-=(const Matrix& rhs) { for (int i = 0; i < row_; i++) { for (int j = 0; j < column_; j++) { data_[i][j] -= rhs.data_[i][j]; } } return *this; } Matrix& operator*=(const Matrix& rhs) { std::vector> mat(row_, std::vector(rhs.column_)); for (int i = 0; i < row_; i++) { for (int k = 0; k < column_; k++) { for (int j = 0; j < rhs.column_; j++) { mat[i][j] += data_[i][k] * rhs.data_[k][j]; } } } data_ = std::move(mat); column_ = rhs.column_; return *this; } friend Matrix operator+(const Matrix& lhs, const Matrix& rhs) { return Matrix(lhs) += rhs; } friend Matrix operator-(const Matrix& lhs, const Matrix& rhs) { return Matrix(lhs) -= rhs; } friend Matrix operator*(const Matrix& lhs, const Matrix& rhs) { return Matrix(lhs) *= rhs; } [[nodiscard]] Matrix pow(long long exp) const { Matrix result(row_, column_); Matrix base(*this); for (int i = 0; i < row_; i++) { result[i][i] = static_cast(1); } while (exp > 0) { if (exp & 1) { result *= base; } base *= base; exp >>= 1; } return result; } [[nodiscard]] Matrix rotate() { std::vector> result(column_, std::vector(row_)); for (int i = 0; i < row_; i++) { for (int j = 0; j < column_; j++) { result[j][row_ - i - 1] = data_[i][j]; } } return Matrix(result); } [[nodiscard]] Matrix transpose() { std::vector> result(column_, std::vector(row_)); for (int i = 0; i < row_; i++) { for (int j = 0; j < column_; j++) { result[j][i] = data_[i][j]; } } return Matrix(result); } int row() { return row_; } int column() { return column_; } private: int row_, column_; std::vector> data_; }; } // namespace nono namespace nono { struct Init {}; void solve([[maybe_unused]] const Init& init) { int h, w; std::cin >> h >> w; Matrix grid(h, w); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { std::cin >> grid[i][j]; } } int ans = 0; for (int a = 0; a < 2; a++) { for (int b = 0; b < 4; b++) { int r = grid.row(); int c = grid.column(); std::vector row(r); std::vector naname(r + c - 1); for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { row[i] += grid[i][j]; naname[i + j] += grid[i][j]; } } for (int i = 0; i < r; i++) { for (int k = 0; k + 1 < r + c; k++) { int j = k - i; if (0 <= j && j < c) { ans = std::max(ans, row[i] + naname[i + j] - grid[i][j]); } else { ans = std::max(ans, row[i] + naname[k]); } } } grid = grid.rotate(); } auto temp = grid; int r = grid.row(); int c = grid.column(); for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { grid[i][j] = temp[i][c - j - 1]; } } } std::vector row(h); std::vector column(w); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { row[i] += grid[i][j]; column[j] += grid[i][j]; } } for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { ans = std::max(ans, row[i] + column[j] - grid[i][j]); } } std::ranges::sort(row, std::greater()); std::ranges::sort(column, std::greater()); if (h > 1) { ans = std::max(ans, row[0] + row[1]); } else { ans = std::max(ans, row[0]); } if (w > 1) { ans = std::max(ans, column[0] + column[1]); } else { ans = std::max(ans, column[0]); } std::cout << ans << std::endl; } } // namespace nono int main() { std::cin.tie(0)->sync_with_stdio(0); std::cout << std::fixed << std::setprecision(16); int t = 1; nono::Init init; while (t--) nono::solve(init); }