Language: C++

#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define gc getchar
typedef long long LL;
typedef long double LD;
typedef pair<int,int> pii;
const int tb[]={0,1,0,-1};
const int tk[]={1,0,-1,0};
int petas[2][2005][2005]={},baris,kolom,q,prefiks[2][2005][2005]={},prefikslain[2][2005][2005]={};
template <typename T> void fastscan(T &angka){
	angka=0;
	char input=gc();
	while(!(48<=input&&input<=57))	input=gc();
	while(48<=input&&input<=57)	angka=(angka<<3)+(angka<<1)+input-48,input=gc();
}
bool masuk(int bar,int kol){
	return (1<=bar&&bar<=baris&&1<=kol&&kol<=kolom);
}
void isipetas(){
	for(int i=1;i<=baris;i++)
	{
		for(int j=1;j<=kolom;j++)
		{
			if(petas[0][i][j]==0)
				continue;
			for(int k=0;k<4;k++)
			{
				//cout<<"dari "<<i<<" "<<j<<" ke "<<i+tb[k]<<" "<<j+tk[k]<<endl;
				if(!masuk(i+tb[k],j+tk[k]))
					continue;
				petas[1][i][j]+=petas[0][i+tb[k]][j+tk[k]];
			}
		}
	}
}
void buatprefiks(int jenis){
	for(int i=1;i<=baris;i++)
		for(int j=1;j<=kolom;j++)
			prefiks[jenis][i][j]=prefiks[jenis][i-1][j]+prefiks[jenis][i][j-1]-prefiks[jenis][i-1][j-1]+petas[jenis][i][j];
}
void buatprefiks(){
	buatprefiks(1);
	buatprefiks(0);
}
int total(int jenis,pii awal,pii akhir){
	int ret=prefiks[jenis][akhir.fi][akhir.se]-prefiks[jenis][awal.fi-1][akhir.se]-prefiks[jenis][akhir.fi][awal.se-1]+prefiks[jenis][awal.fi-1][awal.se-1];
	return ret;
}
//0 itu melintang, 1 itu menurun
isiprefikslain(int jenis){
	if(jenis==0)
	{
		for(int i=1;i<=baris;i++)
		{
			prefikslain[jenis][i][0]=0;
			for(int j=1;j<=kolom;j++)
			{
				prefikslain[jenis][i][j]=prefikslain[jenis][i][j-1]+((petas[0][i][j]==1&&petas[0][i-1][j]==1)?1:0);
			}
		}
	}
	else
	{
		for(int j=1;j<=kolom;j++)
		{
			prefikslain[jenis][0][j]=0;
			for(int i=1;i<=baris;i++)
			{
				prefikslain[jenis][i][j]=prefikslain[jenis][i-1][j]+(petas[0][i][j-1]==1&&petas[0][i][j]==1)?1:0;
			}
		}
	}
}
isiprefikslain(){
	isiprefikslain(0);
	isiprefikslain(1);
}
int main()
{
	fastscan(baris),fastscan(kolom),fastscan(q);
	for(int i=1;i<=baris;i++)
	{
		for(int j=1;j<=kolom;j++)
		{
			char input=gc();
			while(!(48<=input&&input<=49))	input=gc();
			petas[0][i][j]=input-48;
		}
	}
	isipetas();
	buatprefiks();
	isiprefikslain();
	/*for(int i=1;i<=baris;i++)
	{
		for(int j=1;j<=kolom;j++)
			cout<<petas[0][i][j]<<" ";
		cout<<endl;
	}
	cout<<endl;*/
	/*for(int i=1;i<=baris;i++)
	{
		for(int j=1;j<=kolom;j++)
			cout<<petas[1][i][j]<<" ";
		cout<<endl;
	}
	cout<<endl;*/
	/*for(int i=1;i<=baris;i++)
	{
		for(int j=1;j<=kolom;j++)
			cout<<prefikslain[0][i][j]<<" ";
		cout<<endl;
	}
	cout<<endl;*/
	for(int i=1;i<=q;i++)
	{
		pii awal,akhir;
		fastscan(awal.fi),fastscan(awal.se);
		fastscan(akhir.fi),fastscan(akhir.se);
		int a=total(0,awal,akhir),b=total(1,awal,akhir);
		//a itu nodes, b itu edge
		//cout<<"awalnya "<<a<<" "<<b<<endl;
		b-=prefikslain[0][awal.fi][akhir.se]-prefikslain[0][awal.fi][awal.se-1];
		//cout<<b<<endl;
		b-=prefikslain[0][akhir.fi+1][akhir.se]-prefikslain[0][akhir.fi+1][awal.se-1];
		//cout<<b<<endl;
		b-=prefikslain[1][akhir.fi][awal.se]-prefikslain[1][awal.fi-1][awal.se];
		//cout<<b<<endl;
		b-=prefikslain[1][akhir.fi][akhir.se+1]-prefikslain[1][awal.fi-1][akhir.se+1];
		//cout<<"akhirnya "<<a<<" "<<b<<endl;
		b=b>>1;
		//cout<<"akhirnya "<<a<<" "<<b<<endl;
		printf("%d\n",a-b);
	}
}

Compiler output:
a.cc:55:1: error: ISO C++ forbids declaration of 'isiprefikslain' with no type [-fpermissive]
   55 | isiprefikslain(int jenis){
      | ^~~~~~~~~~~~~~
a.cc: In function 'int isiprefikslain(int)':
a.cc:78:1: warning: no return statement in function returning non-void [-Wreturn-type]
   78 | }
      | ^
a.cc: At global scope:
a.cc:79:1: error: ISO C++ forbids declaration of 'isiprefikslain' with no type [-fpermissive]
   79 | isiprefikslain(){
      | ^~~~~~~~~~~~~~
a.cc: In function 'int isiprefikslain()':
a.cc:82:1: warning: no return statement in function returning non-void [-Wreturn-type]
   82 | }
      | ^