Language: C++

//import java.util.Scanner;
import java.io.*;

class Rmq{
    int segt[] = new int[(1<<21)*2];
    public int inp[] =new int[(1<<21)];
    int MAX_VALUE=2147483647;
    int min(int a,int b){return a<b?a:b;}
    int max(int a,int b){return a>b?a:b;}
    void update(int value,int pos,int n){
	int index=n-1+pos;
	segt[index]=value;//[pos,pos]
	while(index>0){
	    index=(index-1)/2;
	    segt[index]=min(segt[index*2+1],segt[index*2+2]);
	}
    }
    int query(int l,int r,int now,int ql,int qr){
	if (l == ql && r == qr){
	    return segt[now];
	}
	int midr=(l+r)/2,midl=midr+1;
	int ret=MAX_VALUE;
	if (ql <= midr)ret=min(ret,query(l,midr,now*2+1,ql,min(qr,midr)));
	if (midl <= qr)ret=min(ret,query(midl,r,now*2+2,max(midl,ql),qr));
	return ret;
    }

    public void init(int r,int c,int lim){
	boolean isrowmajor=r<=c;
	for(int i=0;i<lim;i++){
	    segt[i]=MAX_VALUE;
	}
	if (isrowmajor){
	    for(int i=0;i<r;i++){
		for(int j=0;j<c;j++){
		    update(inp[i*c+j],i*c+j,lim);
		}
	    }
	}else {
	    for(int i=0;i<r;i++){
		for(int j=0;j<c;j++){
		    update(inp[i+j*r],i+j*r,lim);
		}
	    }
	}
    }
    int searchmin(int r1,int c1,int r2,int c2,int r,int c,int lim){
	boolean isrowmajor = r<=c;  
	int ret=MAX_VALUE;
	lim--;
	if (isrowmajor){
	    for(int i=r1;i<=r2;i++){
		ret=min(ret,query(0,lim,0,i*c+c1,i*c+c2));
	    }
	}else {
	    for(int j=c1;j<=c2;j++){
		ret=min(ret,query(0,lim,0,j*r+r1,j*r+r2));
	    }
	}
	return ret;
    }    

}

class Main{
    void run(){
	Scanner in = new Scanner();
	//System.setOut(new PrintStream(new BufferedOutputStream(System.out)));
	int r,c,q;
	Rmq rmq=new Rmq();
	while(true){
	    r=in.nextInt();
	    c=in.nextInt();
	    q=in.nextInt();
	    if (r == 0)break;
	    int lim=1;
	    boolean isrowmajor = r<=c;
	    while(true){
		if (lim >= r*c)break;
		lim*=2;
	    }
	    if (isrowmajor){
		for(int i=0;i<r;i++){
		    for(int j=0;j<c;j++){
			rmq.inp[i*c+j]=in.nextInt();
		    }
		}
	    }else {
		for(int i=0;i<r;i++){
		    for(int j=0;j<c;j++){
			rmq.inp[i+j*r]=in.nextInt();
		    }
		}
	    }
	    rmq.init(r,c,lim);
	    for(int i=0;i<q;i++){
		int r1,r2,c1,c2;
		r1=in.nextInt();
		c1=in.nextInt();
		r2=in.nextInt();
		c2=in.nextInt();
		System.out.println(rmq.searchmin(r1,c1,r2,c2,r,c,lim));
	    }
	}
    }
    public static void main(String args[]){
	Main a = new Main();
	a.run();
    }
}


class Scanner {
    int nextInt() {
        try {
            int c = System.in.read();
            if (c == -1)
                return c;
            while (c != '-' && (c < '0' || '9' < c)) {
                c = System.in.read();
                if (c == -1)
                    return c;
            }
            if (c == '-')
                return -nextInt();
            int res = 0;
            do {
                res *= 10;
                res += c - '0';
                c = System.in.read();
            } while ('0' <= c && c <= '9');
            return res;
        } catch (Exception e) {
            return -1;
        }
    }
 
    long nextLong() {
        try {
            int c = System.in.read();
            if(c==-1)return -1;
            while (c != '-' && (c < '0' || '9' < c)){
                c = System.in.read();
                if(c==-1)return -1;
            }
            if (c == '-')
                return -nextLong();
            long res = 0;
            do {
                res *= 10;
                res += c - '0';
                c = System.in.read();
            } while ('0' <= c && c <= '9');
            return res;
        } catch (Exception e) {
            return -1;
        }
    }
 
    double nextDouble() {
        return Double.parseDouble(next());
    }
 
    String next() {
        try {
            StringBuilder res = new StringBuilder("");
            int c = System.in.read();
            while (Character.isWhitespace(c))
                c = System.in.read();
            do {
                res.append((char) c);
            } while (!Character.isWhitespace(c = System.in.read()));
            return res.toString();
        } catch (Exception e) {
            return null;
        }
    }
 
