Language: C++

//Compete against Yourself.
#include <bits/stdc++.h>
#define int long long
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define sz(i) (int)(i.size())
#define F first
#define S second
#define L long double
#define P pair<int, int>
const int mod = (int)1e9 + 7;
using namespace std;
#if LOCAL
#include "../../tr.h"
#else
#define tr(...)
#define endl '\n'
#endif
template<class T> void smin(T& a, T val) {if (a > val) a = val;}template<class T> void smax(T& a, T val) {if (a < val) a = val;}
const int N = 80;

int n, m, a[N][N], b[N][N];
gp_hash_table<int, int> dp[N][N];

void add(int x, int y, int red, int blue){
   if(x-1 >= 0){
      for(auto it: dp[x-1][y]){
         dp[x][y][abs(it.F+(red-blue))] = 1;
         dp[x][y][abs(it.F-(red-blue))] = 1;
      }
   }
   if(y-1 >= 0){
      for(auto it: dp[x][y-1]){
         dp[x][y][abs(it.F+(red-blue))] = 1;
         dp[x][y][abs(it.F-(red-blue))] = 1;
      }
   }
   if(x-1 < 0 and y-1 < 0){
      dp[x][y][abs(red-blue)] = 1;
   }
}


int32_t main(){_

   cin>>n>>m;

   for (int i = 0; i < n; ++i)
   {
      for (int j = 0; j < m; ++j)
      {
         cin >> a[i][j];
      }
   }

   for (int i = 0; i < n; ++i)
   {
      for (int j = 0; j < m; ++j)
      {
         cin >> b[i][j];
      }
   }

   for (int i = 0; i < n; ++i)
   {
      for (int j = 0; j < m; ++j)
      {
         add(i, j, a[i][j], b[i][j]);
      }  
   }

   tr(n, m, dp[n-1][m-1]);
   for(auto it: dp[n-1][m-1]){
      cout << it.F;
      exit(0);
   }

   return 0;
}


Compiler output:
In file included from /usr/include/c++/14/ext/pb_ds/detail/type_utils.hpp:47,
                 from /usr/include/c++/14/ext/pb_ds/tag_and_trait.hpp:46,
                 from /usr/include/c++/14/ext/pb_ds/assoc_container.hpp:46,
                 from a.cc:4:
/usr/include/c++/14/tr1/type_traits:123:3: error: redefinition of 'struct std::tr1::__is_integral_helper<long long int>'
  123 |   _DEFINE_SPEC(0, __is_integral_helper, long long, true)
      |   ^
/usr/include/c++/14/tr1/type_traits:117:3: note: previous definition of 'struct std::tr1::__is_integral_helper<long long int>'
  117 |   _DEFINE_SPEC(0, __is_integral_helper, int, true)
      |   ^
/usr/include/c++/14/tr1/type_traits:124:3: error: redefinition of 'struct std::tr1::__is_integral_helper<long long unsigned int>'
  124 |   _DEFINE_SPEC(0, __is_integral_helper, unsigned long long, true)
      |   ^
/usr/include/c++/14/tr1/type_traits:118:3: note: previous definition of 'struct std::tr1::__is_integral_helper<long long unsigned int>'
  118 |   _DEFINE_SPEC(0, __is_integral_helper, unsigned int, true)
      |   ^
/usr/include/c++/14/tr1/type_traits:554:3: error: redefinition of 'struct std::tr1::__is_signed_helper<long long int>'
  554 |   _DEFINE_SPEC(0, __is_signed_helper, long long, true)
      |   ^
/usr/include/c++/14/tr1/type_traits:552:3: note: previous definition of 'struct std::tr1::__is_signed_helper<long long int>'
  552 |   _DEFINE_SPEC(0, __is_signed_helper, int, true)
      |   ^
/usr/include/c++/14/tr1/type_traits:569:3: error: redefinition of 'struct std::tr1::__is_unsigned_helper<long long unsigned int>'
  569 |   _DEFINE_SPEC(0, __is_unsigned_helper, unsigned long long, true)
      |   ^
/usr/include/c++/14/tr1/type_traits:567:3: note: previous definition of 'struct std::tr1::__is_unsigned_helper<long long unsigned int>'
  567 |   _DEFINE_SPEC(0, __is_unsigned_helper, unsigned int, true)
      |   ^
In file included from /usr/include/c++/14/tr1/functional:43,
                 from /usr/include/c++/14/ext/pb_ds/detail/standard_policies.hpp:51,
                 from /usr/include/c++/14/ext/pb_ds/assoc_container.hpp:47:
