// tested by Hightail: https://github.com/dj3500/hightail
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <stack>
#include <cstring>
#include <iomanip>
#include <ctime>
#include <cassert>
using namespace std;
#define pb push_back
#define INF 1001001001
#define FOR(i,n) for(int (i)=0;(i)<(n);++(i))
#define FORI(i,n) for(int (i)=1;(i)<=(n);++(i))
#define mp make_pair
#define pii pair<int,int>
#define ll long long
#define vi vector<int>
#define SZ(x) ((int)((x).size()))
#define fi first
#define se second
#define wez(n) int (n); scanf("%d",&(n));
#define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m));
#define wez3(n,m,k) int (n),(m),(k); scanf("%d %d %d",&(n),&(m),&(k));
inline void pisz(int n) { printf("%d\n",n); }
template<typename T,typename TT> ostream& operator<<(ostream &s,pair<T,TT> t) {return s<<"("<<t.first<<","<<t.second<<")";}
template<typename T> ostream& operator<<(ostream &s,vector<T> t){FOR(i,SZ(t))s<<t[i]<<" ";return s; }
#define DBG(vari) cout<<"["<<__LINE__<<"] "<<#vari<<" = "<<(vari)<<endl;
#define ALL(t) t.begin(),t.end()
#define FOREACH(i,t) for (__typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define TESTS wez(testow)while(testow--)
#define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
#define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i)
#define REMAX(a,b) (a)=max((a),(b));
#define REMIN(a,b) (a)=min((a),(b));
#define IOS ios_base::sync_with_stdio(0);

const int N = 2222;

int bit[N][N];
int maxvalx,maxvaly;

void czysc(int mxv, int myv) {
    maxvalx=mxv;
    maxvaly=myv;
    FOR(i,mxv+1) FOR(j,myv+1) bit[i][j]=0;
}

int suma(int x,int y) { // suma [1..x][1..y]
    x++;
    y++;
    int y1;
    int wyn=0;
    while(x>0) {
        y1=y;
        while(y1) {
            wyn+=bit[x][y1];
            y1-=(y1&-y1);
        }
        x-=(x&-x);
    }
    return wyn;
}

int suma(int a,int b,int c,int d) { // [a..c][b..d]
    a--;
    b--;
    return suma(c,d)-suma(a,d)-suma(c,b)+suma(a,b);
}

void dodaj(int x, int y, int val) {
    x++;
    y++;
	int y1;
	while(x<=maxvalx) {
		y1=y;
		while(y1<=maxvaly) {
			bit[x][y1]+=val;
			y1+=(y1&-y1); 
		}
		x+=(x&-x); 
	}
}

//pii parent[N][N];
int depth[N][N], depthtr[N][N];
char S[N][N];
bool seen[N][N];
const int dx[] = {-1,1,0,0}, dy[] = {0,0,-1,1};
int n,m;

bool ok (int i, int j) {
   return i >= 1 && i <= n && j >= 1 && j <= m;
}

void dfs (int i, int j, int de) {
   if (!ok(i,j)) return;
   if (S[i][j] != '1') return;
   if (seen[i][j]++) return;
   if (de == 5) {
      dodaj(i, j, 1);
      //DBG(mp(i,j))
      // root
   }
   //parent[i][j] = mp(pi,pj);
   depth[i][j] = de;
   FOR(d,4) {
      int i1 = i + dx[d], j1 = j + dy[d];
      dfs(i1, j1, de+1);
   }
}

int main () {
   scanf("%d %d", &n, &m);
   czysc(n+2,m+2);
   wez(Q)
   FORI(i,n) scanf("%s", S[i] + 1);
   FORI(i,n) FORI(j,m) {
      dfs(i,j,5);
   }
   FORI(i,n) FORI(j,m) depthtr[j][i] = depth[i][j];
   while (Q--) {
      wez2(x1,y1)
      wez2(x2,y2)
      int res = suma(x1, y1, x2, y2);
      REP(y,y1,y2) {
         //if ('1' == S[x1][y] && '1' == S[x1-1][y]) {
            if (depth[x1][y] == depth[x1-1][y] + 1) {
               ++res;
            }
         //}
      }
      REP(y,y1,y2) {
         //if ('1' == S[x2][y] && '1' == S[x2+1][y]) {
            if (depth[x2][y] == depth[x2+1][y] + 1) {
               ++res;
            }
         //}
      }
      REP(x,x1,x2) {
         //if ('1' == S[x][y1] && '1' == S[x][y1-1]) {
            //if (depth[x][y1] == depth[x][y1-1] + 1) {
            if (depthtr[y1][x] == depthtr[y1-1][x] + 1) {
               ++res;
            }
         //}
      }
      REP(x,x1,x2) {
         //if ('1' == S[x][y2] && '1' == S[x][y2+1]) {
            //if (depth[x][y2] == depth[x][y2+1] + 1) {
            if (depthtr[y2][x] == depthtr[y2+1][x] + 1) {
               ++res;
            }
         //}
      }
      pisz(res);
   }
}

Measured memory_bytes: 68224