# Connected Horses Problem

Kindly someone elaborate on the process in which input is taken in the following code?

Problem Statement :

``````import java.util.Scanner;
import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;
import static java.lang.Math.*;

public class Check2 implements Runnable{

{
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;

{
this.stream = stream;
}

{
if (numChars==-1)
throw new InputMismatchException();

if (curChar >= numChars)
{
curChar = 0;
try
{
}
catch (IOException e)
{
throw new InputMismatchException();
}

if(numChars <= 0)
return -1;
}
return buf[curChar++];
}

public String nextLine()
{
String str = "";
try
{
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
public int nextInt()
{

while(isSpaceChar(c))

int sgn = 1;

if (c == '-')
{
sgn = -1;
}

int res = 0;
do
{
if(c<'0'||c>'9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
}
while (!isSpaceChar(c));

return res * sgn;
}

public long nextLong()
{
while (isSpaceChar(c))
int sgn = 1;
if (c == '-')
{
sgn = -1;
}
long res = 0;

do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
}
while (!isSpaceChar(c));
return res * sgn;
}

public double nextDouble()
{
while (isSpaceChar(c))
int sgn = 1;
if (c == '-')
{
sgn = -1;
}
double res = 0;
while (!isSpaceChar(c) && c != '.')
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
}
if (c == '.')
{
double m = 1;
while (!isSpaceChar(c))
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
}
}
return res * sgn;
}

{
while (isSpaceChar(c))
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
}
while (!isSpaceChar(c));

return res.toString();
}

public boolean isSpaceChar(int c)
{
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '' || c == '\t' || c == -1;
}

public String next()
{
}

public interface SpaceCharFilter
{
public boolean isSpaceChar(int ch);
}
}

static int co=0,f=0;
static int n,m;

static void Check2(int n){
for(int i=0;i<=n;i++){
}

}

}
public static void main(String[] args) throws Exception {
new Thread(null, new Check2(), "Check2", 1<<26).start();

}
public void run(){
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
//Scanner in=new Scanner(System.in);
PrintWriter w=new PrintWriter(System.out);

long mod=1000000007l;
long fact[]=new long[1000001];
fact[0]=1;
fact[1]=1;
for(int i=2;i<=1000000;i++)
{
fact[i]=i*fact[i-1];
fact[i]%=mod;
}

int t=in.nextInt();
while(t-->0){

n=in.nextInt();
m=in.nextInt();
co=1;
int q=in.nextInt();
int a[][]=new int[n+1][m+1];

//	Check2(n);
for(int i=0;i<q;i++)
{
int c=in.nextInt();
int d=in.nextInt();
a[c][d]=1;

}

int v[][]=new int[n+1][m+1];
long ans=1;

for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++){

if(v[i][j]==0&&a[i][j]==1){
co=1;
dfs2(i,j,v,a);
//        w.println(co+" "+i+" "+j+"hiiiiiiiiiii");
ans=ans*fact[co];
ans%=mod;
}
}
}
w.println(ans);

}

w.close();

}

static void dfs(int i,int v[]){
if(v[i]==1){
return ;
}
else{

v[i]=1;
while(p.hasNext()){
Integer ne=p.next();

dfs(ne,v);
//
//        System.out.println(q+" "+i);
//System.out.println(i+" "+max1+" "+max2);
}

}

}

static void dfs2(int i,int j,int v[][],int a[][])
{

v[i][j]=1;
if(i<n-1&&j!=m&&a[i+2][j+1]==1&&v[i+2][j+1]==0)

{
co++;
v[i+2][j+1]=1;
dfs2(i+2,j+1,v,a);
}
if(i<n-1&&j!=0&&a[i+2][j-1]==1&&v[i+2][j-1]==0)

{
co++;
v[i+2][j-1]=1;
dfs2(i+2,j-1,v,a);
}
if(i>1&&j!=m&&a[i-2][j+1]==1&&v[i-2][j+1]==0)

{
co++;
v[i-2][j+1]=1;
dfs2(i-2,j+1,v,a);
}

if(i>1&&j!=0&&a[i-2][j-1]==1&&v[i-2][j-1]==0)

{
co++;
v[i-2][j-1]=1;
dfs2(i-2,j-1,v,a);
}
if(i!=n&&j>=2&&a[i+1][j-2]==1&&v[i+1][j-2]==0)

{
co++;
v[i+1][j-2]=1;
dfs2(i+1,j-2,v,a);
}

if(i!=n&&j<m-1&&a[i+1][j+2]==1&&v[i+1][j+2]==0)

{
co++;
v[i+1][j+2]=1;
dfs2(i+1,j+2,v,a);
}
if(i>=1&&j<m-1&&a[i-1][j+2]==1&&v[i-1][j+2]==0)

{
//    System.out.println(i+" "+j);
co++;
v[i-1][j+2]=1;
dfs2(i-1,j+2,v,a);
}

if(i>=1&&j>=2&&a[i-1][j-2]==1&&v[i-1][j-2]==0)

{
co++;
v[i-1][j-2]=1;
dfs2(i-1,j-2,v,a);
}

}

static class node{

int y;
int val;

node(int a,int b){

y=a;
val=b;

}

}

}
``````