Language: C++

#include<bits/stdc++.h>

using namespace std;

typedef long long int64;
const int64 INF = 1LL << 58;

struct StronglyConnectedComponents
{
  vector< vector< int > > gg, rg;
  vector< pair< int, int > > edges;
  vector< int > comp, order, used;

  StronglyConnectedComponents(size_t v) : gg(v), rg(v), comp(v, -1), used(v, 0) {}

  void add_edge(int x, int y)
  {
    gg[x].push_back(y);
    rg[y].push_back(x);
    edges.emplace_back(x, y);
  }

  int operator[](int k)
  {
    return (comp[k]);
  }

  void dfs(int idx)
  {
    if(used[idx]) return;
    used[idx] = true;
    for(int to : gg[idx]) dfs(to);
    order.push_back(idx);
  }

  void rdfs(int idx, int cnt)
  {
    if(comp[idx] != -1) return;
    comp[idx] = cnt;
    for(int to : rg[idx]) rdfs(to, cnt);
  }

  void build(vector< vector< int > > &t)
  {
    for(int i = 0; i < gg.size(); i++) dfs(i);
    reverse(begin(order), end(order));
    int ptr = 0;
    for(int i : order) rdfs(i, ptr), ptr++;

    t.resize(ptr);
    set< pair< int, int > > connect;
    for(auto &e : edges) {
      int x = comp[e.first], y = comp[e.second];
      if(x == y) continue;
      if(connect.count({x, y})) continue;
      t[x].push_back(y);
      connect.emplace(x, y);
    }
  }
};

int H, W, N;
int link[500][500];
bool mine[500][500];
int linked[250000];

int main()
{
  cin >> H >> W >> N;

  StronglyConnectedComponents scc(H * W);

  for(int i = 0; i < H; i++) {
    for(int j = 0; j < W; j++) {
      int y, x;
      cin >> y >> x;
      link[i][j] = y * W + x;
      linked[i * W + j] = link[i][j];
      if(y == i && x == j) {
        mine[i][j] = true;
      } else {
        scc.add_edge(i * W + j, link[i][j]);
      }
    }
  }

  vector< vector< int > > g;
  scc.build(g);
  vector< vector< int > > ventex(g.size());
  vector< set< int > > query(g.size());
  vector< int > beet(g.size(), 0);
  vector< int > dinamic(H * W, -1);

  for(int i = 0; i < H * W; i++) {
    ventex[scc[i]].push_back(i);
  }
  for(int i = 0; i < g.size(); i++) {
    if(ventex[i].empty()) continue;
    if(ventex[i].size() == 1 && !mine[ventex[i][0] / W][ventex[i][0] % W]) continue;
    beet[i] = true;
  }

  for(int i = 0; i < g.size(); i++) {
    if(ventex[i].empty()) continue;
    if(beet[i]) {
      int now = ventex[i][0];
      int ptr = 0;
      while(dinamic[now] == -1) {
        dinamic[now] = ptr++;
        now = linked[now];
      }
    }
  }

  for(int i = 0; i < N; i++) {
    int y, x;
    cin >> y >> x;
    y = y * W + x;
    query[scc[y]].emplace(beet[scc[y]] ? dinamic[y] : 0);
  }


  int64 res = INF;

  for(int i = 0; i < g.size(); i++) {
    if(ventex[i].empty()) continue;

    if(!beet[i]) {

      int future = linked[ventex[i][0]];
      bool commit = beet[scc[future]];

      for(int j : query[i]) {
        int nxt = j + 1;
        nxt = (j + 1 - dinamic[future] + ventex[scc[future]].size()) % ventex[scc[future]].size();
        if(query[scc[future]].count(nxt)) {
          res = min(res, j + 1LL);
        } else {
          query[scc[future]].insert(nxt);
        }

      }
    
  }

  if(res == INF) {
    cout << -1 << endl;
  } else {
    cout << res << endl;
  }
}

Compiler output:
a.cc:63:18: error: 'int link [500][500]' redeclared as different kind of entity
   63 | int link[500][500];
      |                  ^
In file included from /usr/include/x86_64-linux-gnu/bits/sigstksz.h:24,
                 from /usr/include/signal.h:328,
                 from /usr/include/c++/14/csignal:42,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:116,
                 from a.cc:1:
/usr/include/unistd.h:819:12: note: previous declaration 'int link(const char*, const char*)'
  819 | extern int link (const char *__from, const char *__to)
      |            ^~~~
a.cc: In function 'int main()':
a.cc:77:13: warning: pointer to a function used in arithmetic [-Wpointer-arith]
   77 |       link[i][j] = y * W + x;
      |             ^
a.cc:77:16: warning: pointer to a function used in arithmetic [-Wpointer-arith]
   77 |       link[i][j] = y * W + x;
      |                ^
a.cc:77:18: error: assignment of read-only location '*(link + (((sizetype)i) + ((sizetype)j)))'
   77 |       link[i][j] = y * W + x;
      |       ~~~~~~~~~~~^~~~~~~~~~~
a.cc:78:33: warning: pointer to a function used in arithmetic [-Wpointer-arith]
   78 |       linked[i * W + j] = link[i][j];
      |                                 ^
a.cc:78:36: warning: pointer to a function used in arithmetic [-Wpointer-arith]
   78 |       linked[i * W + j] = link[i][j];
      |                                    ^
a.cc:78:36: error: invalid conversion from 'int (*)(const char*, const char*) noexcept' to 'int' [-fpermissive]
   78 |       linked[i * W + j] = link[i][j];
      |                           ~~~~~~~~~^
      |                                    |
      |                                    int (*)(const char*, const char*) noexcept
a.cc:82:39: warning: pointer to a function used in arithmetic [-Wpointer-arith]
   82 |         scc.add_edge(i * W + j, link[i][j]);
      |                                       ^
a.cc:82:42: warning: pointer to a function used in arithmetic [-Wpointer-arith]
   82 |         scc.add_edge(i * W + j, link[i][j]);
      |                                          ^
a.cc:82:42: error: invalid conversion from 'int (*)(const char*, const char*) noexcept' to 'int' [-fpermissive]
   82 |         scc.add_edge(i * W + j, link[i][j]);
      |                                 ~~~~~~~~~^
      |                                          |
      |                                          int (*)(const char*, const char*) noexcept
a.cc:16:28: note:   initializing argument 2 of 'void StronglyConnectedComponents::add_edge(int, int)'
   16 |   void add_edge(int x, int y)
      |                        ~~~~^
a.cc:151:2: error: expected '}' at end of input
  151 | }
      |  ^
a.cc:68:1: note: to match this '{'
   68 | {
      | ^