#include<bits/stdc++.h>// 20030830inlineintread(){int res =0;bool bo =0;char c;while(((c =getchar())<'0'|| c >'9')&& c !='-');if(c =='-') bo =1;else res = c -48;while((c =getchar())>='0'&& c <='9')
res =(res <<3)+(res <<1)+(c -48);return bo ?~res +1: res;}template<classT>inline T Min(const T &a,const T &b){return a < b ? a : b;}constint N =25e5+5, M =1e7+5, dx[]={-1,1,0,0},
dy[]={0,0,-1,1}, ZZQ =9999991;typedeflonglong ll;namespace Hash
{int ecnt, nxt[N], adj[ZZQ], tot[N]; ll val[N];voidclean(){while(ecnt--) adj[val[ecnt +1]% ZZQ]=0;
ecnt =0;}voidins(ll v,int t){int u = v % ZZQ;
nxt[++ecnt]= adj[u]; adj[u]= ecnt; tot[ecnt]= t;
val[ecnt]= v;}intquery(ll v){int u = v % ZZQ;for(int e = adj[u]; e; e = nxt[e])if(val[e]== v)return tot[e];return0;}};int n, m, c, X[N], Y[N], tot, col[N], ort[N][4], fa[N], ecnt, nxt[M],
adj[N], go[M], dfn[N], low[N], times, sze;bool wf[N], havecut;voidadd_edge(int u,int v){
nxt[++ecnt]= adj[u]; adj[u]= ecnt; go[ecnt]= v;
nxt[++ecnt]= adj[v]; adj[v]= ecnt; go[ecnt]= u;}
ll which(int x,int y){return1ll*(x -1)* m + y;}intcx(int x){if(fa[x]!= x) fa[x]=cx(fa[x]);return fa[x];}voidzm(int x,int y){int ix =cx(x), iy =cx(y);if(ix != iy) fa[iy]= ix;}voiddfs(int u,int rt){
sze++;int tql =0;
dfn[u]= low[u]=++times;for(int e = adj[u], v = go[e]; e; e = nxt[e], v = go[e])if(!dfn[v]){dfs(v, rt);
tql++;if(((u == rt && tql >1)||(u != rt && dfn[u]<= low[v]))&& wf[u]) havecut =1;
low[u]=Min(low[u], low[v]);}else low[u]=Min(low[u], dfn[v]);}voidwork(){
Hash::clean();int x, y;
n =read(); m =read(); c =read();if(!c)return(void)puts(1ll* n * m <=2?"-1":(n ==1|| m ==1?"1":"2"));
tot =0;for(int i =1; i <= c; i++)
X[i]=read(), Y[i]=read(), Hash::ins(which(X[i], Y[i]),++tot),
col[tot]=1, wf[tot]=0;for(int i =1; i <= c; i++)for(int a =-2; a <=2; a++)for(int b =-2; b <=2; b++){int tx = X[i]+ a, ty = Y[i]+ b;if(tx <1|| tx > n || ty <1|| ty > m)continue;
ll p =which(tx, ty);if(!Hash::query(p)) Hash::ins(p,++tot),
X[tot]= tx, Y[tot]= ty, col[tot]=0, wf[tot]=0;
wf[Hash::query(p)]|=abs(a)<=1&&abs(b)<=1;}for(int i =1; i <= tot; i++) fa[i]= i;for(int i =1; i <= tot; i++)for(int k =0; k <4; k++){int x = X[i]+ dx[k], y = Y[i]+ dy[k];if(x <1|| x > n || y <1|| y > m){
ort[i][k]=0;continue;}
ort[i][k]= Hash::query(which(x, y));if(ort[i][k])zm(i, ort[i][k]);}int ac =0;for(int i =1; i <= tot; i++) ac +=cx(i)== i;for(int i =1; i <= tot; i++) fa[i]= i;for(int i =1; i <= tot; i++)if(!col[i])for(int k =0; k <4; k++){int j = ort[i][k];if(!j)continue;if(!col[j])zm(i, j);}int wa =0;for(int i =1; i <= tot; i++) wa +=!col[i]&&cx(i)== i;if(ac < wa)return(void)puts("0");if(1ll* n * m - c <=2)return(void)puts("-1");
ecnt = times =0;for(int i =1; i <= tot; i++) adj[i]= dfn[i]=0;for(int i =1; i <= tot; i++)if(!col[i])for(int k =0; k <4; k +=2){int j = ort[i][k];if(!j)continue;if(!col[j])add_edge(i, j);}
havecut =0;for(int i =1; i <= tot; i++)if(sze =0,!col[i]&&!dfn[i]&&(dfs(i, i), havecut || sze ==2))return(void)puts("1");puts("2");}intmain(){int T =read();while(T--)work();return0;}