//饺子皮是圆皮 #include using namespace std; namespace fast_IO{ #define FASTIO #define IOSIZE 1000000 char ibuf[IOSIZE],obuf[IOSIZE];char*p1=ibuf,*p2=ibuf,*p3=obuf; #ifdef ONLINE_JUDGE #define getchar()((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++)) #define putchar(x)((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x) #endif #define isdigit(ch)(ch>47&&ch<58) #define isspace(ch)(ch<33) templateinline T read(){T s=0;int w=1;char ch;while(ch=getchar(),!isdigit(ch)and(ch!=EOF))if(ch=='-')w=-1;if(ch==EOF)return false;while(isdigit(ch))s=s*10+ch-48,ch=getchar();return s*w;}templateinline bool read(T&s){s=0;int w=1;char ch;while(ch=getchar(),!isdigit(ch)and(ch!=EOF))if(ch=='-')w=-1;if(ch==EOF)return false;while(isdigit(ch))s=s*10+ch-48,ch=getchar();return s*=w,true;}inline bool read(char&s){while(s=getchar(),isspace(s));return true;}inline bool read(char*s){char ch;while(ch=getchar(),isspace(ch));if(ch==EOF)return false;while(!isspace(ch))*s++=ch,ch=getchar();*s='\000';return true;}templateinline void print(T x){if(x<0)putchar('-'),x=-x;if(x>9)print(x/10);putchar(x%10+48);}inline void print(char x){putchar(x);}inline void print(char*x){while(*x)putchar(*x++);}inline void print(const char*x){for(int i=0;x[i];i++)putchar(x[i]);} #ifdef _GLIBCXX_STRING inline bool read(std::string&s){s="";char ch;while(ch=getchar(),isspace(ch));if(ch==EOF)return false;while(!isspace(ch))s+=ch,ch=getchar();return true;}inline void print(std::string x){for(int i=0,n=x.size();iinline int read(T&a,T1&...other){return read(a)+read(other...);}templateinline void print(T a,T1...other){print(a);print(other...);}struct Fast_IO{~Fast_IO(){fwrite(obuf,p3-obuf,1,stdout);}}io;templateFast_IO&operator>>(Fast_IO&io,T&b){return read(b),io;}templateFast_IO&operator<<(Fast_IO&io,T b){return print(b),io;} } #define cin fast_IO::io #define cout fast_IO::io #define endl '\n' // const int maxn=2e6+10; const int INF=0x3f3f3f3f; int n,h,w,q,cnt,Log; int s[510][510]; int id(int x,int y){return (x-1)*h+y;} struct Graph{ int x,y,w; bool operator < (const Graph &it)const{ return w=0;i--)if(dep[f[x][i]]>=dep[y])res=max(res,mx[x][i]),x=f[x][i]; if(x==y)return res; for(int i=Log;i>=0;i--)if(f[x][i]!=f[y][i])res=max(res,max(mx[x][i],mx[y][i])),x=f[x][i],y=f[y][i]; res=max(res,max(mx[x][0],mx[y][0])); return res; } int main(){ cin>>h>>w; n=h*w; for(int i=1;i<=h;i++)for(int j=1;j<=w;j++)cin>>s[i][j]; for(int i=1;i<=h;i++){ for(int j=1;j<=w;j++){ if(i!=1)e[++cnt]={id(i,j),id(i-1,j),max(s[i][j],s[i-1][j])}; if(j!=1)e[++cnt]={id(i,j),id(i,j-1),max(s[i][j],s[i][j-1])}; } } kruskal(); memset(fa,0,sizeof(fa)); Log=__lg(n); dfs(1); cin>>q; while(q--){ int x1,y1,x2,y2,x,y; cin>>x1>>y1>>x2>>y2; x=id(x1,y1),y=id(x2,y2); if(x==y){ cout<<0<<'\n'; continue ; } cout<