Language: C++

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
#include<deque>
#include<cmath>
#include<map>
#define F first
#define S second

using namespace std;
typedef pair<int,int> P;
static const int INF = (1<<29);
int h,w;
char cmap[1100][1100];
int par[1100][1100];
int rank[1100][1100];
P pos[1100];
bool used[1100][1100];
static const int dx[4] = {0,1,0,-1};
static const int dy[4] = {1,0,-1,0};


void init(){
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      par[i][j] = j+h*i;
      pos[j+h*i] = P(j,i);
      rank[i][j] = 0;
      used[i][j] = false;
    }
  }
}

int find(int x,int y){
  if(par[y][x] == (x+h*y))return (x+h*y);
  else return par[y][x] = find(pos[par[y][x]].F,pos[par[y][x]].S);
}

void unit(P x,P y){
  //cout <<"in unit, x(" << x.F << "," << x.S <<") " << ",y(" << y.F << "," <<y.S << ")" << endl;
  //int X = find(x.F,x.S);
  //cout << "X = " << X;
  //int Y = find(y.F,y.S);
  //cout << ",Y = " << Y << endl;
  //if(X == Y)return;
  int a = find(x.F,x.S),b = find(y.F,y.S);
  if(a != b){
    P p = pos[a];
    par[p.S][p.F] = b;
  }
  /*
  if(rank[x.S][x.F] < rank[y.S][y.F]){
    par[x.S][x.F] = y.F+y.S*h;
  }else{
    par[y.S][y.F] = x.F+x.S*h;
    if(rank[x.S][x.F] == rank[y.S][y.F])rank[x.S][x.F]++;
  }
  */
}

bool same(P x,P y){
  return find(x.F,x.S) == find(y.F,y.S);
}

void display()
{
  cout << "--------par---------" << endl;
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      cout << par[i][j] << " ";
    }
    cout << endl;
  }
  
  cout << "--------rank--------" << endl;
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      cout << rank[i][j] << " ";
    }
    cout << endl;
  }
}


void dfs(int x,int y){
  used[y][x] = true;
  //cout  <<"x = "<< x << ", y = " << y << endl;
  for(int i=0;i<4;i++){
    int nx = x+dx[i],ny = y+dy[i];
    //cout << "P(" << x << "," << y << ") nx = " << nx << " ny = "<< ny << endl;
    if(0<= nx && nx < w && 0<= ny && ny < h && !used[ny][nx] && cmap[ny][nx] != '#'){
      P A = P(x,y),B = P(nx,ny);
      unit(A,B);
      dfs(nx,ny);
    }
  }

}







int main(){
  cin >> w >> h;
    int gx,gy;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	cin >> cmap[i][j];
	if(cmap[i][j] == 't'){
	  gx = j;
	  gy = i;
	}
      }
    }
    //cout << "pre init" << endl;
    init();
    //cout << "pre dfs" << endl;
    
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	if(cmap[i][j] == '#')continue;
	if(!used[i][j])dfs(j,i);
	
      }
    }

    for(int i=0;i<h;i++)for(int j=0;j<w;j++)used[i][j] = false;


 for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	if(cmap[i][j] == '#')continue;
	if(!used[i][j])dfs(j,i);
	
      }
    }
    
    /*
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	if(cmap[i][j] != '#'){
	  for(int k=0;k<4;k++){
	    int nx = j+dx[i],ny = i+dy[i];
	    if(0<=nx && nx <w && 0<=ny && ny<h && cmap[ny][nx] != '#'){
	      unit(P(nx,ny),P(j,i));
	    }
	  }
	}
      }
    }
    */
 //cout << "pre display" << endl;
 //display();


 bool zero = false;
 int N;
 cin >> N;
 if(find(gx,gy) == find(0,0)){
   zero = true;
 }
 P p;
 int cnt = -1;
 for(int i=0;i<N;i++){
   cin >> p.F >> p.S;
   cmap[p.S][p.F] = '.';
   for(int j=0;j<4;j++){
     int nx = p.F+dx[j],ny = p.S+dy[j];
     if(0<= nx && nx < w && 0<= ny && ny < h && cmap[ny][nx] != '#'){
       unit(p,P(nx,ny));
     }
   }
   if(find(gx,gy) == find(0,0) && cnt == -1){
     //cout << i+1 << endl;
     //display();
     //cnt++;
     cnt = i+1;
   }
 }

 if(zero){
   cout <<0 << endl;
   return 0;
 }
 //display();
 
 if(cnt != -1)cout << cnt << endl;
 else cout << -1 << endl;






  
  return 0;
}