/usr/include/c++/14/tr1/functional_hash.h:82:3: error: redefinition of 'std::size_t std::tr1::hash<_Tp>::operator()(_Tp) const [with _Tp = long long int; std::size_t = long unsigned int]'
   82 |   _TR1_hashtable_define_trivial_hash(long long)
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/tr1/functional_hash.h:80:3: note: 'std::size_t std::tr1::hash<_Tp>::operator()(_Tp) const [with _Tp = long long int; std::size_t = long unsigned int]' previously declared here
   80 |   _TR1_hashtable_define_trivial_hash(int)
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/tr1/functional_hash.h:86:3: error: redefinition of 'std::size_t std::tr1::hash<_Tp>::operator()(_Tp) const [with _Tp = long long unsigned int; std::size_t = long unsigned int]'
   86 |   _TR1_hashtable_define_trivial_hash(unsigned long long)
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/tr1/functional_hash.h:84:3: note: 'std::size_t std::tr1::hash<_Tp>::operator()(_Tp) const [with _Tp = long long unsigned int; std::size_t = long unsigned int]' previously declared here
   84 |   _TR1_hashtable_define_trivial_hash(unsigned int)
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/ext/pb_ds/detail/gp_hash_table_map_/gp_ht_map_.hpp:193,
                 from /usr/include/c++/14/ext/pb_ds/detail/container_base_dispatch.hpp:72,
                 from /usr/include/c++/14/ext/pb_ds/assoc_container.hpp:48:
/usr/include/c++/14/ext/pb_ds/detail/unordered_iterator/const_iterator.hpp: In instantiation of 'class __gnu_pbds::detail::gp_ht_map<long long int, long long int, std::tr1::hash<long long int>, std::equal_to<long long int>, std::allocator<char>, false, __gnu_pbds::direct_mask_range_hashing<>, __gnu_pbds::linear_probe_fn<long unsigned int>, __gnu_pbds::hash_standard_resize_policy<__gnu_pbds::hash_exponential_size_policy<>, __gnu_pbds::hash_load_check_resize_trigger<>, false, long unsigned int> >::const_iterator_':
/usr/include/c++/14/ext/pb_ds/detail/unordered_iterator/iterator.hpp:47:7:   required from 'class __gnu_pbds::detail::gp_ht_map<long long int, long long int, std::tr1::hash<long long int>, std::equal_to<long long int>, std::allocator<char>, false, __gnu_pbds::direct_mask_range_hashing<>, __gnu_pbds::linear_probe_fn<long unsigned int>, __gnu_pbds::hash_standard_resize_policy<__gnu_pbds::hash_exponential_size_policy<>, __gnu_pbds::hash_load_check_resize_trigger<>, false, long unsigned int> >::iterator_'
   47 | class iterator_
      |       ^~~~~~~~~
/usr/include/c++/14/ext/pb_ds/detail/gp_hash_table_map_/iterator_fn_imps.hpp:46:1:   required from 'class __gnu_pbds::detail::gp_ht_map<long long int, long long int, std::tr1::hash<long long int>, std::equal_to<long long int>, std::allocator<char>, false, __gnu_pbds::direct_mask_range_hashing<>, __gnu_pbds::linear_probe_fn<long unsigned int>, __gnu_pbds::hash_standard_resize_policy<__gnu_pbds::hash_exponential_size_policy<>, __gnu_pbds::hash_load_check_resize_trigger<>, false, long unsigned int> >'
   46 | PB_DS_CLASS_C_DEC::s_end_it;
      | ^
/usr/include/c++/14/ext/pb_ds/assoc_container.hpp:104:9:   required from 'class __gnu_pbds::basic_hash_table<long long int, long long int, std::tr1::hash<long long int>, std::equal_to<long long int>, __gnu_pbds::hash_standard_resize_policy<__gnu_pbds::hash_exponential_size_policy<>, __gnu_pbds::hash_load_check_resize_trigger<>, false, long unsigned int>, false, __gnu_pbds::gp_hash_tag, __gnu_cxx::typelist::node<__gnu_cxx::typelist::chain<__gnu_pbds::direct_mask_range_hashing<>, __gnu_cxx::typelist::chain<__gnu_pbds::linear_probe_fn<long unsigned int>, __gnu_cxx::typelist::null_type> > >, std::allocator<char> >'
  104 |   class basic_hash_table : public PB_DS_HASH_BASE
      |         ^~~~~~~~~~~~~~~~
/usr/include/c++/14/ext/pb_ds/assoc_container.hpp:368:9:   required from 'class __gnu_pbds::gp_hash_table<long long int, long long int>'
  368 |   class gp_hash_table : public PB_DS_GP_HASH_BASE
      |         ^~~~~~~~~~~~~
a.cc:25:32:   required from here
   25 | gp_hash_table<int, int> dp[N][N];
      |                                ^
