Problem p00209 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
#define F first
#define S second
using namespace std;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef pair<int, int> P;

void Input(VVI &G, int n) {
  for (int i = 0; i < n; i++) {
    G[i].resize(n);
    for (int j = 0; j < n; j++) {
      cin >> G[i][j];
    }
  }
}

VVI rotate90(VVI G) {
  int n = G.size();

  VVI ret;
  ret.resize(n);

  for (int i = 0; i < n; i++) {
    ret[i].resize(n);
    for (int j = 0; j < n; j++)
      ret[i][j] = G[j][n - i - 1];
  }

  return ret;
}

bool check(VVI &G, VVI &G2, P p) {

  for (int i = p.S; i < p.S + G2.size(); i++) {
    for (int j = p.F; j < p.F + G2.size(); j++) {
      // cout << G[i][j] <<  " ";
      if (G2[i - p.S][j - p.F] == -1)
        continue;
      else if (!(i < G.size() && j < G.size()))
        return false;
      else if (G[i][j] != G2[i - p.S][j - p.F])
        return false;
    }
    // cout << endl;
  }
  return true;
}

void print(VVI &G) {
  for (int i = 0; i < G.size(); i++) {
    for (int j = 0; j < G[i].size(); j++)
      cout << G[i][j] << " ";
    cout << endl;
  }
  cout << endl;
}

int main() {
  int n, m;
  while (cin >> n >> m, n | m) {

    VVI G, G2;
    bool Fin = false;
    G.resize(n);
    G2.resize(m);
    Input(G, n);
    Input(G2, m);
    vector<P> vec;

    for (int x = 0; x < 4; x++) {

      for (int i = 0; i < G.size(); i++) {
        for (int j = 0; j < G.size(); j++) {
          // cout << j <<  " - " << i << " th" << endl;
          if (check(G, G2, P(j, i))) {
            Fin = true;
            bool f = false;
            for (int k = 0; k < G2.size(); k++) {
              for (int l = 0; l < G2.size(); l++) {
                if (G2[k][l] != -1) {
                  vec.push_back(P(i + k + 1, j + l + 1));
                  f = true;
                  break;
                }
              }
              if (f)
                break;
            }
          }
          // cout << endl;
        }
      }

      G2 = rotate90(G2);
    }

    sort(vec.begin(), vec.end());

    if (!vec.size())
      cout << "NA" << endl;
    else
      cout << vec[0].S << " " << vec[0].F << endl;
  }
  return 0;
}

Error: 0
8 4
2 1 3 1 1 5 1 3 
2 3 2 4 1 0 2 1 
0 3 1 2 1 1 4 2 
1 2 3 2 1 1 5 4 
0 2 0 1 1 3 2 1 
1 3 1 2 2 4 3 2 
5 1 2 1 4 1 1 5 
4 1 1 0 1 2 2 1 
2 -1 -1 -1 
0 3 -1 -1 
-1 2 2 4 
-1 1 -1 1 
5 3
1 0 2 3 5 
2 3 7 2 1 
2 5 4 2 2 
8 9 0 3 3 
3 6 0 4 7 
-1 -1 2 
-1 3 5 
0 4 -1 


Fixed submission:

#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <vector>
#define F first
#define S second
using namespace std;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef pair<int, int> P;

void Input(VVI &G, int n) {
  for (int i = 0; i < n; i++) {
    G[i].resize(n);
    for (int j = 0; j < n; j++) {
      cin >> G[i][j];
      cerr << G[i][j] << " ";
    }
    cerr << endl;
  }
}

VVI rotate90(VVI G) {
  int n = G.size();

  VVI ret;
  ret.resize(n);

  for (int i = 0; i < n; i++) {
    ret[i].resize(n);
    for (int j = 0; j < n; j++)
      ret[i][j] = G[j][n - i - 1];
  }

  return ret;
}

bool check(VVI &G, VVI &G2, P p) {

  for (int i = p.S; i < p.S + G2.size(); i++) {
    for (int j = p.F; j < p.F + G2.size(); j++) {
      // cout << G[i][j] <<  " ";
      if (G2[i - p.S][j - p.F] == -1)
        continue;
      else if (!(i < G.size() && j < G.size()))
        return false;
      else if (G[i][j] != G2[i - p.S][j - p.F])
        return false;
    }
    // cout << endl;
  }
  return true;
}

void print(VVI &G) {
  for (int i = 0; i < G.size(); i++) {
    for (int j = 0; j < G[i].size(); j++)
      cout << G[i][j] << " ";
    cout << endl;
  }
  cout << endl;
}

int main() {
  int n, m;
  while (cin >> n >> m, n | m) {
    cerr << n << " " << m << endl;
    VVI G, G2;
    bool Fin = false;
    G.resize(n);
    G2.resize(m);
    Input(G, n);
    Input(G2, m);

    for (int x = 0; x < 4; x++) {

      for (int i = 0; i < G.size(); i++) {
        for (int j = 0; j < G.size(); j++) {
          // cout << j <<  " - " << i << " th" << endl;
          if (check(G, G2, P(j, i))) {
            Fin = true;
            for (int k = 0; k < G2.size(); k++)
              for (int l = 0; l < G2.size(); l++)
                if (G2[k][l] != -1) {
                  cout << j + l + 1 << " " << i + k + 1 << endl;
                  goto F;
                }

            cout << j + 1 << " " << i + 1 << endl;
            goto F;
          }
          // cout << endl;
        }
      }

      G2 = rotate90(G2);
    }

  F:
    if (!Fin)
      cout << "NA" << endl;
  }
  return 0;
}