#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
#include<deque>
#include<cmath>
#include<map>
#define F first
#define S second

using namespace std;
/*
static const int dx[] = {0,1,0,-1},dy[] = {1,0,-1,0};
int par[1010*1010];

int find(int x){
  if(x == par[x])return x;
  return find(par[x]);
}

void unit(int x,int y){
  x = find(x);
  y = find(y);
  if(x!=y){
    par[x] = y;
  }
}


int main(){
  int w,h,g;
  char cmap[1100][1100];
  cin >> w >> h;
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      p[k+i*h] = j+i*h;
      if(cmap[i][j] == 't')g = j+i*h;
    }
  }

  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      if(cmap[i][j] == '#')continue;
      for(int k=0;k<4;k++){
	int nx = j+dx[i],ny = i+dy[i];
	if(0<=nx && nx <w && 0<= ny && ny < h && cmap[ny][nx] != '#'){
	    unit(nx+ny*w,j+i*w);/**/
/*
	  }
      }
    }
  }


  return 0;
}
*/

typedef pair<int,int> P;
static const int INF = (1<<29);
int h,w;
char cmap[1100][1100];
int par[1100][1100];
int rank[1100][1100];
P pos[1100];
bool used[1100][1100];
static const int dx[4] = {0,1,0,-1};
static const int dy[4] = {1,0,-1,0};


void init(){
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      par[i][j] = j+w*i;
      pos[j+w*i] = P(j,i);
      rank[i][j] = 0;
      used[i][j] = false;
    }
  }
}

int find(int x,int y){
  if(par[y][x] == (x+w*y))return (x+w*y);
  else return par[y][x] = find(pos[par[y][x]].F,pos[par[y][x]].S);
}

void unit(P x,P y){
  //cout <<"in unit, x(" << x.F << "," << x.S <<") " << ",y(" << y.F << "," <<y.S << ")" << endl;
  //int X = find(x.F,x.S);
  //cout << "X = " << X;
  //int Y = find(y.F,y.S);
  //cout << ",Y = " << Y << endl;
  //if(X == Y)return;
  int a = find(x.F,x.S),b = find(y.F,y.S);
  if(a != b){
    P p = pos[a];
    par[p.S][p.F] = b;
  }
  /*
  if(rank[x.S][x.F] < rank[y.S][y.F]){
    par[x.S][x.F] = y.F+y.S*h;
  }else{
    par[y.S][y.F] = x.F+x.S*h;
    if(rank[x.S][x.F] == rank[y.S][y.F])rank[x.S][x.F]++;
  }
  */

}

bool same(P x,P y){
  return find(x.F,x.S) == find(y.F,y.S);
}

void display()
{
  cout << "--------par---------" << endl;
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      cout << par[i][j] << " ";
    }
    cout << endl;
  }
  
  cout << "--------rank--------" << endl;
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      cout << rank[i][j] << " ";
    }
    cout << endl;
  }
}


void dfs(int x,int y){
  used[y][x] = true;
  //cout  <<"x = "<< x << ", y = " << y << endl;
  for(int i=0;i<4;i++){
    int nx = x+dx[i],ny = y+dy[i];
    //cout << "P(" << x << "," << y << ") nx = " << nx << " ny = "<< ny << endl;
    if(0<= nx && nx < w && 0<= ny && ny < h && !used[ny][nx] && cmap[ny][nx] != '#'){
      P A = P(x,y),B = P(nx,ny);
      unit(A,B);
      dfs(nx,ny);
    }
  }

}







