/* Author haleyk10198 */
/* �@��: haleyk10198 */
/* CF handle: haleyk100198*/
#include <bits/stdc++.h>
#define MOD 1000000007
#define LINF (1LL<<60)
#define INF 2147483647
#define PI 3.1415926535897932384626433
#define ll long long
#define pii pair<int,int>
#define pb push_back
#define mp(x,y) make_pair((x),(y))
using namespace std;
string itos(int x){
stringstream ss;
ss << x;
return ss.str();
}
int n, m, qu, b[10][100010];
vector<pii> e[10][4], con;
int vis[10][4];
queue<pii> q;
int fr, ff;
vector<pii> E[401100][10][2];
struct node{
int Err;
int res, isnull, cccon, lpos, rpos;
node(){
isnull = false;
res = cccon = lpos = rpos = 0;
}
};
node res;
node merge(node lhs, node rhs){
res.Err = fr++;
res.res = res.isnull = res.cccon = res.lpos = res.rpos = 0;
if(lhs.isnull)
return rhs;
if(rhs.isnull)
return lhs;
memset(vis, 0, sizeof(vis));
res.lpos = lhs.lpos, res.rpos = rhs.rpos;
for(int i = 0; i < n; i++)
for(int j = 0; j < 2; j++){
E[res.Err][i][j].clear();
e[i][j] = E[lhs.Err][i][j];
e[i][j+2] = E[rhs.Err][i][j];
for(auto &x: e[i][j+2])
x.second += 2;
}
for(int i = 0; i < n; i++)
if(b[i][lhs.rpos] == b[i][rhs.lpos])
e[i][1].pb(mp(i, 2)), e[i][2].pb(mp(i, 1));
for(int i = 0; i < n; i++){
for(int j = 0; j < 4; j++)
if(not vis[i][j]){
res.res++;
int f = 1;
while(q.size())
q.pop();
con.clear();
q.push(mp(i, j));
while(q.size()){
int x = q.front().first;
int y = q.front().second;
q.pop();
if(vis[x][y])
continue;
vis[x][y] = true;
for(auto i: e[x][y])
q.push(i);
if(y == 0 || y == 3){
con.pb(mp(x, y/3));
if(f)
res.cccon++, f = 0;
}
}
for(int i = 0; i+1 < con.size(); i++)
E[res.Err][con[i].first][con[i].second].pb(mp(con[i+1].first, con[i+1].second)), E[res.Err][con[i+1].first][con[i+1].second].pb(mp(con[i].first, con[i].second));
}
}
res.res += lhs.res+rhs.res-lhs.cccon-rhs.cccon;
return res;
}
node T[400010], a[100010];
void build(int l = 0, int r = m, int pos = 1){
if(l+1 == r){
T[pos] = a[l];
// cout << T[pos].Err << ' ';
return;
}
int mid = (l+r)/2;
build(l, mid, pos*2);
build(mid, r, pos*2+1);
T[pos].Err = fr++;
T[pos] = merge(T[pos*2], T[pos*2+1]);
return;
}
node ask(int l, int r, int x = 0, int y = m, int pos = 1){
if(x >= r || l >= y){
node res;
res.Err = fr++;
res.isnull = true;
return res;
}
if(l <= x && r >= y)
return T[pos];
int mid = (x+y)/2;
return merge(ask(l, r, x, mid, pos*2), ask(l, r, mid, y, pos*2+1));
}
int main(){
// freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
ios_base::sync_with_stdio(false);
cin >> n >> m >> qu;
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
cin >> b[i][j];
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++){
a[j].Err = j;
E[j][i][0].pb(mp(i, 1)), E[j][i][1].pb(mp(i, 0));
if(i+1 == n){
a[j].res += n;
a[j].cccon = a[j].res;
a[j].lpos = a[j].rpos = j;
continue;
}
if(b[i+1][j] == b[i][j])
E[j][i][0].pb(mp(i+1, 0)), E[j][i+1][0].pb(mp(i, 0)), a[j].res--;
}
fr = m;
build();
ff = fr;
while(qu--){
fr = ff;
int l, r;
cin >> l >> r;
cout << ask(--l, r).res << endl;
}
return 0;
}
LyogQXV0aG9yIGhhbGV5azEwMTk4ICovCi8qIO+/vUDvv73vv706ICBoYWxleWsxMDE5OCAqLwovKiBDRiBoYW5kbGU6IGhhbGV5azEwMDE5OCovCiNpbmNsdWRlIDxiaXRzL3N0ZGMrKy5oPgoKI2RlZmluZSBNT0QgMTAwMDAwMDAwNwojZGVmaW5lIExJTkYgKDFMTDw8NjApCiNkZWZpbmUgSU5GIDIxNDc0ODM2NDcKI2RlZmluZSBQSSAzLjE0MTU5MjY1MzU4OTc5MzIzODQ2MjY0MzMKI2RlZmluZSBsbCBsb25nIGxvb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y5FcnJdW2ldW2pdLmNsZWFyKCk7CiAgICAgICAgICAgIGVbaV1bal0gPSBFW2xocy5FcnJdW2ldW2pdOwogICAgICAgICAgICBlW2ldW2orMl0gPSBFW3Jocy5FcnJdW2ldW2pdOwogICAgICAgICAgICBmb3IoYXV0byAmeDogZVtpXVtqKzJdKQogICAgICAgICAgICAgICAgeC5zZWNvbmQgKz0gMjsKICAgICAgICB9CiAgICBmb3IoaW50IGkgPSAwOyBpIDwgbjsgaSsrKQogICAgICAgIGlmKGJbaV1bbGhzLnJwb3NdID09IGJbaV1bcmhzLmxwb3NdKQogICAgICAgICAgICBlW2ldWzFdLnBiKG1wKGksIDIpKSwgZVtpXVsyXS5wYihtcChpLCAxKSk7CiAgICBmb3IoaW50IGkgPSAwOyBpIDwgbjsgaSsrKXsKICAgICAgICBmb3IoaW50IGogPSAwOyBqIDwgNDsgaisrKQogICAgICAgIGlmKG5vdCB2aXNbaV1bal0pewogICAgICAgICAgICByZXMucmVzKys7CiAgICAgICAgICAgIGludCBmID0gMTsKICAgICAgICAgICAgd2hpbGUocS5zaXplKCkpCiAgICAgICAgICAgICAgICBxLnBvcCgpOwogICAgICAgICAgICBjb24uY2xlYXIoKTsKICAgICAgICAgICAgcS5wdXNoKG1wKGksIGopKTsKICAgICAgICAgICAgd2hpbGUocS5zaXplKCkpewogICAgICAgICAgICAgICAgaW50IHggPSBxLmZyb250KCkuZmlyc3Q7CiAgICAgICAgICAgICAgICBpbnQgeSA9IHEuZnJvbnQoKS5zZWNvbmQ7CiAgICAgICAgICAgICAgICBxLnBvcCgpOwogICAgICAgICAgICAgICAgaWYodmlzW3hdW3ldKQogICAgICAgICAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICAgICAgICAgdmlzW3hdW3ldID0gdHJ1ZTsKICAgICAgICAgICAgICAgIGZvcihhdXRvIGk6IGVbeF1beV0pCiAgICAgICAgICAgICAgICAgICAgcS5wdXNoKGkpOwogICAgICAgICAgICAgICAgaWYoeSA9PSAwIHx8IHkgPT0gMyl7CiAgICAgICAgICAgICAgICAgICAgY29uLnBiKG1wKHgsIHkvMykpOyAKICAgICAgICAgICAgICAgICAgICBpZihmKQogICAgICAgICAgICAgICAgICAgICAgICByZXMuY2Njb24rKywgZiA9IDA7CiAgICAgICAgICAgICAgICB9ICAgICAgICAgICAKICAgICAgICAgICAgfQogICAgICAgICAgICBmb3IoaW50IGkgPSAwOyBpKzEgPCBjb24uc2l6ZSgpOyBpKyspCiAgICAgICAgICAgICAgICBFW3Jlcy5FcnJdW2NvbltpXS5maXJzdF1bY29uW2ldLnNlY29uZF0ucGIobXAoY29uW2krMV0uZmlyc3QsIGNvbltpKzFdLnNlY29uZCkpLCBFW3Jlcy5FcnJdW2NvbltpKzFdLmZpcnN0XVtjb25baSsxXS5zZWNvbmRdLnBiKG1wKGNvbltpXS5maXJzdCwgY29uW2ldLnNlY29uZCkpOwogICAgICAgIH0KICAgIH0KICAgIHJlcy5yZXMgKz0gbGhzLnJlcytyaHMucmVzLWxocy5jY2Nvbi1yaHMuY2Njb247CiAgICByZXR1cm4gcmVzOwp9Cgpub2RlIFRbNDAwMDEwXSwgYVsxMDAwMTBdOwoKdm9pZCBidWlsZChpbnQgbCA9IDAsIGludCByID0gbSwgaW50IHBvcyA9IDEpewogICAgaWYobCsxID09IHIpewogICAgICAgIFRbcG9zXSA9IGFbbF07CiAgICAgICAgLy8gY291dCA8PCBUW3Bvc10uRXJyIDw8ICcgJzsKICAgICAgICByZXR1cm47CiAgICB9CiAgICBpbnQgbWlkID0gKGwrcikvMjsKICAgIGJ1aWxkKGwsIG1pZCwgcG9zKjIpOwogICAgYnVpbGQobWlkLCByLCBwb3MqMisxKTsKICAgIFRbcG9zXS5FcnIgPSBmcisrOwogICAgVFtwb3NdID0gbWVyZ2UoVFtwb3MqMl0sIFRbcG9zKjIrMV0pOwogICAgcmV0dXJuOwp9Cgpub2RlIGFzayhpbnQgbCwgaW50IHIsIGludCB4ID0gMCwgaW50IHkgPSBtLCBpbnQgcG9zID0gMSl7CiAgICBpZih4ID49IHIgfHwgbCA+PSB5KXsKICAgICAgICBub2RlIHJlczsKICAgICAgICByZXMuRXJyID0gZnIrKzsKICAgICAgICByZXMuaXNudWxsID0gdHJ1ZTsKICAgICAgICByZXR1cm4gcmVzOwogICAgfQogICAgaWYobCA8PSB4ICYmIHIgPj0geSkKICAgICAgICByZXR1cm4gVFtwb3NdOwogICAgaW50IG1pZCA9ICh4K3kpLzI7CiAgICByZXR1cm4gbWVyZ2UoYXNrKGwsIHIsIHgsIG1pZCwgcG9zKjIpLCBhc2sobCwgciwgbWlkLCB5LCBwb3MqMisxKSk7Cn0KCmludCBtYWluKCl7CiAgICAvLyBmcmVvcGVuKCJpbnB1dC50eHQiLCJyIixzdGRpbik7CiAgICAvL2ZyZW9wZW4oIm91dHB1dC50eHQiLCJ3IixzdGRvdXQpOwogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7CiAgICBjaW4gPj4gbiA+PiBtID4+IHF1OwogICAgZm9yKGludCBpID0gMDsgaSA8IG47IGkrKykKICAgICAgICBmb3IoaW50IGogPSAwOyBqIDwgbTsgaisrKQogICAgICAgICAgICBjaW4gPj4gYltpXVtqXTsKICAgIGZvcihpbnQgaSA9IDA7IGkgPCBuOyBpKyspCiAgICAgICAgZm9yKGludCBqID0gMDsgaiA8IG07IGorKyl7CiAgICAgICAgICAgIGFbal0uRXJyID0gajsKICAgICAgICAgICAgRVtqXVtpXVswXS5wYihtcChpLCAxKSksIEVbal1baV1bMV0ucGIobXAoaSwgMCkpOwogICAgICAgICAgICBpZihpKzEgPT0gbil7CiAgICAgICAgICAgICAgICBhW2pdLnJlcyArPSBuOwogICAgICAgICAgICAgICAgYVtqXS5jY2NvbiA9IGFbal0ucmVzOwogICAgICAgICAgICAgICAgYVtqXS5scG9zID0gYVtqXS5ycG9zID0gajsKICAgICAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmKGJbaSsxXVtqXSA9PSBiW2ldW2pdKQogICAgICAgICAgICAgICAgRVtqXVtpXVswXS5wYihtcChpKzEsIDApKSwgRVtqXVtpKzFdWzBdLnBiKG1wKGksIDApKSwgYVtqXS5yZXMtLTsKICAgICAgICB9CiAgICBmciA9IG07CiAgICBidWlsZCgpOwogICAgZmYgPSBmcjsKICAgIHdoaWxlKHF1LS0pewogICAgICAgIGZyID0gZmY7CiAgICAgICAgaW50IGwsIHI7CiAgICAgICAgY2luID4+IGwgPj4gcjsKICAgICAgICBjb3V0IDw8IGFzaygtLWwsIHIpLnJlcyA8PCBlbmRsOwogICAgfQogICAgcmV0dXJuIDA7Cn0=