using System; using System.ComponentModel; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.Contracts; using System.IO; using System.Linq; using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Text; using static Functions; using static OutputLib; using ModInt = StaticModInt; static class Program { static public void Main(string[] args) { Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }); new Solver(); Console.Out.Flush(); } } public class Solver { public Solver() { Solve(); } void Solve() { int h = ri, w = ri; var a = new int[h, w]; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) a[i, j] = ri; var c = new int[h, w]; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) c[i, j] = -1; int[] dx = { 1, 0, -1, 0 }, dy = { 0, 1, 0, -1 }; bool Safe(int x, int y) => 0 <= x && x < h && 0 <= y && y < w; var pq = new MyPriorityQueue[2]; for (int k = 0; k < 2; k++) pq[k] = new MyPriorityQueue(x => -x.v); bool Update(int x, int y, int v) { bool f = false; c[x, y] = v; for (int k = 0; k < 4; k++) { int nx = x + dx[k], ny = y + dy[k]; if (!Safe(nx, ny)) continue; if (c[nx, ny] == -1) pq[v % 2].Enqueue((nx, ny, a[nx, ny])); else if (c[nx, ny] != v) f = true; } return f; } Update(0, 0, 0); Update(h - 1, w - 1, 1); for (int i = 0; ; i++) { var (x, y, _) = pq[i % 2].Dequeue(); while (c[x, y] != -1) (x, y, _) = pq[i % 2].Dequeue(); if (Update(x, y, i % 2)) { Write(i + 1); break; } } } const long INF = 1L << 60; int ri { [MethodImpl(256)] get => (int)sc.Integer(); } (int, int) ri2 { [MethodImpl(256)] get => (ri, ri); } (int, int, int) ri3 { [MethodImpl(256)] get => (ri, ri, ri); } (int, int, int, int) ri4 { [MethodImpl(256)] get => (ri, ri, ri, ri); } (int, int, int, int, int) ri5 { [MethodImpl(256)] get => (ri, ri, ri, ri, ri); } long rl { [MethodImpl(256)] get => sc.Integer(); } (long, long) rl2 { [MethodImpl(256)] get => (rl, rl); } (long, long, long) rl3 { [MethodImpl(256)] get => (rl, rl, rl); } (long, long, long, long) rl4 { [MethodImpl(256)] get => (rl, rl, rl, rl); } (long, long, long, long, long) rl5 { [MethodImpl(256)] get => (rl, rl, rl, rl, rl); } uint rui { [MethodImpl(256)] get => (uint)sc.UInteger(); } ulong rul { [MethodImpl(256)] get => sc.UInteger(); } double rd { [MethodImpl(256)] get => sc.Double(); } string rs { [MethodImpl(256)] get => sc.Scan(); } string rline { [MethodImpl(256)] get => sc.Line(); } public StreamScanner sc = new StreamScanner(Console.OpenStandardInput()); } public static class Functions { [MethodImpl(256)] public static int Popcount(ulong x) { x = (x & 0x5555555555555555UL) + ((x >> 1) & 0x5555555555555555UL); x = (x & 0x3333333333333333UL) + ((x >> 2) & 0x3333333333333333UL); x = (x & 0x0f0f0f0f0f0f0f0fUL) + ((x >> 4) & 0x0f0f0f0f0f0f0f0fUL); x = (x & 0x00ff00ff00ff00ffUL) + ((x >> 8) & 0x00ff00ff00ff00ffUL); x = (x & 0x0000ffff0000ffffUL) + ((x >> 16) & 0x0000ffff0000ffffUL); x = (x & 0x00000000ffffffffUL) + ((x >> 32) & 0x00000000ffffffffUL); return (int)x; } [MethodImpl(256)] public static int Popcount(long x) { x = (x & 0x5555555555555555L) + ((x >> 1) & 0x5555555555555555L); x = (x & 0x3333333333333333L) + ((x >> 2) & 0x3333333333333333L); x = (x & 0x0f0f0f0f0f0f0f0fL) + ((x >> 4) & 0x0f0f0f0f0f0f0f0fL); x = (x & 0x00ff00ff00ff00ffL) + ((x >> 8) & 0x00ff00ff00ff00ffL); x = (x & 0x0000ffff0000ffffL) + ((x >> 16) & 0x0000ffff0000ffffL); x = (x & 0x00000000ffffffffL) + ((x >> 32) & 0x00000000ffffffffL); return (int)x; } [MethodImpl(256)] public static int Popcount(int x) { x = (x & 0x55555555) + ((x >> 1) & 0x55555555); x = (x & 0x33333333) + ((x >> 2) & 0x33333333); x = (x & 0x0f0f0f0f) + ((x >> 4) & 0x0f0f0f0f); x = (x & 0x00ff00ff) + ((x >> 8) & 0x00ff00ff); x = (x & 0x0000ffff) + ((x >> 16) & 0x0000ffff); return x; } [MethodImpl(256)] public static int Ctz(long x) { if (x == 0) return -1; return Popcount((ulong)((x & -x) - 1)); } [MethodImpl(256)] public static int CeilPow2(int n) { int x = 0; while ((1 << x) < n) x++; return x; } [MethodImpl(256)] public static int SafeMod(int x, int m) { int r = x % m; return r < 0 ? r + Math.Abs(m) : r; } [MethodImpl(256)] public static long SafeMod(long x, long m) { long r = x % m; return r < 0 ? r + Math.Abs(m) : r; } [MethodImpl(256)] public static bool IsIncreasing(long a, long b, long c) => a <= b && b <= c; [MethodImpl(256)] public static int Sign(long x) => x == 0 ? 0 : (x < 0 ? -1 : 1); [MethodImpl(256)] public static int Sign(double x) => x == 0 ? 0 : (x < 0 ? -1 : 1); [MethodImpl(256)] public static int DigitSum(long n, int d = 10) { long s = 0; while (n > 0) { s += n % d; n /= d; } return (int)s; } [MethodImpl(256)] public static long Floor(long a, long b) => a >= 0 ? a / b : (a + 1) / b - 1; [MethodImpl(256)] public static long Ceil(long a, long b) => a > 0 ? (a - 1) / b + 1 : a / b; [MethodImpl(256)] public static int Gcd(int a, int b) => b == 0 ? a : Gcd(b, a % b); [MethodImpl(256)] public static uint Gcd(uint a, uint b) => b == 0 ? a : Gcd(b, a % b); [MethodImpl(256)] public static long Gcd(long a, long b) => b == 0 ? a : Gcd(b, a % b); [MethodImpl(256)] public static ulong Gcd(ulong a, ulong b) => b == 0 ? a : Gcd(b, a % b); [MethodImpl(256)] public static (long x, long y, long g) ExtGcd(long a, long b) { if (b == 0) return (Sign(a), 0, Math.Abs(a)); long c = SafeMod(a, b); var (x2, y2, g) = ExtGcd(b, c); long x = SafeMod(y2, b); long y = (g - a * x) / b; return (x, y, g); } [MethodImpl(256)] public static void Swap(ref int x, ref int y) { x ^= y; y ^= x; x ^= y; } [MethodImpl(256)] public static void Swap(ref long x, ref long y) { x ^= y; y ^= x; x ^= y; } [MethodImpl(256)] public static void Swap(ref T x, ref T y) { T t = y; y = x; x = t; } [MethodImpl(256)] public static T Clamp(T x, T l, T r) where T : IComparable => x.CompareTo(l) <= 0 ? l : (x.CompareTo(r) <= 0 ? x : r); [MethodImpl(256)] public static T Clamp(ref T x, T l, T r) where T : IComparable => x = x.CompareTo(l) <= 0 ? l : (x.CompareTo(r) <= 0 ? x : r); [MethodImpl(256)] public static void Chmin(ref T x, T y) where T : IComparable { if (x.CompareTo(y) > 0) x = y; } [MethodImpl(256)] public static void Chmax(ref T x, T y) where T : IComparable { if (x.CompareTo(y) < 0) x = y; } [MethodImpl(256)] public static int LowerBound(long[] arr, long val, int l = -1, int r = -1) => LowerBound(arr.AsSpan(), t => Sign(t - val), l, r); [MethodImpl(256)] public static int LowerBound(int[] arr, int val, int l = -1, int r = -1) => LowerBound(arr.AsSpan(), t => t - val, l, r); [MethodImpl(256)] public static int LowerBound(T[] arr, T val, int l = -1, int r = -1) where T : IComparable => LowerBound(arr.AsSpan(), t => t.CompareTo(val), l, r); [MethodImpl(256)] public static int LowerBound(T[] arr, Func comp, int l = -1, int r = -1) => LowerBound(arr.AsSpan(), comp, l, r); [MethodImpl(256)] public static int LowerBound(Span data, Func comp, int l = -1, int r = -1) { if (data.Length == 0) return -1; if (l == -1) l = 0; if (r == -1) r = data.Length; while (l < r) { int x = (l + r) / 2; if (comp(data[x]) < 0) l = x + 1; else r = x; } return l; } [MethodImpl(256)] public static int RangeCount(T[] arr, T geq, T lt, int l = -1, int r = -1) where T : IComparable => Math.Max(0, LowerBound(arr.AsSpan(), t => t.CompareTo(lt), l, r) - LowerBound(arr.AsSpan(), t => t.CompareTo(geq), l, r)); [MethodImpl(256)] public static string ToBase2(long v, int digit = -1) { if (digit == -1) { digit = 0; while ((v >> digit) > 0) digit++; } var c = new string[digit]; for (int i = 0; i < digit; i++) c[digit - 1 - i] = ((v >> i) & 1) == 0 ? "0" : "1"; return string.Join("", c); } [MethodImpl(256)] public static string ToBaseN(long v, int n, int digit = -1) { if (digit == -1) { digit = 0; long pow = 1; while (v >= pow) { digit++; pow *= n; } } var c = new int[digit]; for (int i = 0; i < digit; i++, v /= n) c[digit - 1 - i] = (int)(v % n); return string.Join("", c); } } public partial class StreamScanner { public StreamScanner(Stream stream) { str = stream; } private readonly Stream str; private readonly byte[] buf = new byte[1024]; private int len, ptr; public bool isEof = false; public bool IsEndOfStream { get { return isEof; } } [MethodImpl(256)] private byte read() { if (isEof) throw new EndOfStreamException(); if (ptr >= len) { ptr = 0; if ((len = str.Read(buf, 0, 1024)) <= 0) { isEof = true; return 0; } } return buf[ptr++]; } [MethodImpl(256)] public char Char() { byte b = 0; do b = read(); while (b < 33 || 126 < b); return (char)b; } [MethodImpl(256)] public string Line() { var sb = new StringBuilder(); for (var b = Char(); b != 10 && !isEof; b = (char)read()) sb.Append(b); return sb.ToString(); } [MethodImpl(256)] public string Scan() { var sb = new StringBuilder(); for (var b = Char(); b >= 33 && b <= 126; b = (char)read()) sb.Append(b); return sb.ToString(); } [MethodImpl(256)] public long Integer() { long ret = 0; byte b = 0; var ng = false; do b = read(); while (b != '-' && (b < '0' || '9' < b)); if (b == '-') { ng = true; b = read(); } for (; true; b = read()) { if (b < '0' || '9' < b) return ng ? -ret : ret; else ret = ret * 10 + b - '0'; } } [MethodImpl(256)] public ulong UInteger() { ulong ret = 0; byte b = 0; do b = read(); while (b < '0' || '9' < b); for (; true; b = read()) { if (b < '0' || '9' < b) return ret; else ret = ret * 10 + b - '0'; } } [MethodImpl(256)] public double Double() => double.Parse(Scan()); } public static class OutputLib { [MethodImpl(256)] public static void WriteJoin(string s, IEnumerable t) => Console.WriteLine(string.Join(s, t)); [MethodImpl(256)] public static void WriteMat(T[,] a, string sep = " ") { int sz1 = a.GetLength(0), sz2 = a.GetLength(1); var b = new T[sz2]; for (int i = 0; i < sz1; i++) { for (int j = 0; j < sz2; j++) b[j] = a[i, j]; WriteJoin(sep, b); } } [MethodImpl(256)] public static void WriteMat(T[][] a, string sep = " ") { for (int i = 0; i < a.Length; i++) WriteJoin(sep, a[i]); } [MethodImpl(256)] public static void WriteMat(T[][] a, Func map, string sep = " ") { for (int i = 0; i < a.Length; i++) WriteJoin(sep, a[i].Select(x => map(x))); } [MethodImpl(256)] public static void Write(object t) => Console.WriteLine(t.ToString()); [MethodImpl(256)] public static void Write(params object[] arg) => Console.WriteLine(string.Join(" ", arg.Select(x => x.ToString()))); [MethodImpl(256)] public static void Write(string str) => Console.WriteLine(str); [MethodImpl(256)] public static void WriteFlush(object t) { Console.WriteLine(t.ToString()); Console.Out.Flush(); } [MethodImpl(256)] public static void WriteError(object t) => Console.Error.WriteLine(t.ToString()); [MethodImpl(256)] public static void Flush() => Console.Out.Flush(); [MethodImpl(256)] public static void YN(bool t) => Console.WriteLine(t ? "YES" : "NO"); [MethodImpl(256)] public static void Yn(bool t) => Console.WriteLine(t ? "Yes" : "No"); [MethodImpl(256)] public static void yn(bool t) => Console.WriteLine(t ? "yes" : "no"); [MethodImpl(256)] public static void DeleteLine() => Console.Write("\x1b[1A\x1b[2K"); [MethodImpl(256)] public static void ProgressBar(long now, long total, int blocks = 50) { int x = (int)((2 * now * blocks + 1) / (2 * total)); Console.Write($"\x1b[G[\x1b[42m{string.Concat(Enumerable.Repeat("_", x))}\x1b[0m{string.Concat(Enumerable.Repeat("_", blocks - x))}] : {now} / {total}"); } } public interface IStaticMod { uint Mod { get; } bool IsPrime { get; } } public readonly struct Mod1000000007 : IStaticMod { public uint Mod => 1000000007; public bool IsPrime => true; } public readonly struct Mod998244353 : IStaticMod { public uint Mod => 998244353; public bool IsPrime => true; } public readonly struct StaticModInt : IEquatable> where T : struct, IStaticMod { internal readonly uint _v; private static readonly T op = default; public int Value => (int)_v; public static int Mod => (int)op.Mod; public static StaticModInt Zero => default; public static StaticModInt One => new StaticModInt(1u); [MethodImpl(256)] public static StaticModInt Raw(int v) { var u = unchecked((uint)v); return new StaticModInt(u); } [MethodImpl(256)] public StaticModInt(long v) : this(Round(v)) { } [MethodImpl(256)] public StaticModInt(ulong v) : this((uint)(v % op.Mod)) { } [MethodImpl(256)] private StaticModInt(uint v) => _v = v; [MethodImpl(256)] private static uint Round(long v) { var x = v % op.Mod; if (x < 0) x += op.Mod; return (uint)x; } [MethodImpl(256)] public static StaticModInt operator ++(StaticModInt v) { var x = v._v + 1; if (x == op.Mod) x = 0; return new StaticModInt(x); } [MethodImpl(256)] public static StaticModInt operator --(StaticModInt v) { var x = v._v; if (x == 0) x = op.Mod; return new StaticModInt(x - 1); } [MethodImpl(256)] public static StaticModInt operator +(StaticModInt lhs, StaticModInt rhs) { var v = lhs._v + rhs._v; if (v >= op.Mod) v -= op.Mod; return new StaticModInt(v); } [MethodImpl(256)] public static StaticModInt operator -(StaticModInt lhs, StaticModInt rhs) { unchecked { var v = lhs._v - rhs._v; if (v >= op.Mod) v += op.Mod; return new StaticModInt(v); } } [MethodImpl(256)] public static StaticModInt operator *(StaticModInt lhs, StaticModInt rhs) => new StaticModInt((uint)((ulong)lhs._v * rhs._v % op.Mod)); [MethodImpl(256)] public static StaticModInt operator /(StaticModInt lhs, StaticModInt rhs) => new StaticModInt((uint)((ulong)lhs._v * Inv(rhs._v) % op.Mod)); [MethodImpl(256)] public static StaticModInt operator +(StaticModInt v) => v; [MethodImpl(256)] public static StaticModInt operator -(StaticModInt v) => new StaticModInt(v._v == 0 ? 0 : op.Mod - v._v); [MethodImpl(256)] public static bool operator ==(StaticModInt lhs, StaticModInt rhs) => lhs._v == rhs._v; [MethodImpl(256)] public static bool operator !=(StaticModInt lhs, StaticModInt rhs) => lhs._v != rhs._v; [MethodImpl(256)] public static implicit operator StaticModInt(int v) => new StaticModInt(v); [MethodImpl(256)] public static implicit operator StaticModInt(uint v) => new StaticModInt((long)v); [MethodImpl(256)] public static implicit operator StaticModInt(long v) => new StaticModInt(v); [MethodImpl(256)] public static implicit operator StaticModInt(ulong v) => new StaticModInt(v); [MethodImpl(256)] public static implicit operator long(StaticModInt v) => v._v; [MethodImpl(256)] public static implicit operator ulong(StaticModInt v) => v._v; [MethodImpl(256)] public StaticModInt Pow(long n) { var x = this; var r = new StaticModInt(1U); if (n < 0) (x, n) = (x.Inv(), -n); while (n > 0) { if ((n & 1) > 0) r *= x; x *= x; n >>= 1; } return r; } [MethodImpl(256)] public StaticModInt Inv() => new StaticModInt(Inv(_v)); [MethodImpl(256)] static ulong Inv(ulong x) { long u = op.Mod, xu = 1, yu = 0, v = (long)x, xv = 0, yv = 1; while (v != 0) { long w = SafeMod(u, v); long q = (u - w) / v; long xw = xu - xv * q; long yw = yu - yv * q; u = v; xu = xv; yu = yv; v = w; xv = xw; yv = yw; } return (ulong)(yu < 0 ? yu + op.Mod : yu); } [MethodImpl(256)] static long SafeMod(long x, long m) { long r = x % m; if (r < 0) r += m; return r; } [MethodImpl(256)] public override string ToString() => _v.ToString(); [MethodImpl(256)] public string ToString(string format, IFormatProvider formatProvider) => _v.ToString(format, formatProvider); [MethodImpl(256)] public override bool Equals(object? obj) => obj is StaticModInt m && Equals(m); [MethodImpl(256)] public bool Equals(StaticModInt other) => _v == other._v; [MethodImpl(256)] public override int GetHashCode() => _v.GetHashCode(); } public class MyPriorityQueue : IEnumerable { public int Count { get; private set; } = 0; private readonly List> _data; private readonly Func _keySelector; private readonly IComparer _keyComparer; public MyPriorityQueue(Func keySelector) : this(keySelector, Comparer.Default) { } public MyPriorityQueue(Func keySelector, IComparer keyComparer) { _keySelector = keySelector; _keyComparer = keyComparer; _data = new List>(); } public void Clear() { Count = 0; _data.Clear(); } public void Enqueue(TValue item) { if (Count == _data.Count) _data.Add(new KeyValuePair(_keySelector(item), item)); else _data[Count] = new KeyValuePair(_keySelector(item), item); var childIndex = Count++; while (childIndex > 0) { var parentIndex = (childIndex - 1) / 2; if (Compare(_data[childIndex].Key, _data[parentIndex].Key) >= 0) break; Swap(childIndex, parentIndex); childIndex = parentIndex; } } public TValue Dequeue() { var lastIndex = --Count; var firstItem = _data[0]; _data[0] = _data[lastIndex]; lastIndex--; var parentIndex = 0; while (true) { var childIndex = parentIndex * 2 + 1; if (childIndex > lastIndex) break; var rightChild = childIndex + 1; if (rightChild <= lastIndex && Compare(_data[rightChild].Key, _data[childIndex].Key) < 0) childIndex = rightChild; if (Compare(_data[parentIndex].Key, _data[childIndex].Key) <= 0) break; Swap(parentIndex, childIndex); parentIndex = childIndex; } return firstItem.Value; } public TValue Peek() => _data[0].Value; private void Swap(int a, int b) { var tmp = _data[a]; _data[a] = _data[b]; _data[b] = tmp; } private int Compare(TKey a, TKey b) => _keyComparer.Compare(b, a); public IEnumerator GetEnumerator() => _data.Select(r => r.Value).GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); }