macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    // 3
    // 1 2
    // 1 3
    // 1 4
    // のようなタイプ
    ($next:expr, [ $t:tt ]) => {{
        let len = read_value!($next, usize);
        (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    }};

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

fn main() {
    input! {
        h: usize,
        w: usize,
        a:[[i32;w];h],
        b:[[i32;w];h],
    };

    let k_num = (80 + 79) * 40;
    let mut dp = vec![vec![vec![false; k_num + 1]; h]; w];

    for l in 0..(w + h) {
        let mut j = if l < w { 0 } else { l - w + 1 };
        while j < h {
            if l >= j {
                let i = l - j;
                let abs = (a[j][i] - b[j][i]).abs();
                if i >= 1 {
                    for k in 0..(k_num + 1) {
                        // i-1,jから生成
                        if dp[i - 1][j][k] {
                            let plus = k as i32 + abs;
                            if plus <= k_num as i32 {
                                dp[i][j][plus as usize] = true;
                            }

                            let minus = (k as i32 - abs).abs();
                            dp[i][j][minus as usize] = true;
                        }
                    }
                }
                if j >= 1 {
                    for k in 0..(k_num + 1) {
                        // i-1,jから生成
                        if dp[i][j - 1][k] {
                            let plus = k as i32 + abs;
                            if plus <= k_num as i32 {
                                dp[i][j][plus as usize] = true;
                            }

                            let minus = (k as i32 - abs).abs();
                            dp[i][j][minus as usize] = true;
                        }
                    }
                }
                if i == 0 && j == 0 {
                    dp[i][j][abs as usize] = true;
                }
            }
            j += 1;
        }
    }

    for k in 0..(k_num + 1) {
        if dp[w - 1][h - 1][k] {
            println!("{}", k);
            break;
        }
    }
}


Measured memory_bytes: 49532