stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include <iostream>

#include <algorithm>

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <cmath>

#include <iomanip>

using namespace std;

int a,b,c,x,d;



int main()

{

    cin>>a>>b>>c>>x;

    for(int i = 0;i <= a;i ++)

    {

        for(int j = 0;j <= b;j ++)

        {

            for(int k = 0;k <= c;k ++)

                if(i * 500 + j * 100 + k * 50 == x)d ++;

        }

    }

    cout<<d<<endl;

}


// Optimized solution to the same problem (faster):
#include<cstdio>

using namespace std;

int A, B, C;

int X;

int solve(){

int result = 0;

for(int i = 0; i <= A; ++i){

for(int j = 0; j <= B; ++j){

for(int k = 0; k <= C; ++k){

int tmp = i * 500 + j * 100 + k * 50;

if(tmp == X) result++;

}

}

}

return result;

}

int main(){

scanf("%d%d%d%d", &A, &B, &C, &X);

int result = solve();

printf("%d\n", result);

return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <cstdio>

#include <cstdlib>

#include <cmath>

#include <cstdio>

#include <algorithm>

using namespace std;

int main(){

       int a,b,c,x;

       scanf("%d%d%d%d",&a,&b,&c,&x);

       int n=0;

       for(int i=0;i<=a;i++){

              for(int j=0;j<=b;j++){

                     for(int k=0;k<=c;k++){

                            if(i*500+j*100+k*50==x){

                                   n++;

                            }

                     }

              }

       }

       printf("%d",n);

       return 0;

}




// Optimized solution to the same problem (faster):
#include<cstdio>

using namespace std;

int A, B, C;

int X;

int solve(){

int result = 0;

for(int i = 0; i <= A; ++i){

for(int j = 0; j <= B; ++j){

for(int k = 0; k <= C; ++k){

int tmp = i * 500 + j * 100 + k * 50;

if(tmp == X) result++;

}

}

}

return result;

}

int main(){

scanf("%d%d%d%d", &A, &B, &C, &X);

int result = solve();

printf("%d\n", result);

return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <cstdio>

#include <cstring>

#include <cmath>

#include <string>

using namespace std;

int main(){

    int a,b,c;

    int x;

    scanf("%d%d%d%d",&a,&b,&c,&x);

    int i,j,k;

    int cnt = 0;

    for(i = 0; i <= a; i++){

        for(j = 0; j <= b; j++){

            for(k = 0; k <= c; k++){

                if(i*500+j*100+k*50==x)

                    cnt++;

            }

        }

    }

    printf("%d\n",cnt);

    return 0;

}


// Optimized solution to the same problem (faster):
#include<cstdio>

using namespace std;

int A, B, C;

int X;

int solve(){

int result = 0;

for(int i = 0; i <= A; ++i){

for(int j = 0; j <= B; ++j){

for(int k = 0; k <= C; ++k){

int tmp = i * 500 + j * 100 + k * 50;

if(tmp == X) result++;

}

}

}

return result;

}

int main(){

scanf("%d%d%d%d", &A, &B, &C, &X);

int result = solve();

printf("%d\n", result);

return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>



using namespace std;



//#define _FILES

#define PB push_back

#define MP make_pair

#define X first

#define Y second

const int MAXN = 2005;



int f[MAXN][MAXN], h[MAXN][MAXN], v[MAXN][MAXN], c[MAXN][MAXN];



int main()

{

    ios_base::sync_with_stdio(false);



    #ifdef _FILES

        freopen("","r",stdin);

        freopen("","w",stdout);

    #endif // _FILES

    int n, m, q;

    cin >> n >> m >> q;

    for (int i=1;i<=n;i++)

    {

        string s;

        cin >> s;

        for (int j=0;j<m;j++)

        {

            if (s[j] == '1')

            {

                f[i][j+1] = 1;

            }

        }

    }



    for (int i=n;i>=1;i--)

    {

        for (int j=m;j>=1;j--)

        {

            c[i][j] = c[i+1][j] + c[i][j+1] - c[i+1][j+1];



            if (f[i][j])

            {

                c[i][j]++;

                if (f[i+1][j])

                {

                    c[i][j]--;

                }



                if (f[i][j+1])

                {

                    c[i][j]--;

                }

            }



            v[i][j] = v[i+1][j];

            if ((f[i][j]) && (f[i][j+1]))

            {

                v[i][j]++;

            }



            h[i][j] = h[i][j+1];

            if ((f[i][j]) && (f[i+1][j]))

            {

                h[i][j]++;

            }

        }

    }



    for (int i=0;i<q;i++)

    {

        int x1, y1, x2, y2;

        cin >> x1 >> y1 >> x2 >> y2;

        int ans = c[x1][y1] + h[x2][y1] - h[x2][y2+1] + v[x1][y2] - v[x2+1][y2] - c[x2+1][y1] - c[x1][y2+1] + c[x2+1][y2+1];

        cout << ans << "\n";

    }

    return 0;

}




// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

using namespace std;

#define MAXN 2010

int sum1[MAXN][MAXN],sum2[MAXN][MAXN],cl[MAXN][MAXN],n,m,q;

char s[MAXN][MAXN];

int main()

{

	scanf("%d%d%d",&n,&m,&q);

	for(int i=1;i<=n;i++)

	{

		scanf("%s",s[i]+1);

		s[i][0]='0';

		for(int j=1;j<=m;j++)

		{

			sum1[i][j]=sum1[i][j-1];

			sum2[i][j]=sum2[i][j-1];

			if(s[i][j]=='1'&&s[i-1][j]=='1')

				sum1[i][j]++;

			if(s[i][j]=='1'&&s[i][j-1]=='0')

				sum2[i][j]++;

			if(s[i][j]=='1'&&s[i][j+1]=='1')

				cl[i][j]=1;

			cl[i][j]+=cl[i-1][j];

		}

		for(int j=1;j<=m;j++)

		{

			sum1[i][j]+=sum1[i-1][j];

			sum2[i][j]+=sum2[i-1][j];

		}

	}

	int x1,y1,x2,y2;

	while(q--)

	{

		scanf("%d%d%d%d",&x1,&y1,&x2,&y2);

		x1--,y1--;

		int ans=sum2[x2][y2]-sum2[x1][y2]-sum2[x2][y1]+sum2[x1][y1]+cl[x2][y1]-cl[x1][y1];

		ans-=sum1[x2][y2]-sum1[x1+1][y2]-sum1[x2][y1]+sum1[x1+1][y1];

		printf("%d\n",ans);

	}

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

#define F(i,a,b) for(int i=(a);i<=(b);++i)



int N,M,Q;

char str[2005];

int fk[2005][2005],f2[2005][2005],f3[2005][2005];



int main(){

	scanf("%d%d%d",&N,&M,&Q);

	F(i,1,N){

		scanf("%s",str+1);

		F(j,1,M){

			if(str[j]=='1'){

				fk[i][j]=1;

				if(fk[i-1][j]) f2[i-1][j]=1;

				if(fk[i][j-1]) f3[i][j-1]=1;

			}

		}

	}

	F(i,1,N) F(j,1,M)

		fk[i][j]+=fk[i-1][j]+fk[i][j-1]-fk[i-1][j-1],

		f2[i][j]+=f2[i-1][j]+f2[i][j-1]-f2[i-1][j-1],

		f3[i][j]+=f3[i-1][j]+f3[i][j-1]-f3[i-1][j-1];

	F(i,1,Q){

		int X1,Y1,X2,Y2;

		scanf("%d%d%d%d",&X1,&Y1,&X2,&Y2);

		int Dots=fk[X2][Y2]-fk[X1-1][Y2]-fk[X2][Y1-1]+fk[X1-1][Y1-1];

		int Edges=f2[X2-1][Y2]-f2[X1-1][Y2]-f2[X2-1][Y1-1]+f2[X1-1][Y1-1]+f3[X2][Y2-1]-f3[X1-1][Y2-1]-f3[X2][Y1-1]+f3[X1-1][Y1-1];

		printf("%d\n",Dots-Edges);

	}

	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

using namespace std;

#define MAXN 2010

int sum1[MAXN][MAXN],sum2[MAXN][MAXN],cl[MAXN][MAXN],n,m,q;

char s[MAXN][MAXN];

int main()

{

	scanf("%d%d%d",&n,&m,&q);

	for(int i=1;i<=n;i++)

	{

		scanf("%s",s[i]+1);

		s[i][0]='0';

		for(int j=1;j<=m;j++)

		{

			sum1[i][j]=sum1[i][j-1];

			sum2[i][j]=sum2[i][j-1];

			if(s[i][j]=='1'&&s[i-1][j]=='1')

				sum1[i][j]++;

			if(s[i][j]=='1'&&s[i][j-1]=='0')

				sum2[i][j]++;

			if(s[i][j]=='1'&&s[i][j+1]=='1')

				cl[i][j]=1;

			cl[i][j]+=cl[i-1][j];

		}

		for(int j=1;j<=m;j++)

		{

			sum1[i][j]+=sum1[i-1][j];

			sum2[i][j]+=sum2[i-1][j];

		}

	}

	int x1,y1,x2,y2;

	while(q--)

	{

		scanf("%d%d%d%d",&x1,&y1,&x2,&y2);

		x1--,y1--;

		int ans=sum2[x2][y2]-sum2[x1][y2]-sum2[x2][y1]+sum2[x1][y1]+cl[x2][y1]-cl[x1][y1];

		ans-=sum1[x2][y2]-sum1[x1+1][y2]-sum1[x2][y1]+sum1[x1+1][y1];

		printf("%d\n",ans);

	}

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

#define MN 2005

inline int read(){

	int x=0,f=1;char ch=getchar();

	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}

	while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}

	return x*f;

}

int n,m,q;

bool mp[MN][MN];

int l[MN][MN],h[MN][MN],b[MN][MN];

int main(){

	n=read(),m=read(),q=read();char c[MN];

	for(int i=1;i<=n;i++){

		scanf("%s",c+1);

		for(int j=1;j<=m;j++) mp[i][j]=c[j]=='1';

	}

	for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){

		l[i][j]=l[i-1][j]+l[i][j-1]-l[i-1][j-1]+(mp[i][j]&&mp[i][j-1]);

		h[i][j]=h[i-1][j]+h[i][j-1]-h[i-1][j-1]+(mp[i][j]&&mp[i-1][j]);

		b[i][j]=b[i-1][j]+b[i][j-1]-b[i-1][j-1]+(mp[i][j]);

	}

	int ans,x1,y1,x2,y2;

	while(q--){

		x1=read(),y1=read(),x2=read(),y2=read();

		ans=0;

		ans+=b[x2][y2]-b[x2][y1-1]-b[x1-1][y2]+b[x1-1][y1-1];

		ans-=l[x2][y2]-l[x2][y1]-l[x1-1][y2]+l[x1-1][y1];

		ans-=h[x2][y2]-h[x2][y1-1]-h[x1][y2]+h[x1][y1-1];

		printf("%d\n",ans);

	}

	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

using namespace std;

#define MAXN 2010

int sum1[MAXN][MAXN],sum2[MAXN][MAXN],cl[MAXN][MAXN],n,m,q;

char s[MAXN][MAXN];

int main()

{

	scanf("%d%d%d",&n,&m,&q);

	for(int i=1;i<=n;i++)

	{

		scanf("%s",s[i]+1);

		s[i][0]='0';

		for(int j=1;j<=m;j++)

		{

			sum1[i][j]=sum1[i][j-1];

			sum2[i][j]=sum2[i][j-1];

			if(s[i][j]=='1'&&s[i-1][j]=='1')

				sum1[i][j]++;

			if(s[i][j]=='1'&&s[i][j-1]=='0')

				sum2[i][j]++;

			if(s[i][j]=='1'&&s[i][j+1]=='1')

				cl[i][j]=1;

			cl[i][j]+=cl[i-1][j];

		}

		for(int j=1;j<=m;j++)

		{

			sum1[i][j]+=sum1[i-1][j];

			sum2[i][j]+=sum2[i-1][j];

		}

	}

	int x1,y1,x2,y2;

	while(q--)

	{

		scanf("%d%d%d%d",&x1,&y1,&x2,&y2);

		x1--,y1--;

		int ans=sum2[x2][y2]-sum2[x1][y2]-sum2[x2][y1]+sum2[x1][y1]+cl[x2][y1]-cl[x1][y1];

		ans-=sum1[x2][y2]-sum1[x1+1][y2]-sum1[x2][y1]+sum1[x1+1][y1];

		printf("%d\n",ans);

	}

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>



using namespace std;



int n,m,t,ans,k,a[2010][2010],b[2010][2010],c[2010][2010];

char mp[2010][2010];



int main()

{

	scanf("%d%d%d",&n,&m,&t);

	for(int i=1; i<=n; i++)

		scanf("%s",mp[i]+1);

	for(int i=1; i<=n; i++)

	{

		for(int j=1; j<=m; j++)

		{

			a[i][j]=a[i][j-1]+a[i-1][j]-a[i-1][j-1];

			b[i][j]=b[i][j-1]+b[i-1][j]-b[i-1][j-1];

			c[j][i]=c[j][i-1]+c[j-1][i]-c[j-1][i-1];

			if(mp[i][j]=='1')a[i][j]++;

			if(mp[i][j-1]=='1'&&mp[i][j]=='1')b[i][j]++;

			if(mp[i-1][j]=='1'&&mp[i][j]=='1')c[j][i]++;

		}

	}

	while(t--)

	{

		int x,y,xx,yy;

		scanf("%d%d%d%d",&x,&y,&xx,&yy);

		k=a[xx][yy]-a[x-1][yy]+a[x-1][y-1]-a[xx][y-1];

		ans=b[xx][yy]-b[xx][y]-b[x-1][yy]+b[x-1][y];

		ans+=c[yy][xx]-c[yy][x]-c[y-1][xx]+c[y-1][x];

		printf("%d\n",k-ans);

	}

	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

using namespace std;

#define MAXN 2010

int sum1[MAXN][MAXN],sum2[MAXN][MAXN],cl[MAXN][MAXN],n,m,q;

char s[MAXN][MAXN];

int main()

{

	scanf("%d%d%d",&n,&m,&q);

	for(int i=1;i<=n;i++)

	{

		scanf("%s",s[i]+1);

		s[i][0]='0';

		for(int j=1;j<=m;j++)

		{

			sum1[i][j]=sum1[i][j-1];

			sum2[i][j]=sum2[i][j-1];

			if(s[i][j]=='1'&&s[i-1][j]=='1')

				sum1[i][j]++;

			if(s[i][j]=='1'&&s[i][j-1]=='0')

				sum2[i][j]++;

			if(s[i][j]=='1'&&s[i][j+1]=='1')

				cl[i][j]=1;

			cl[i][j]+=cl[i-1][j];

		}

		for(int j=1;j<=m;j++)

		{

			sum1[i][j]+=sum1[i-1][j];

			sum2[i][j]+=sum2[i-1][j];

		}

	}

	int x1,y1,x2,y2;

	while(q--)

	{

		scanf("%d%d%d%d",&x1,&y1,&x2,&y2);

		x1--,y1--;

		int ans=sum2[x2][y2]-sum2[x1][y2]-sum2[x2][y1]+sum2[x1][y1]+cl[x2][y1]-cl[x1][y1];

		ans-=sum1[x2][y2]-sum1[x1+1][y2]-sum1[x2][y1]+sum1[x1+1][y1];

		printf("%d\n",ans);

	}

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <cstdio>

#include <cstring>

#include <iostream>

#include <algorithm>

using namespace std;

const int N=2005;

inline int read()

{

	int x=0,f=1,ch=getchar();

	while(ch<'0'||ch>'9'){if(ch=='-'){f=-1;}ch=getchar();}

	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}

	return x*f;

}

inline int cread()

{

	int ch=getchar();

	while(ch<'0'||ch>'9') ch=getchar();

	return ch-'0';

}

int n,m,q;

int map[N][N];

int sum[N][N],sum1[N][N],sum2[N][N];

inline int query(int x1,int y1,int x2,int y2)

{

	return sum[x2][y2]-sum[x2][y1]-sum[x1][y2]+sum[x1][y1];

}

inline int query1(int x1,int y1,int x2,int y2)

{

	x2--;

	return sum1[x2][y2]-sum1[x2][y1]-sum1[x1][y2]+sum1[x1][y1];

}

inline int query2(int x1,int y1,int x2,int y2)

{

	y2--;

	return sum2[x2][y2]-sum2[x2][y1]-sum2[x1][y2]+sum2[x1][y1];

}

int main()

{

	n=read(),m=read(),q=read();

	for(int i=1;i<=n;i++)

		for(int j=1;j<=m;j++)

			map[i][j]=cread();

	for(int i=1;i<=n;i++)

		for(int j=1;j<=m;j++)

			sum[i][j]=sum[i-1][j]+sum[i][j-1]+map[i][j]-sum[i-1][j-1];

	for(int i=1;i<=n;i++)

		for(int j=1;j<=m;j++)

		{

			if(map[i][j]&&map[i+1][j]) sum1[i][j]=1;

			if(map[i][j]&&map[i][j+1]) sum2[i][j]=1;

			sum1[i][j]+=sum1[i-1][j]+sum1[i][j-1]-sum1[i-1][j-1];

			sum2[i][j]+=sum2[i-1][j]+sum2[i][j-1]-sum2[i-1][j-1];

		}

	for(int i=1;i<=q;i++)

	{

		int x1=read(),y1=read(),x2=read(),y2=read();x1--,y1--;

//		cout<<query(x1,y1,x2,y2)<<"\t"<<query1(x1,y1,x2,y2)<<"\t"<<query2(x1,y1,x2,y2)<<endl;

		printf("%d\n",query(x1,y1,x2,y2)-query1(x1,y1,x2,y2)-query2(x1,y1,x2,y2));

	}

	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

using namespace std;

int n,sum[2005][2005],u[2005][2005],m,l[2005][2005],t[2005][2005];

char map[2005][2005];

int main()

{

	int q,x1,y1,x2,y2;

	scanf("%d%d%d",&n,&m,&q);

	for(int i=1;i<=n;i++) scanf("%s",map[i]+1);

	for(int i=1;i<=n;i++)

	for(int j=1;j<=m;j++){

		sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1];

		u[i][j]=u[i-1][j]+u[i][j-1]-u[i-1][j-1];

		l[i][j]=l[i-1][j];t[i][j]=t[i][j-1];

		if(map[i][j]=='1'){

			sum[i][j]++;

			if(map[i-1][j]=='1') u[i][j]++,t[i][j]++;

			if(map[i][j-1]=='1') u[i][j]++,l[i][j]++;

		}

	}

	while(q--)

	{

		scanf("%d%d%d%d",&x1,&y1,&x2,&y2);

		printf("%d\n",sum[x2][y2]-sum[x1-1][y2]-sum[x2][y1-1]+sum[x1-1][y1-1]-(u[x2][y2]-u[x1-1][y2]-u[x2][y1-1]+u[x1-1][y1-1]-t[x1][y2]-l[x2][y1]+t[x1][y1-1]+l[x1-1][y1]));

	}

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

 

using namespace std;

using namespace __gnu_pbds;

 

#define fi first

#define se second

#define mp make_pair

#define pb push_back

#define fbo find_by_order

#define ook order_of_key

 

typedef long long ll;

typedef pair<int,int> ii;

typedef vector<int> vi;

typedef long double ld; 

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

typedef set<int>::iterator sit;

typedef map<int,int>::iterator mit;

typedef vector<int>::iterator vit;



char a[2111][2111];

int up[2111][2111];

int ri[2111][2111];



int UP(int x1,int y1,int x2,int y2)

{

	return (up[x2][y2]-up[x2][y1-1]-up[x1-1][y2]+up[x1-1][y1-1]);

}



int RI(int x1,int y1,int x2,int y2)

{

	return (ri[x2][y2]-ri[x2][y1-1]-ri[x1-1][y2]+ri[x1-1][y1-1]);

}



int dp[2011][2011];



int DP(int x1,int y1,int x2,int y2)

{

	return (dp[x2][y2]-dp[x2][y1-1]-dp[x1-1][y2]+dp[x1-1][y1-1]);

}



int main()

{

	ios_base::sync_with_stdio(0); cin.tie(0);

	int n,m,q;

	cin>>n>>m>>q;

	for(int i=1;i<=n;i++)

	{

		for(int j=1;j<=m;j++)

		{

			cin>>a[i][j];

			if(a[i][j]=='1') dp[i][j]=1;

		}

	}

	for(int i=1;i<n;i++)

	{

		for(int j=1;j<=m;j++)

		{

			if(a[i][j]=='1'&&a[i+1][j]=='1')

			{

				up[i][j]=1;

			}

		}

	}

	for(int i=1;i<=n;i++)

	{

		for(int j=1;j<m;j++)

		{

			if(a[i][j]=='1'&&a[i][j+1]=='1')

			{

				ri[i][j]=1;

			}

		}

	}

	for(int i=1;i<=n;i++)

	{

		for(int j=1;j<=m;j++)

		{

			up[i][j]+=up[i-1][j]+up[i][j-1]-up[i-1][j-1];

			dp[i][j]+=dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1];

			ri[i][j]+=ri[i-1][j]+ri[i][j-1]-ri[i-1][j-1];

		}

	}

	for(int i=0;i<q;i++)

	{

		int x1,y1,x2,y2;

		cin>>x1>>y1>>x2>>y2;

		int V = DP(x1,y1,x2,y2);

		int E = RI(x1,y1,x2,y2-1)+UP(x1,y1,x2-1,y2);

		cout<<V-E<<'\n';

	}

}


// Optimized solution to the same problem (faster):
#include<cstdio>

const int MAXN=2005;



int N,M,Q;

bool grid[MAXN][MAXN];



int V[MAXN][MAXN],E1[MAXN][MAXN],E2[MAXN][MAXN];



int main()

{

	scanf("%d%d%d",&N,&M,&Q);

	for(int i=1;i<=N;i++)

		for(int j=1,x;j<=M;j++)

		{

			scanf("%1d",&x);

			grid[i][j]=x;

		}

	for(int i=1;i<=N;i++)

		for(int j=1;j<=M;j++)

			V[i][j]=V[i-1][j]+V[i][j-1]-V[i-1][j-1]+grid[i][j];

	for(int i=1;i<=N;i++)

		for(int j=1;j<=M;j++)

			E1[i][j]=E1[i-1][j]+E1[i][j-1]-E1[i-1][j-1]+(grid[i][j]&&grid[i][j-1]);

	for(int i=1;i<=N;i++)

		for(int j=1;j<=M;j++)

			E2[i][j]=E2[i-1][j]+E2[i][j-1]-E2[i-1][j-1]+(grid[i][j]&&grid[i-1][j]);

	

	while(Q--)

	{

		int x1,y1,x2,y2;

		scanf("%d%d%d%d",&x1,&y1,&x2,&y2);

		printf("%d\n",

		(V[x2][y2]-V[x2][y1-1]-V[x1-1][y2]+V[x1-1][y1-1])

		-(E1[x2][y2]-E1[x2][y1]-E1[x1-1][y2]+E1[x1-1][y1])

		-(E2[x2][y2]-E2[x2][y1-1]-E2[x1][y2]+E2[x1][y1-1]));

	}

	

	return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;



#define FOR(i,a,b) for(int i = (a); i < (b); i++)

#define RFOR(i,b,a) for(int i = (b) - 1; i >= (a); i--)

#define ITER(it, a) for(typeof(a.begin()) it = a.begin(); it != a.end(); it++)

#define FILL(a, value) memset(a, value, sizeof(a))



#define SZ(a) (int) a.size()

#define ALL(a) a.begin(),a.end()

#define PB push_back

#define MP make_pair



typedef long long LL;

typedef vector<int> VI;

typedef pair<int, int> PII;



const double PI = acos(-1.0);

const LL INF = 1000 * 1000 * 1000 + 7;

const LL LINF = INF * (LL)INF;



const int MAX = 2020;



int V[MAX][MAX];

int EX[MAX][MAX];

int EY[MAX][MAX];

char S[MAX][MAX];



int getS(int x1, int y1, int x2, int y2, int a[][MAX])

{

	int res = a[x2][y2];

	if (x1) res -= a[x1-1][y2];

	if (y1) res -= a[x2][y1 - 1];

	if (x1 && y1) res += a[x1-1][y1-1];

	return res;

}



int main()

{

	//freopen("in.txt", "r", stdin);

	//ios::sync_with_stdio(false); cin.tie(0);



	int n, m, q;

	scanf("%d%d%d", &n, &m, &q);

	FOR (i, 0, n)

	{

		scanf("%s", S[i]);

		FOR (j, 0, m)

		{

			S[i][j] -= '0';

		}

	}



	FOR (i, 0, n)

	{

		FOR (j, 0, m)

		{

			V[i][j] += S[i][j];

			if (i) V[i][j] += V[i-1][j];

			if (j) V[i][j] += V[i][j-1];

			if (i && j) V[i][j] -= V[i-1][j-1];



			if (i) EX[i][j] += S[i][j] & S[i-1][j];

			if (j) EY[i][j] += S[i][j] & S[i][j-1];



			if (i) EX[i][j] += EX[i-1][j];

			if (j) EX[i][j] += EX[i][j-1];

			if (i && j) EX[i][j] -= EX[i-1][j-1];



			if (i) EY[i][j] += EY[i-1][j];

			if (j) EY[i][j] += EY[i][j-1];

			if (i && j) EY[i][j] -= EY[i-1][j-1];

		}

	}



	FOR (i, 0, q)

	{

		int x1, y1, x2, y2;

		scanf("%d%d%d%d", &x1, &y1, &x2, &y2);

		x1--;

		y1--;

		x2--;

		y2--;



		int v = getS(x1, y1, x2, y2, V);

		int ex = getS(x1 + 1, y1, x2, y2, EX);

		int ey = getS(x1, y1 + 1, x2, y2, EY);



		int e = ex + ey;



		int cnt = v - e;

		printf("%d\n", cnt);



	}



}


// Optimized solution to the same problem (faster):
#include<cstdio>

const int MAXN=2005;



int N,M,Q;

bool grid[MAXN][MAXN];



int V[MAXN][MAXN],E1[MAXN][MAXN],E2[MAXN][MAXN];



int main()

{

	scanf("%d%d%d",&N,&M,&Q);

	for(int i=1;i<=N;i++)

		for(int j=1,x;j<=M;j++)

		{

			scanf("%1d",&x);

			grid[i][j]=x;

		}

	for(int i=1;i<=N;i++)

		for(int j=1;j<=M;j++)

			V[i][j]=V[i-1][j]+V[i][j-1]-V[i-1][j-1]+grid[i][j];

	for(int i=1;i<=N;i++)

		for(int j=1;j<=M;j++)

			E1[i][j]=E1[i-1][j]+E1[i][j-1]-E1[i-1][j-1]+(grid[i][j]&&grid[i][j-1]);

	for(int i=1;i<=N;i++)

		for(int j=1;j<=M;j++)

			E2[i][j]=E2[i-1][j]+E2[i][j-1]-E2[i-1][j-1]+(grid[i][j]&&grid[i-1][j]);

	

	while(Q--)

	{

		int x1,y1,x2,y2;

		scanf("%d%d%d%d",&x1,&y1,&x2,&y2);

		printf("%d\n",

		(V[x2][y2]-V[x2][y1-1]-V[x1-1][y2]+V[x1-1][y1-1])

		-(E1[x2][y2]-E1[x2][y1]-E1[x1-1][y2]+E1[x1-1][y1])

		-(E2[x2][y2]-E2[x2][y1-1]-E2[x1][y2]+E2[x1][y1-1]));

	}

	

	return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

#define R register

#define L 2002



inline void read(int &x){

    static int ch; static bool flag;

    for(flag=0,ch=getchar(); ch>'9'||ch<'0'; ch=getchar()) flag |= ch=='-';

    for(x=0; ch>='0'&&ch<='9'; ch=getchar()) x = (x<<1) + (x<<3) + ch-48;

    x = flag ? -x : x; 

}



int N,M,Q,Ans;

char Map[L][L];

int node[L][L],x[L][L],y[L][L];



inline int calc(int a1,int b1,int a2,int b2,int s[L][L]){

	return s[a2][b2]-s[a1-1][b2]+s[a1-1][b1-1]-s[a2][b1-1];

}



int main()

{

	read(N),read(M),read(Q);

	for(R int i=1;i<=N;++i) scanf("%s",Map[i]+1);

	for(R int i=1;i<=N;++i)

		for(R int j=1;j<=M;++j){

			node[i][j] = Map[i][j]=='1';

			node[i][j] += node[i-1][j]+node[i][j-1]-node[i-1][j-1];

			x[i][j] = Map[i][j]=='1' && Map[i-1][j]=='1';

			x[i][j] += x[i-1][j]+x[i][j-1]-x[i-1][j-1];

			y[i][j] = Map[i][j]=='1' && Map[i][j-1]=='1';

			y[i][j] += y[i-1][j]+y[i][j-1]-y[i-1][j-1];

		}

	while(Q--){

		R int a1,b1,a2,b2; read(a1),read(b1),read(a2),read(b2); Ans=0;

		Ans += calc(a1,b1,a2,b2,node);

		Ans -= calc(a1+1,b1,a2,b2,x);

		Ans -= calc(a1,b1+1,a2,b2,y);

		printf("%d\n",Ans);

	}

	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

const int MAXN=2005;



int N,M,Q;

bool grid[MAXN][MAXN];



int V[MAXN][MAXN],E1[MAXN][MAXN],E2[MAXN][MAXN];



int main()

{

	scanf("%d%d%d",&N,&M,&Q);

	for(int i=1;i<=N;i++)

		for(int j=1,x;j<=M;j++)

		{

			scanf("%1d",&x);

			grid[i][j]=x;

		}

	for(int i=1;i<=N;i++)

		for(int j=1;j<=M;j++)

			V[i][j]=V[i-1][j]+V[i][j-1]-V[i-1][j-1]+grid[i][j];

	for(int i=1;i<=N;i++)

		for(int j=1;j<=M;j++)

			E1[i][j]=E1[i-1][j]+E1[i][j-1]-E1[i-1][j-1]+(grid[i][j]&&grid[i][j-1]);

	for(int i=1;i<=N;i++)

		for(int j=1;j<=M;j++)

			E2[i][j]=E2[i-1][j]+E2[i][j-1]-E2[i-1][j-1]+(grid[i][j]&&grid[i-1][j]);

	

	while(Q--)

	{

		int x1,y1,x2,y2;

		scanf("%d%d%d%d",&x1,&y1,&x2,&y2);

		printf("%d\n",

		(V[x2][y2]-V[x2][y1-1]-V[x1-1][y2]+V[x1-1][y1-1])

		-(E1[x2][y2]-E1[x2][y1]-E1[x1-1][y2]+E1[x1-1][y1])

		-(E2[x2][y2]-E2[x2][y1-1]-E2[x1][y2]+E2[x1][y1-1]));

	}

	

	return 0;

}


// Fastest known solution for this problem: