#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

#define int long long

int dx[] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};

int H, W, Q;
bool s[2000][2000];
int one[2000][2000] = {};
int d[2000][2000] = {}, imos_x_ud[2001][2001] = {}, imos_x_du[2001][2001] = {},
    imos_y_lr[2001][2001] = {}, imos_y_rl[2001][2001] = {};
int x_1[200000], y_1[200000], x_2[200000], y_2[200000];

struct node {
    int y, x, t;
};

signed main() {
    cin >> H >> W >> Q;
    for (int i = 0; i < H; i++) {
        string a;
        cin >> a;
        for (int j = 0; j < W; j++) {
            s[i][j] = a[j] == '1';
        }
    }
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            if (!s[i][j]) {
                continue;
            }
            queue<node> bfs;
            bfs.push({i, j, 1});
            s[i][j] = false;
            one[i][j] = 1;
            while (!bfs.empty()) {
                int y = bfs.front().y, x = bfs.front().x, t = bfs.front().t;
                d[y][x] = t;
                for (int i = 0; i < 4; i++) {
                    int ny = y + dy[i], nx = x + dx[i];
                    if (ny >= 0 && ny < H && nx >= 0 && nx < W && s[ny][nx]) {
                        bfs.push({ny, nx, t + 1});
                        s[ny][nx] = false;
                    }
                }
                bfs.pop();
            }
        }
    }
    for (int i = 0; i < H; i++) {
        for (int j = 1; j < W; j++) {
            one[i][j] += one[i][j - 1];
            if (d[i][j - 1] != 0 && d[i][j] != 0) {
                if (d[i][j - 1] < d[i][j]) {
                    imos_y_lr[i][j]++;
                } else {
                    imos_y_rl[i][j]++;
                }
            }
        }
    }
    for (int i = 1; i < H; i++) {
        for (int j = 0; j < W; j++) {
            one[i][j] += one[i - 1][j];
            if (d[i - 1][j] != 0 && d[i][j] != 0) {
                if (d[i - 1][j] < d[i][j]) {
                    imos_x_ud[i][j]++;
                } else {
                    imos_x_du[i][j]++;
                }
            }
        }
    }
    for (int i = 0; i < H; i++) {
        for (int j = 1; j < W; j++) {
            imos_x_ud[i][j] += imos_x_ud[i][j - 1];
            imos_x_du[i][j] += imos_x_du[i][j - 1];
        }
    }
    for (int i = 1; i < H; i++) {
        for (int j = 0; j < W; j++) {
            imos_y_lr[i][j] += imos_y_lr[i - 1][j];
            imos_y_rl[i][j] += imos_y_rl[i - 1][j];
        }
    }
    for (int i = 0; i < Q; i++) {
        cin >> y_1[i] >> x_1[i] >> y_2[i] >> x_2[i];
        x_1[i]--;
        y_1[i]--;
        x_2[i]--;
        y_2[i]--;
    }
    for (int i = 0; i < Q; i++) {
        int ans = 0;
        ans += imos_x_ud[y_1[i]][x_2[i]];
        ans += imos_x_du[y_2[i] + 1][x_2[i]];
        if (x_1[i] != 0) {
            ans -= imos_x_ud[y_1[i]][x_1[i] - 1];
            ans -= imos_x_du[y_2[i] + 1][x_1[i] - 1];
        }
        ans += imos_y_lr[y_2[i]][x_1[i]];
        ans += imos_y_rl[y_2[i]][x_2[i] + 1];
        if (y_1[i] != 0) {
            ans -= imos_y_lr[y_1[i] - 1][x_1[i]];
            ans -= imos_y_rl[y_1[i] - 1][x_2[i] + 1];
        }
        ans += one[y_2[i]][x_2[i]];
        if (x_1[i] != 0)
            ans -= one[y_2[i]][x_1[i] - 1];
        if (y_1[i] != 0)
            ans -= one[y_1[i] - 1][x_2[i]];
        if (x_1[i] != 0 && y_1[i] != 0)
            ans += one[y_1[i] - 1][x_1[i] - 1];
        cout << ans << endl;
    }
}

Measured memory_bytes: 199552