    String nextLine() {
        try {
            StringBuilder res = new StringBuilder("");
            int c = System.in.read();
            while (c == '\r' || c == '\n')
                c = System.in.read();
            do {
                res.append((char) c);
                c = System.in.read();
            } while (c != '\r' && c != '\n');
            return res.toString();
        } catch (Exception e) {
            return null;
        }
    }
}

Compiler output:
a.cc:2:1: error: 'import' does not name a type
    2 | import java.io.*;
      | ^~~~~~
a.cc:2:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:6:11: error: expected ':' before 'int'
    6 |     public int inp[] =new int[(1<<21)];
      |           ^~~~
      |           :
a.cc:29:11: error: expected ':' before 'void'
   29 |     public void init(int r,int c,int lim){
      |           ^~~~~
      |           :
a.cc:5:9: error: flexible array member 'Rmq::segt' not at end of 'class Rmq'
    5 |     int segt[] = new int[(1<<21)*2];
      |         ^~~~
a.cc:64:2: error: expected ';' after class definition
   64 | }
      |  ^
      |  ;
a.cc:5:9: error: initializer for flexible array member 'int Rmq::segt []'
    5 |     int segt[] = new int[(1<<21)*2];
      |         ^~~~
a.cc:6:16: error: initializer for flexible array member 'int Rmq::inp []'
    6 |     public int inp[] =new int[(1<<21)];
      |                ^~~
a.cc: In member function 'void Rmq::init(int, int, int)':
a.cc:30:9: error: 'boolean' was not declared in this scope; did you mean 'bool'?
   30 |         boolean isrowmajor=r<=c;
      |         ^~~~~~~
      |         bool
a.cc:34:13: error: 'isrowmajor' was not declared in this scope
   34 |         if (isrowmajor){
      |             ^~~~~~~~~~
a.cc: In member function 'int Rmq::searchmin(int, int, int, int, int, int, int)':
a.cc:49:9: error: 'boolean' was not declared in this scope; did you mean 'bool'?
   49 |         boolean isrowmajor = r<=c;
      |         ^~~~~~~
      |         bool
a.cc:52:13: error: 'isrowmajor' was not declared in this scope
   52 |         if (isrowmajor){
      |             ^~~~~~~~~~
a.cc: At global scope:
a.cc:107:11: error: expected ':' before 'static'
  107 |     public static void main(String args[]){
      |           ^~~~~~~
      |           :
a.cc:107:29: error: 'String' has not been declared
  107 |     public static void main(String args[]){
      |                             ^~~~~~
a.cc:111:2: error: expected ';' after class definition
  111 | }
      |  ^
      |  ;
a.cc: In member function 'void Main::run()':
a.cc:68:9: error: 'Scanner' was not declared in this scope
   68 |         Scanner in = new Scanner();
      |         ^~~~~~~
a.cc:73:15: error: 'in' was not declared in this scope; did you mean 'int'?
   73 |             r=in.nextInt();
      |               ^~
      |               int
a.cc:78:13: error: 'boolean' was not declared in this scope; did you mean 'bool'?
   78 |             boolean isrowmajor = r<=c;
      |             ^~~~~~~
      |             bool
a.cc:83:17: error: 'isrowmajor' was not declared in this scope
   83 |             if (isrowmajor){
      |                 ^~~~~~~~~~
a.cc:103:17: error: 'System' was not declared in this scope
  103 |                 System.out.println(rmq.searchmin(r1,c1,r2,c2,r,c,lim));
      |                 ^~~~~~
a.cc: In static member function 'static void Main::main(int*)':
a.cc:108:18: error: conversion from 'Main*' to non-scalar type 'Main' requested
  108 |         Main a = new Main();
      |                  ^~~~~~~~~~
a.cc: At global scope:
a.cc:165:5: error: 'String' does not name a type
  165 |     String next() {
      |     ^~~~~~
a.cc:180:5: error: 'String' does not name a type
  180 |     String nextLine() {
      |     ^~~~~~
a.cc:195:2: error: expected ';' after class definition
  195 | }
      |  ^
      |  ;
a.cc: In member function 'int Scanner::nextInt()':
a.cc:117:21: error: 'System' was not declared in this scope
  117 |             int c = System.in.read();
      |                     ^~~~~~
a.cc:134:18: error: 'Exception' does not name a type
  134 |         } catch (Exception e) {
      |                  ^~~~~~~~~
a.cc: In member function 'long int Scanner::nextLong()':
a.cc:141:21: error: 'System' was not declared in this scope
  141 |             int c = System.in.read();
      |                     ^~~~~~
a.cc:156:18: error: 'Exception' does not name a type
  156 |         } catch (Exception e) {
      |                  ^~~~~~~~~
a.cc: In member function 'double Scanner::nextDouble()':
a.cc:162:16: error: 'Double' was not declared in this scope; did you mean 'double'?
  162 |         return Double.parseDouble(next());
      |                ^~~~~~
      |                double
a.cc:162:35: error: 'next' was not declared in this scope; did you mean 'nextInt'?
  162 |         return Double.parseDouble(next());
      |                                   ^~~~
      |                                   nextInt