/usr/include/c++/14/ext/pb_ds/detail/unordered_iterator/const_iterator.hpp:86:3: error: postfix '__gnu_pbds::detail::gp_ht_map<Key, Mapped, Hash_Fn, Eq_Fn, _Alloc, Store_Hash, Comb_Probe_Fn, Probe_Fn, Resize_Policy>::const_iterator_ __gnu_pbds::detail::gp_ht_map<Key, Mapped, Hash_Fn, Eq_Fn, _Alloc, Store_Hash, Comb_Probe_Fn, Probe_Fn, Resize_Policy>::const_iterator_::operator++(long long int) [with Key = long long int; Mapped = long long int; Hash_Fn = std::tr1::hash<long long int>; Eq_Fn = std::equal_to<long long int>; _Alloc = std::allocator<char>; bool Store_Hash = false; Comb_Probe_Fn = __gnu_pbds::direct_mask_range_hashing<>; Probe_Fn = __gnu_pbds::linear_probe_fn<long unsigned int>; Resize_Policy = __gnu_pbds::hash_standard_resize_policy<__gnu_pbds::hash_exponential_size_policy<>, __gnu_pbds::hash_load_check_resize_trigger<>, false, long unsigned int>]' must have 'int' as its argument
   86 |   operator++(int)
      |   ^~~~~~~~
In file included from /usr/include/c++/14/ext/pb_ds/detail/gp_hash_table_map_/gp_ht_map_.hpp:194:
/usr/include/c++/14/ext/pb_ds/detail/unordered_iterator/iterator.hpp: In instantiation of 'class __gnu_pbds::detail::gp_ht_map<long long int, long long int, std::tr1::hash<long long int>, std::equal_to<long long int>, std::allocator<char>, false, __gnu_pbds::direct_mask_range_hashing<>, __gnu_pbds::linear_probe_fn<long unsigned int>, __gnu_pbds::hash_standard_resize_policy<__gnu_pbds::hash_exponential_size_policy<>, __gnu_pbds::hash_load_check_resize_trigger<>, false, long unsigned int> >::iterator_':
/usr/include/c++/14/ext/pb_ds/detail/gp_hash_table_map_/iterator_fn_imps.hpp:46:1:   required from 'class __gnu_pbds::detail::gp_ht_map<long long int, long long int, std::tr1::hash<long long int>, std::equal_to<long long int>, std::allocator<char>, false, __gnu_pbds::direct_mask_range_hashing<>, __gnu_pbds::linear_probe_fn<long unsigned int>, __gnu_pbds::hash_standard_resize_policy<__gnu_pbds::hash_exponential_size_policy<>, __gnu_pbds::hash_load_check_resize_trigger<>, false, long unsigned int> >'
   46 | PB_DS_CLASS_C_DEC::s_end_it;
      | ^
/usr/include/c++/14/ext/pb_ds/assoc_container.hpp:104:9:   required from 'class __gnu_pbds::basic_hash_table<long long int, long long int, std::tr1::hash<long long int>, std::equal_to<long long int>, __gnu_pbds::hash_standard_resize_policy<__gnu_pbds::hash_exponential_size_policy<>, __gnu_pbds::hash_load_check_resize_trigger<>, false, long unsigned int>, false, __gnu_pbds::gp_hash_tag, __gnu_cxx::typelist::node<__gnu_cxx::typelist::chain<__gnu_pbds::direct_mask_range_hashing<>, __gnu_cxx::typelist::chain<__gnu_pbds::linear_probe_fn<long unsigned int>, __gnu_cxx::typelist::null_type> > >, std::allocator<char> >'
  104 |   class basic_hash_table : public PB_DS_HASH_BASE
      |         ^~~~~~~~~~~~~~~~
/usr/include/c++/14/ext/pb_ds/assoc_container.hpp:368:9:   required from 'class __gnu_pbds::gp_hash_table<long long int, long long int>'
  368 |   class gp_hash_table : public PB_DS_GP_HASH_BASE
      |         ^~~~~~~~~~~~~
a.cc:25:32:   required from here
   25 | gp_hash_table<int, int> dp[N][N];
      |                                ^
/usr/include/c++/14/ext/pb_ds/detail/unordered_iterator/iterator.hpp:113:3: error: postfix '__gnu_pbds::detail::gp_ht_map<Key, Mapped, Hash_Fn, Eq_Fn, _Alloc, Store_Hash, Comb_Probe_Fn, Probe_Fn, Resize_Policy>::iterator_ __gnu_pbds::detail::gp_ht_map<Key, Mapped, Hash_Fn, Eq_Fn, _Alloc, Store_Hash, Comb_Probe_Fn, Probe_Fn, Resize_Policy>::iterator_::operator++(long long int) [with Key = long long int; Mapped = long long int; Hash_Fn = std::tr1::hash<long long int>; Eq_Fn = std::equal_to<long long int>; _Alloc = std::allocator<char>; bool Store_Hash = false; Comb_Probe_Fn = __gnu_pbds::direct_mask_range_hashing<>; Probe_Fn = __gnu_pbds::linear_probe_fn<long unsigned int>; Resize_Policy = __gnu_pbds::hash_standard_resize_policy<__gnu_pbds::hash_exponential_size_policy<>, __gnu_pbds::hash_load_check_resize_trig