#![allow(non_snake_case)] #![allow(unused_imports)] #![allow(unused_macros)] #![allow(clippy::needless_range_loop)] #![allow(clippy::comparison_chain)] #![allow(clippy::nonminimal_bool)] #![allow(clippy::neg_multiply)] #![allow(dead_code)] // use itertools::Itertools; // use superslice::Ext; use std::cmp::Reverse; use std::collections::{BTreeMap, BTreeSet, BinaryHeap, VecDeque}; #[derive(Default)] struct Solver {} impl Solver { fn solve(&mut self) { input! { H: usize, W: usize, A: [[usize; W]; H] } let mut ans = 0; // yoko for i in 0..H { let mut s = 0; let mut used = vec![vec![false; W]; H]; for j in 0..W { s += A[i][j]; used[i][j] = true; } // yoko for k in 0..H { let mut ss = s; for l in 0..W { if !used[k][l] { ss += A[k][l]; } } ans = max!(ans, ss); } // tate for k in 0..W { let mut ss = s; for l in 0..H { if !used[l][k] { ss += A[l][k]; } } ans = max!(ans, ss); } // naname1 for k in 0..W { let mut x = 0_usize; let mut y = k; let mut ss = s; while x < H && y < W { if !used[x][y] { ss += A[x][y]; } x = x.wrapping_add(1); y = y.wrapping_add(1); } ans = max!(ans, ss); } // naname2 for k in 0..H { let mut x = k; let mut y = 0_usize; let mut ss = s; while x < H && y < W { if !used[x][y] { ss += A[x][y]; } x = x.wrapping_add(1); y = y.wrapping_add(1); } ans = max!(ans, ss); } // naname3 for k in 0..W { let mut x = 0_usize; let mut y = k; let mut ss = s; while x < H && y < W { if !used[x][y] { ss += A[x][y]; } x = x.wrapping_add(1); y = y.wrapping_add(!0); } ans = max!(ans, ss); } // naname4 for k in 0..H { let mut x = k; let mut y = W - 1; let mut ss = s; while x < H && y < W { if !used[x][y] { ss += A[x][y]; } x = x.wrapping_add(1); y = y.wrapping_add(!0); } ans = max!(ans, ss); } } // tate for i in 0..W { let mut s = 0; let mut used = vec![vec![false; W]; H]; for j in 0..H { s += A[j][i]; used[j][i] = true; } // yoko for k in 0..H { let mut ss = s; for l in 0..W { if !used[k][l] { ss += A[k][l]; } } ans = max!(ans, ss); } // tate for k in 0..W { let mut ss = s; for l in 0..H { if !used[l][k] { ss += A[l][k]; } } ans = max!(ans, ss); } // naname1 for k in 0..W { let mut x = 0_usize; let mut y = k; let mut ss = s; while x < H && y < W { if !used[x][y] { ss += A[x][y]; } x = x.wrapping_add(1); y = y.wrapping_add(1); } ans = max!(ans, ss); } // naname2 for k in 0..H { let mut x = k; let mut y = 0_usize; let mut ss = s; while x < H && y < W { if !used[x][y] { ss += A[x][y]; } x = x.wrapping_add(1); y = y.wrapping_add(1); } ans = max!(ans, ss); } // naname3 for k in 0..W { let mut x = 0_usize; let mut y = k; let mut ss = s; while x < H && y < W { if !used[x][y] { ss += A[x][y]; } x = x.wrapping_add(1); y = y.wrapping_add(!0); } ans = max!(ans, ss); } // naname4 for k in 0..H { let mut x = k; let mut y = W - 1; let mut ss = s; while x < H && y < W { if !used[x][y] { ss += A[x][y]; } x = x.wrapping_add(1); y = y.wrapping_add(!0); } ans = max!(ans, ss); } } // naname1 for i in 0..W { let mut s = 0; let mut used = vec![vec![false; W]; H]; let mut x = 0_usize; let mut y = i; while x < H && y < W { s += A[x][y]; used[x][y] = true; x = x.wrapping_add(1); y = y.wrapping_add(1); } // yoko for k in 0..H { let mut ss = s; for l in 0..W { if !used[k][l] { ss += A[k][l]; } } ans = max!(ans, ss); } // tate for k in 0..W { let mut ss = s; for l in 0..H { if !used[l][k] { ss += A[l][k]; } } ans = max!(ans, ss); } // naname1 for k in 0..W { let mut x = 0_usize; let mut y = k; let mut ss = s; while x < H && y < W { if !used[x][y] { ss += A[x][y]; } x = x.wrapping_add(1); y = y.wrapping_add(1); } ans = max!(ans, ss); } // naname2 for k in 0..H { let mut x = k; let mut y = 0_usize; let mut ss = s; while x < H && y < W { if !used[x][y] { ss += A[x][y]; } x = x.wrapping_add(1); y = y.wrapping_add(1); } ans = max!(ans, ss); } // naname3 for k in 0..W { let mut x = 0_usize; let mut y = k; let mut ss = s; while x < H && y < W { if !used[x][y] { ss += A[x][y]; } x = x.wrapping_add(1); y = y.wrapping_add(!0); } ans = max!(ans, ss); } // naname4 for k in 0..H { let mut x = k; let mut y = W - 1; let mut ss = s; while x < H && y < W { if !used[x][y] { ss += A[x][y]; } x = x.wrapping_add(1); y = y.wrapping_add(!0); } ans = max!(ans, ss); } } // naname2 for i in 0..H { let mut used = vec![vec![false; W]; H]; let mut x = i; let mut y = 0_usize; let mut s = 0; while x < H && y < W { s += A[x][y]; used[x][y] = true; x = x.wrapping_add(1); y = y.wrapping_add(1); } // yoko for k in 0..H { let mut ss = s; for l in 0..W { if !used[k][l] { ss += A[k][l]; } } ans = max!(ans, ss); } // tate for k in 0..W { let mut ss = s; for l in 0..H { if !used[l][k] { ss += A[l][k]; } } ans = max!(ans, ss); } // naname1 for k in 0..W { let mut x = 0_usize; let mut y = k; let mut ss = s; while x < H && y < W { if !used[x][y] { ss += A[x][y]; } x = x.wrapping_add(1); y = y.wrapping_add(1); } ans = max!(ans, ss); } // naname2 for k in 0..H { let mut x = k; let mut y = 0_usize; let mut ss = s; while x < H && y < W { if !used[x][y] { ss += A[x][y]; } x = x.wrapping_add(1); y = y.wrapping_add(1); } ans = max!(ans, ss); } // naname3 for k in 0..W { let mut x = 0_usize; let mut y = k; let mut ss = s; while x < H && y < W { if !used[x][y] { ss += A[x][y]; } x = x.wrapping_add(1); y = y.wrapping_add(!0); } ans = max!(ans, ss); } // naname4 for k in 0..H { let mut x = k; let mut y = W - 1; let mut ss = s; while x < H && y < W { if !used[x][y] { ss += A[x][y]; } x = x.wrapping_add(1); y = y.wrapping_add(!0); } ans = max!(ans, ss); } } // naname3 for i in 0..W { let mut s = 0; let mut used = vec![vec![false; W]; H]; let mut x = 0_usize; let mut y = i; while x < H && y < W { s += A[x][y]; used[x][y] = true; x = x.wrapping_add(1); y = y.wrapping_add(!0); } // yoko for k in 0..H { let mut ss = s; for l in 0..W { if !used[k][l] { ss += A[k][l]; } } ans = max!(ans, ss); } // tate for k in 0..W { let mut ss = s; for l in 0..H { if !used[l][k] { ss += A[l][k]; } } ans = max!(ans, ss); } // naname1 for k in 0..W { let mut x = 0_usize; let mut y = k; let mut ss = s; while x < H && y < W { if !used[x][y] { ss += A[x][y]; } x = x.wrapping_add(1); y = y.wrapping_add(1); } ans = max!(ans, ss); } // naname2 for k in 0..H { let mut x = k; let mut y = 0_usize; let mut ss = s; while x < H && y < W { if !used[x][y] { ss += A[x][y]; } x = x.wrapping_add(1); y = y.wrapping_add(1); } ans = max!(ans, ss); } // naname3 for k in 0..W { let mut x = 0_usize; let mut y = k; let mut ss = s; while x < H && y < W { if !used[x][y] { ss += A[x][y]; } x = x.wrapping_add(1); y = y.wrapping_add(!0); } ans = max!(ans, ss); } // naname4 for k in 0..H { let mut x = k; let mut y = W - 1; let mut ss = s; while x < H && y < W { if !used[x][y] { ss += A[x][y]; } x = x.wrapping_add(1); y = y.wrapping_add(!0); } ans = max!(ans, ss); } } // naname4 for i in 0..H { let mut used = vec![vec![false; W]; H]; let mut x = i; let mut y = W - 1; let mut s = 0; while x < H && y < W { s += A[x][y]; used[x][y] = true; x = x.wrapping_add(1); y = y.wrapping_add(!0); } // yoko for k in 0..H { let mut ss = s; for l in 0..W { if !used[k][l] { ss += A[k][l]; } } ans = max!(ans, ss); } // tate for k in 0..W { let mut ss = s; for l in 0..H { if !used[l][k] { ss += A[l][k]; } } ans = max!(ans, ss); } // naname1 for k in 0..W { let mut x = 0_usize; let mut y = k; let mut ss = s; while x < H && y < W { if !used[x][y] { ss += A[x][y]; } x = x.wrapping_add(1); y = y.wrapping_add(1); } ans = max!(ans, ss); } // naname2 for k in 0..H { let mut x = k; let mut y = 0_usize; let mut ss = s; while x < H && y < W { if !used[x][y] { ss += A[x][y]; } x = x.wrapping_add(1); y = y.wrapping_add(1); } ans = max!(ans, ss); } // naname3 for k in 0..W { let mut x = 0_usize; let mut y = k; let mut ss = s; while x < H && y < W { if !used[x][y] { ss += A[x][y]; } x = x.wrapping_add(1); y = y.wrapping_add(!0); } ans = max!(ans, ss); } // naname4 for k in 0..H { let mut x = k; let mut y = W - 1; let mut ss = s; while x < H && y < W { if !used[x][y] { ss += A[x][y]; } x = x.wrapping_add(1); y = y.wrapping_add(!0); } ans = max!(ans, ss); } } println!("{}", ans); } } #[macro_export] macro_rules! max { ($x: expr) => ($x); ($x: expr, $( $y: expr ),+) => { std::cmp::max($x, max!($( $y ),+)) } } #[macro_export] macro_rules! min { ($x: expr) => ($x); ($x: expr, $( $y: expr ),+) => { std::cmp::min($x, min!($( $y ),+)) } } fn main() { std::thread::Builder::new() .stack_size(128 * 1024 * 1024) .spawn(|| Solver::default().solve()) .unwrap() .join() .unwrap(); } #[macro_export] macro_rules! input { () => {}; (mut $var:ident: $t:tt, $($rest:tt)*) => { let mut $var = __input_inner!($t); input!($($rest)*) }; ($var:ident: $t:tt, $($rest:tt)*) => { let $var = __input_inner!($t); input!($($rest)*) }; (mut $var:ident: $t:tt) => { let mut $var = __input_inner!($t); }; ($var:ident: $t:tt) => { let $var = __input_inner!($t); }; } #[macro_export] macro_rules! __input_inner { (($($t:tt),*)) => { ($(__input_inner!($t)),*) }; ([$t:tt; $n:expr]) => { (0..$n).map(|_| __input_inner!($t)).collect::>() }; ([$t:tt]) => {{ let n = __input_inner!(usize); (0..n).map(|_| __input_inner!($t)).collect::>() }}; (chars) => { __input_inner!(String).chars().collect::>() }; (bytes) => { __input_inner!(String).into_bytes() }; (usize1) => { __input_inner!(usize) - 1 }; ($t:ty) => { $crate::read::<$t>() }; } #[macro_export] macro_rules! println { () => { $crate::write(|w| { use std::io::Write; std::writeln!(w).unwrap() }) }; ($($arg:tt)*) => { $crate::write(|w| { use std::io::Write; std::writeln!(w, $($arg)*).unwrap() }) }; } #[macro_export] macro_rules! print { ($($arg:tt)*) => { $crate::write(|w| { use std::io::Write; std::write!(w, $($arg)*).unwrap() }) }; } #[macro_export] macro_rules! flush { () => { $crate::write(|w| { use std::io::Write; w.flush().unwrap() }) }; } pub fn read() -> T where T: std::str::FromStr, T::Err: std::fmt::Debug, { use std::cell::RefCell; use std::io::*; thread_local! { pub static STDIN: RefCell> = RefCell::new(stdin().lock()); } STDIN.with(|r| { let mut r = r.borrow_mut(); let mut s = vec![]; loop { let buf = r.fill_buf().unwrap(); if buf.is_empty() { break; } if let Some(i) = buf.iter().position(u8::is_ascii_whitespace) { s.extend_from_slice(&buf[..i]); r.consume(i + 1); if !s.is_empty() { break; } } else { s.extend_from_slice(buf); let n = buf.len(); r.consume(n); } } std::str::from_utf8(&s).unwrap().parse().unwrap() }) } pub fn write(f: F) where F: FnOnce(&mut std::io::BufWriter), { use std::cell::RefCell; use std::io::*; thread_local! { pub static STDOUT: RefCell>> = RefCell::new(BufWriter::new(stdout().lock())); } STDOUT.with(|w| f(&mut w.borrow_mut())) } trait Bound { fn lower_bound(&self, x: &T) -> usize; fn upper_bound(&self, x: &T) -> usize; } impl Bound for [T] { fn lower_bound(&self, x: &T) -> usize { let (mut low, mut high) = (0, self.len()); while low + 1 < high { let mid = (low + high) / 2; if self[mid] < *x { low = mid; } else { high = mid; } } if self[low] < *x { low + 1 } else { low } } fn upper_bound(&self, x: &T) -> usize { let (mut low, mut high) = (0, self.len()); while low + 1 < high { let mid = (low + high) / 2; if self[mid] <= *x { low = mid; } else { high = mid; } } if self[low] <= *x { low + 1 } else { low } } }