Language: C++

    //For higher stack size use g++ -O2 -std=c++11 -Wall -Wl,--stack=268435456 a.cpp -o a.exe in cmd
    #include <bits/stdc++.h>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <cstring>
    #include <chrono>
    #include <complex>
    #define ll long long
    #define ld long double
    #define vi vector<int>
    #define vll vector<ll>
    #define vvi vector < vi >
    #define pii pair<int,int>
    #define pll pair<long long, long long>
    #define mod 1000000007
    #define inf 1000000000000000001;
    #define all(c) c.begin(),c.end()
    #define mp(x,y) make_pair(x,y)
    #define mem(a,val) memset(a,val,sizeof(a))
    #define eb emplace_back
    #define pb push_back
    #define f first
    #define s second
    
    using namespace std;

    int ar[101][101],n,m,ar2[101][101];

    bool chk(int i,int j)
    {
        if(i<=0 || j<=0 || i>n || j>m)
            return false;
        return true;
    }

    set<int> st[101][101];

    void func(int i,int j)
    {
        int a,b;
        a=ar[i][j]-ar2[i][j];
        b=ar2[i][j]-ar[i][j];
        
        if(i==n && j==m)
        {
            st[i][j].insert(a);
            st[i][j].insert(b);
            return;
        }
        if(chk(i+1,j))
        {
            if(st[i+1][j].empty())
                func(i+1,j);
            int X=800;
            for(auto it:st[i+1][j])
            {
                if(abs(it+a)<=X)
                    st[i][j].insert(abs(it+a));
                if(abs(it+b)<=X)
                    st[i][j].insert(abs(it+b));
                if(abs(-1*it+a)<=X)
                    st[i][j].insert(abs(-1*it+a));
                if(abs(-1*it+b)<=X)
                    st[i][j].insert(abs(-1*it+b));
            }
        }
        if(chk(i,j+1))
        {
            if(st[i][j+1].empty())
                func(i,j+1);
            for(auto it:st[i][j+1])
            {
                if(abs(it+a)<=X)
                    st[i][j].insert(abs(it+a));
                if(abs(it+b)<=X)
                    st[i][j].insert(abs(it+b));
                if(abs(-1*it+a)<=X)
                    st[i][j].insert(abs(-1*it+a));
                if(abs(-1*it+b)<=X)
                    st[i][j].insert(abs(-1*it+b));
            }
        }
    }

    int main()
    {
        std::ios::sync_with_stdio(false);
        int T;
        T=1;
        // cin.ignore(); must be there when using getline(cin, s)
        while(T--)
        {
            int i,j;
            cin>>n>>m;
            for(i=1;i<=n;++i)
            {
                for(j=1;j<=m;++j)
                    cin>>ar[i][j];
            }
            for(i=1;i<=n;++i)
                for(j=1;j<=m;++j)
                    cin>>ar2[i][j];
            func(1,1);
            int ans=1e8;
            for(auto it:st[1][1])
            {
                ans=min(ans,abs(it));
            }
            cout<<ans<<endl;
        }
        return 0;
    }


Compiler output:
a.cc: In function 'void func(int, int)':
a.cc:74:31: error: 'X' was not declared in this scope
   74 |                 if(abs(it+a)<=X)
      |                               ^
a.cc:76:31: error: 'X' was not declared in this scope
   76 |                 if(abs(it+b)<=X)
      |                               ^
a.cc:78:34: error: 'X' was not declared in this scope
   78 |                 if(abs(-1*it+a)<=X)
      |                                  ^
a.cc:80:34: error: 'X' was not declared in this scope
   80 |                 if(abs(-1*it+b)<=X)
      |                                  ^