int main(){
  cin >> w >> h;
    int gx,gy;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	cin >> cmap[i][j];
	if(cmap[i][j] == 't'){
	  gx = j;
	  gy = i;
	}
      }
    }
    //cout << "pre init" << endl;
    init();
    //cout << "pre dfs" << endl;
    
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	if(cmap[i][j] == '#')continue;
	if(!used[i][j])dfs(j,i);
	
      }
    }

    for(int i=0;i<h;i++)for(int j=0;j<w;j++)used[i][j] = false;


 for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	if(cmap[i][j] == '#')continue;
	if(!used[i][j])dfs(j,i);
	
      }
    }
    
    /*
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	if(cmap[i][j] != '#'){
	  for(int k=0;k<4;k++){
	    int nx = j+dx[i],ny = i+dy[i];
	    if(0<=nx && nx <w && 0<=ny && ny<h && cmap[ny][nx] != '#'){
	      unit(P(nx,ny),P(j,i));
	    }
	  }
	}
      }
    }
    */
 //cout << "pre display" << endl;
 //display();


 bool zero = false;
 int N;
 cin >> N;
 if(find(gx,gy) == find(0,0)){
   zero = true;
 }
 P p;
 int cnt = -1;
 for(int i=0;i<N;i++){
   cin >> p.F >> p.S;
   cmap[p.S][p.F] = '.';
   for(int j=0;j<4;j++){
     int nx = p.F+dx[j],ny = p.S+dy[j];
     if(0<= nx && nx < w && 0<= ny && ny < h && cmap[ny][nx] != '#'){
       unit(p,P(nx,ny));
     }
   }
   if(find(gx,gy) == find(0,0) && cnt == -1){
     //cout << i+1 << endl;
     //display();
     //cnt++;
     cnt = i+1;
   }
 }

 if(zero){
   cout <<0 << endl;
   return 0;
 }
 //display();
 
 if(cnt != -1)cout << cnt << endl;
 else cout << -1 << endl;






  
  return 0;
}

Compiler output:
a.cc: In function 'void init()':
a.cc:30:7: error: reference to 'rank' is ambiguous
   30 |       rank[i][j] = 0;
      |       ^~~~
In file included from /usr/include/c++/14/bits/move.h:37,
                 from /usr/include/c++/14/bits/exception_ptr.h:41,
                 from /usr/include/c++/14/exception:166,
                 from /usr/include/c++/14/ios:41,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:18:5: note:                 'int rank [1100][1100]'
   18 | int rank[1100][1100];
      |     ^~~~
a.cc: In function 'void display()':
a.cc:80:15: error: reference to 'rank' is ambiguous
   80 |       cout << rank[i][j] << " ";
      |               ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:18:5: note:                 'int rank [1100][1100]'
   18 | int rank[1100][1100];
      |     ^~~~
a.cc: At global scope:
a.cc:275:18: error: redefinition of 'const int INF'
  275 | static const int INF = (1<<29);
      |                  ^~~
a.cc:14:18: note: 'const int INF' previously defined here
   14 | static const int INF = (1<<29);
      |                  ^~~
a.cc:276:5: error: redefinition of 'int h'
  276 | int h,w;
      |     ^
a.cc:15:5: note: 'int h' previously declared here
   15 | int h,w;
      |     ^
a.cc:276:7: error: redefinition of 'int w'
  276 | int h,w;
      |       ^
a.cc:15:7: note: 'int w' previously declared here
   15 | int h,w;
      |       ^
a.cc:277:6: error: redefinition of 'char cmap [1100][1100]'
  277 | char cmap[1100][1100];
      |      ^~~~
a.cc:16:6: note: 'char cmap [1100][1100]' previously declared here
   16 | char cmap[1100][1100];
      |      ^~~~
