typenew=recordx:longint;y:longint;z:longint;end;
vari,j,k,l,n,m,head,tail,took,x,y,cs:longint;a:array[1..1000000] of new;map,land,river:array[1..1000,1..1000] of longint;bz:array[1..1000,1..1000] of boolean;fx:array[1..8,1..2] of longint=((-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1));s:ansistring;
procedure riverbfs(dep:longint);
vari,j,k,l,nx,ny:longint;
beginfor i:=1 to 8 dobeginnx:=a[dep].x+fx[i,1];ny:=a[dep].y+fx[i,2];if(nx>0)and(nx<=n)and(ny>0)and(ny<=n) thenbeginif (bz[nx,ny])and(map[nx,ny]=1) thenbegininc(took);a[took].x:=nx;a[took].y:=ny;bz[nx,ny]:=false;river[nx,ny]:=cs;end;end;end;
end;
procedure landbfs(dep:longint);
vari,j,k,l,nx,ny:longint;
beginfor i:=1 to 8 dobeginnx:=a[dep].x+fx[i,1];ny:=a[dep].y+fx[i,2];if(nx>0)and(nx<=n)and(ny>0)and(ny<=n) thenbeginif (bz[nx,ny])and(map[nx,ny]=0) thenbegininc(took);a[took].x:=nx;a[took].y:=ny;bz[nx,ny]:=false;land[nx,ny]:=cs;end;end;end;
end;
begin//assign(input,'cross_the_river.in');reset(input);readln(n,m);fillchar(bz,sizeof(bz),true);for i:=1 to n dobeginreadln(s);for j:=1 to n dobeginif s[j]='1' then inc(map[i,j]);end;end;for i:=1 to n dobeginif map[1,i]=0 thenbeginhead:=1;tail:=1;took:=1;a[1].x:=1;a[1].y:=i;land[1,i]:=cs;bz[i,1]:=false;repeatfor j:=head to tail dolandbfs(j);head:=tail+1;tail:=took;until head>tail;end;if map[n,i]=0 thenbeginhead:=1;tail:=1;took:=1;a[1].x:=n;a[1].y:=i;land[n,i]:=cs;bz[i,1]:=false;repeatfor j:=head to tail dolandbfs(j);head:=tail+1;tail:=took;until head>tail;end;if map[i,1]=0 thenbeginhead:=1;tail:=1;took:=1;a[1].x:=i;a[1].y:=1;land[i,1]:=cs;bz[i,1]:=false;repeatfor j:=head to tail dolandbfs(j);head:=tail+1;tail:=took;until head>tail;end;if map[i,n]=0 thenbeginhead:=1;tail:=1;took:=1;a[1].x:=i;a[1].y:=n;land[i,n]:=cs;bz[i,1]:=false;repeatfor j:=head to tail dolandbfs(j);head:=tail+1;tail:=took;until head>tail;end;end;cs:=1;for i:=1 to n dobeginif map[1,i]=1 thenbeginhead:=1;tail:=1;took:=1;a[1].x:=1;a[1].y:=i;river[1,i]:=cs;bz[i,1]:=false;repeatfor j:=head to tail doriverbfs(j);head:=tail+1;tail:=took;until head>tail;end;if map[n,i]=1 thenbeginhead:=1;tail:=1;took:=1;a[1].x:=n;a[1].y:=i;river[n,i]:=cs;bz[i,1]:=false;repeatfor j:=head to tail doriverbfs(j);head:=tail+1;tail:=took;until head>tail;end;if map[i,1]=1 thenbeginhead:=1;tail:=1;took:=1;a[1].x:=i;a[1].y:=1;river[i,1]:=cs;bz[i,1]:=false;repeatfor j:=head to tail doriverbfs(j);head:=tail+1;tail:=took;until head>tail;end;if map[i,n]=1 thenbeginhead:=1;tail:=1;took:=1;a[1].x:=i;a[1].y:=n;river[i,n]:=cs;bz[i,1]:=false;repeatfor j:=head to tail doriverbfs(j);head:=tail+1;tail:=took;until head>tail;end;end;cs:=0;for i:=2 to n-1 dobeginfor j:=2 to n-1 dobeginif bz[i,j] thenbeginif map[i,j]=0 thenbeginfor k:=1 to 8 dobeginx:=i+fx[k,1];y:=j+fx[k,2];if river[x,y]>0 then cs:=river[x,y]else if (river[x,y]<cs)and(river[x,y]>0) then cs:=river[x,y];end;head:=1;tail:=1;took:=1;a[1].x:=i;a[1].y:=j;land[i,j]:=cs;bz[i,j]:=false;repeatfor k:=head to tail dolandbfs(k);head:=tail+1;tail:=took;until head>tail;endelsebeginfor k:=1 to 8 dobeginx:=i+fx[k,1];y:=j+fx[k,2];if land[x,y]>0 then cs:=land[x,y]else if (land[x,y]<cs)and(land[x,y]>0) then cs:=land[x,y];end;head:=1;tail:=1;took:=1;a[1].x:=i;a[1].y:=j;river[i,j]:=cs+1;inc(cs);bz[i,j]:=false;repeatfor k:=head to tail doriverbfs(k);head:=tail+1;tail:=took;until head>tail;end;end;end;end;for i:=1 to m dobeginreadln(x,y);write(land[x,y],' ');end;writeln;
end.