#![allow(non_snake_case, unused_imports, unused_must_use)] use std::io::{self, prelude::*}; use std::str; fn main() { let (stdin, stdout) = (io::stdin(), io::stdout()); let mut scan = Scanner::new(stdin.lock()); let mut out = io::BufWriter::new(stdout.lock()); macro_rules! input { ($T: ty) => { scan.token::<$T>() }; ($T: ty, $N: expr) => { (0..$N).map(|_| scan.token::<$T>()).collect::>() }; } let H = input!(usize); let W = input!(usize); let A = { let mut a = vec![]; for _ in 0..H { let b = input!(usize, W); a.push(b); } a }; let mut pos_inv = std::collections::BTreeMap::new(); for i in 0..H { for j in 0..W { pos_inv.insert(A[i][j], (i, j)); } } let mut around_1 = std::collections::BTreeSet::new(); let mut around_0 = std::collections::BTreeSet::new(); let mut seen = vec![vec![false; W]; H]; let mut seen1 = vec![vec![false; W]; H]; let mut seen0 = vec![vec![false; W]; H]; macro_rules! act1 { ($y: expr, $x: expr) => { if $x > 0 && !seen[$y][$x - 1] { around_1.insert(A[$y][$x - 1]); } if $y > 0 && !seen[$y - 1][$x] { around_1.insert(A[$y - 1][$x]); } if $x + 1 < W && !seen[$y][$x + 1] { around_1.insert(A[$y][$x + 1]); } if $y + 1 < H && !seen[$y + 1][$x] { around_1.insert(A[$y + 1][$x]); } }; } macro_rules! act0 { ($y: expr, $x: expr) => { if $x > 0 && !seen[$y][$x - 1] { around_0.insert(A[$y][$x - 1]); } if $y > 0 && !seen[$y - 1][$x] { around_0.insert(A[$y - 1][$x]); } if $x + 1 < W && !seen[$y][$x + 1] { around_0.insert(A[$y][$x + 1]); } if $y + 1 < H && !seen[$y + 1][$x] { around_0.insert(A[$y + 1][$x]); } }; } // (0, 0) seen[0][0] = true; seen1[0][0] = true; act1!(0, 0); // (H - 1, W - 1) seen[H - 1][W - 1] = true; seen0[H - 1][W - 1] = true; act0!(H - 1, W - 1); let mut ans = 0; loop { if ans % 2 == 0 { let m = around_1.range(..).next(); match m { Some(&v) => { ans += 1; around_1.remove(&v); let &(y, x) = pos_inv.get(&v).unwrap(); seen[y][x] = true; seen1[y][x] = true; act1!(y, x); if around_0.contains(&v) { around_0.remove(&v); } { if x > 0 && seen0[y][x - 1] { break; } if y > 0 && seen0[y - 1][x] { break; } if x + 1 < W && seen0[y][x + 1] { break; } if y + 1 < H && seen0[y + 1][x] { break; } } } None => { break; } } } else { let m = around_0.range(..).next(); match m { Some(&v) => { ans += 1; around_0.remove(&v); let &(y, x) = pos_inv.get(&v).unwrap(); seen[y][x] = true; seen0[y][x] = true; act0!(y, x); if around_1.contains(&v) { around_1.remove(&v); } { if x > 0 && seen1[y][x - 1] { break; } if y > 0 && seen1[y - 1][x] { break; } if x + 1 < W && seen1[y][x + 1] { break; } if y + 1 < H && seen1[y + 1][x] { break; } } } None => { break; } } } } writeln!(out, "{}", ans); } struct Scanner { reader: R, buf_str: Vec, buf_iter: str::SplitWhitespace<'static>, } impl Scanner { fn new(reader: R) -> Self { Self { reader, buf_str: vec![], buf_iter: "".split_whitespace(), } } fn token(&mut self) -> T { loop { if let Some(token) = self.buf_iter.next() { return token.parse().ok().expect("Failed parse"); } self.buf_str.clear(); self.reader .read_until(b'\n', &mut self.buf_str) .expect("Failed read"); self.buf_iter = unsafe { let slice = str::from_utf8_unchecked(&self.buf_str); std::mem::transmute(slice.split_whitespace()) } } } }