a.cc:278:5: error: redefinition of 'int par [1100][1100]'
  278 | int par[1100][1100];
      |     ^~~
a.cc:17:5: note: 'int par [1100][1100]' previously declared here
   17 | int par[1100][1100];
      |     ^~~
a.cc:279:5: error: redefinition of 'int rank [1100][1100]'
  279 | int rank[1100][1100];
      |     ^~~~
a.cc:18:5: note: 'int rank [1100][1100]' previously declared here
   18 | int rank[1100][1100];
      |     ^~~~
a.cc:280:3: error: redefinition of 'P pos [1100]'
  280 | P pos[1100];
      |   ^~~
a.cc:19:3: note: 'P pos [1100]' previously defined here
   19 | P pos[1100];
      |   ^~~
a.cc:281:6: error: redefinition of 'bool used [1100][1100]'
  281 | bool used[1100][1100];
      |      ^~~~
a.cc:20:6: note: 'bool used [1100][1100]' previously declared here
   20 | bool used[1100][1100];
      |      ^~~~
a.cc:282:18: error: redefinition of 'const int dx [4]'
  282 | static const int dx[4] = {0,1,0,-1};
      |                  ^~
a.cc:21:18: note: 'const int dx [4]' previously defined here
   21 | static const int dx[4] = {0,1,0,-1};
      |                  ^~
a.cc:283:18: error: redefinition of 'const int dy [4]'
  283 | static const int dy[4] = {1,0,-1,0};
      |                  ^~
a.cc:22:18: note: 'const int dy [4]' previously defined here
   22 | static const int dy[4] = {1,0,-1,0};
      |                  ^~
a.cc:286:6: error: redefinition of 'void init()'
  286 | void init(){
      |      ^~~~
a.cc:25:6: note: 'void init()' previously defined here
   25 | void init(){
      |      ^~~~
a.cc: In function 'void init()':
a.cc:291:7: error: reference to 'rank' is ambiguous
  291 |       rank[i][j] = 0;
      |       ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:18:5: note:                 'int rank [1100][1100]'
   18 | int rank[1100][1100];
      |     ^~~~
a.cc: At global scope:
a.cc:297:5: error: redefinition of 'int find(int, int)'
  297 | int find(int x,int y){
      |     ^~~~
a.cc:36:5: note: 'int find(int, int)' previously defined here
   36 | int find(int x,int y){
      |     ^~~~
a.cc:302:6: error: redefinition of 'void unit(P, P)'
  302 | void unit(P x,P y){
      |      ^~~~
a.cc:41:6: note: 'void unit(P, P)' previously defined here
   41 | void unit(P x,P y){
      |      ^~~~
a.cc:325:6: error: redefinition of 'bool same(P, P)'
  325 | bool same(P x,P y){
      |      ^~~~
a.cc:63:6: note: 'bool same(P, P)' previously defined here
   63 | bool same(P x,P y){
      |      ^~~~
a.cc:329:6: error: redefinition of 'void display()'
  329 | void display()
      |      ^~~~~~~
a.cc:67:6: note: 'void display()' previously defined here
   67 | void display()
      |      ^~~~~~~
a.cc: In function 'void display()':
a.cc:342:15: error: reference to 'rank' is ambiguous
  342 |       cout << rank[i][j] << " ";
      |               ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:18:5: note:                 'int rank [1100][1100]'
   18 | int rank[1100][1100];
      |     ^~~~
a.cc: At global scope:
a.cc:349:6: error: redefinition of 'void dfs(int, int)'
  349 | void dfs(int x,int y){
      |      ^~~
a.cc:87:6: note: 'void dfs(int, int)' previously defined here
   87 | void dfs(int x,int y){
      |      ^~~
a.cc:370:5: error: redefinition of 'int main()'
  370 | int main(){
      |     ^~~~
a.cc:108:5: note: 'int main()' previously defined here
  108 | int main(){
      |     ^~~~