Problem:
写真に写っている景色は?
A 君は会津に観光にやってきました。宿泊したホテルの窓からは会津盆地が一望できます。景色を眺めていると、床に写真の切れ端が落ちているのに気がつきました。どうやら窓から外の景色を撮ったようです。「どの辺りを撮ったのかな」と、A 君は窓の外の景色と写真が同じ大きさになるように写真をかざして、写真と窓の外の景色が一致する場所を探し始めました。
それでは、A 君がやっていることをコンピュータでやってみましょう。窓の外の景色を
n × n
マスの正方形に分割します (
n
を景色の大きさと呼びます)。各マスには、そのマスの画像の情報を表す 0 以上の整数が書かれています。マスの位置は座標 (
x, y
) で表します。各マスの座標は以下の通りになります。
写真の切れ端は、
m × m
マスの正方形で表されます (
m
を写真の切れ端の大きさと呼びます) 。この正方形の中で、切れ端の部分のマスには画像の情報が、切れ端に含まれない部分のマスには -1 が書かれています。たとえば、下図では、色が塗られている部分が実際の写真の切れ端の形を表しています。切れ端に穴が開いていることはありますが、必ず 1 つにつながっています。 (0 以上の値をもつマスが頂点だけで接しているときは、つながっていないものとみなします。) また、値 -1 のマスが縦または横 1 列に端から端まで並ぶことはありません。
窓から見える景色の中で、写真の切れ端を 0、90、180、270 度回転させたいずれかと一致する領域を探します。たとえば、景色が下図のように表わせるとき、上図の切れ端を反時計回りに90 度回転させると、色が塗られた領域に一致します。
窓から見える景色と写真の切れ端の情報を入力とし、景色の中で、切れ端を 0、90、180、270 度回転させたいずれかと一致する領域の最も上端に近いマスのうち、最も左端に近いマスの座標を出力するプログラムを作成してください。
一致する領域が複数ある場合は、それらの領域内にあるマスの中で、最も上端に近いマスのうち、最も左端に近いマスの座標を出力してください。一致する領域がないときは、NA と出力します。
Input
複数のデータセットの並びが入力として与えられます。入力の終わりはゼロふたつの行で示されます。各データセットは以下の形式で与えられます。
n
m
w
11
w
12
...
w
1n
w
21
w
22
...
w
2n
:
w
n1
w
n2
...
w
nn
p
11
p
12
...
p
1m
p
21
p
22
...
p
2m
:
p
m1
p
m2
...
p
mm
1 行目に
n, m
(1 ≤
n
≤ 100, 1 ≤
m
≤ 50,
m
≤
n
) が与えられます。
続く
n
行に窓から見える景色の情報
w
ij
(0 ≤
w
ij
≤ 15)、続く
m
行に写真の切れ端のデータの情報
p
ij
(-1 ≤
p
ij
≤ 15) が与えられます。
データセットの数は 100 を超えません。
Output
入力データセットごとに、マスの位置の座標または NA を１行に出力します。
Sample Input
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
0 0
Output for the Sample Input
4 2
NA

Submitted solutions:

code_snippet: #include <bits/stdc++.h>

using namespace std;

int w[100][100], p[50][50], tmp[50][50], n, m;

void turn() {
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < m; j++) tmp[i][j] = p[i][j];
    }
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < m; j++) p[i][j] = tmp[m - 1 - j][i];
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    #ifdef BACKER
        freopen("input.txt", "r", stdin);
        // freopen("output.txt", "w", stdout);
    #endif

    while (cin >> n >> m && n) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) cin >> w[i][j];
        }
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < m; j++) cin >> p[i][j];
        }
        pair<int, int> ans = {100, 100};
        for (int t = 0; t < 4; t++) {
            for (int i = 0; i < n - m + 1; i++) {
                for (int j = 0; j < n - m + 1; j++) {
                    bool ok = 1;
                    for (int k = 0; k < m; k++) {
                        if (!ok) break;
                        for (int l = 0; l < m; l++) {
                            if (p[k][l] != -1 && p[k][l] != w[i + k][j + l]) ok = 0;
                        }
                    }
                    if (!ok) continue;
                    // cout << "!!! " << t << " " << i + 1 << " " << j + 1 << "\n";
                    for (int k = 0; k < m; k++) {
                        for (int l = 0; l < m; l++) {
                            if (p[k][l] != -1) {
                                ans = min(ans, {i + k, j + l});
                                break;
                            }
                        }
                    }
                }
            }
            turn();
        }
        if (ans.first == 100) cout << "NA\n";
        else cout << ans.second + 1 << " " << ans.first + 1 << "\n";
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3188
final_rank: 5

code_snippet: #include <iostream>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define MAX 200
#define INFTY (1 << 21)
int n, m, T[MAX][MAX], P[MAX][MAX];
void valid(int sx, int sy, int &lx, int &ly) {
  int ax = -1, ay;
  rep(y, m) rep(x, m) {
    if (P[x][y] == -1)
      continue;
    if (P[x][y] != T[sx + x][sy + y])
      return;
    if (ax == -1) {
      ay = y;
      ax = x;
    }
  }
  if (ay < ly || (ay == ly && ax < lx)) {
    ly = sy + ay;
    lx = sx + ax;
  }
}
bool rotate(int sx, int sy) {
  int tmp[MAX][MAX];
  int ly = INFTY, lx;
  rep(r, 4) {
    valid(sx, sy, lx, ly);
    rep(y, m) rep(x, m) tmp[x][y] = P[x][y];
    rep(y, m) rep(x, m) P[y][m - x - 1] = tmp[x][y];
  }
  if (ly == INFTY)
    return false;
  cout << lx + 1 << " " << ly + 1 << endl;
  return true;
}
void compute() {
  rep(y, n - m + 1) rep(x, n - m + 1) if (rotate(x, y)) return;
  cout << "NA" << endl;
}
main() {
  while (cin >> n >> m && n) {
    rep(y, n) rep(x, n) cin >> T[x][y];
    rep(y, m) rep(x, m) cin >> P[x][y];
    compute();
  }
}
accuracy: 1
time_ms: 90
memory_kb: 3180
final_rank: 13

code_snippet: #include <cstdio>
#define rep(i, n) for (int i = 0; i < n; i++)
#define MAX 200
#define INFTY (1 << 21)
int n, m, T[MAX][MAX], P[MAX][MAX];
void valid(int sx, int sy, int &lx, int &ly) {
  int ax = -1, ay;
  rep(y, m) rep(x, m) {
    if (P[x][y] == -1)
      continue;
    if (P[x][y] != T[sx + x][sy + y])
      return;
    if (ax == -1) {
      ay = y;
      ax = x;
    }
  }
  if (ay < ly || (ay == ly && ax < lx)) {
    ly = sy + ay;
    lx = sx + ax;
  }
}
bool rotate(int sx, int sy) {
  int tmp[MAX][MAX];
  int ly = INFTY, lx;
  rep(r, 4) {
    valid(sx, sy, lx, ly);
    rep(y, m) rep(x, m) tmp[x][y] = P[x][y];
    rep(y, m) rep(x, m) P[y][m - x - 1] = tmp[x][y];
  }
  if (ly == INFTY)
    return false;
  printf("%d %d\n", lx + 1, ly + 1);
  return true;
}
void compute() {
  rep(y, n - m + 1) rep(x, n - m + 1) if (rotate(x, y)) return;
  printf("NA\n");
}
main() {
  while (scanf("%d %d", &n, &m) && n) {
    rep(y, n) rep(x, n) scanf("%d", &T[x][y]);
    rep(y, m) rep(x, m) scanf("%d", &P[x][y]);
    compute();
  }
}
accuracy: 1
time_ms: 90
memory_kb: 2784
final_rank: 3

code_snippet: #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e9;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
int main() {
  int n, m;
  while (cin >> n >> m, n) {
    vvi in(n, vi(n));
    vvi pic(m, vi(m));
    rep(i, n) rep(j, n) cin >> in[i][j];
    rep(i, m) rep(j, m) cin >> pic[i][j];
    pii out(inf, inf);
    rep(e, 4) {
      rep(i, n - m + 1) rep(j, n - m + 1) {
        bool h = true;
        pii t(inf, inf);
        rep(k, m)
            rep(l, m) if (pic[k][l] != -1 && in[i + k][j + l] != pic[k][l]) h =
                false;
        else if (pic[k][l] != -1) t = min(t, pii(i + k, j + l));
        if (h) {
          out = min(out, t);
        }
      }
      vvi tmp = pic;
      rep(i, m) rep(j, m) pic[i][j] = tmp[j][m - i - 1];
    }
    if (out.first == inf)
      cout << "NA" << endl;
    else
      cout << out.second + 1 << " " << out.first + 1 << endl;
  }
}
accuracy: 1
time_ms: 260
memory_kb: 3204
final_rank: 19

code_snippet: #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e9;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
int main() {
  int n, m;
  while (cin >> n >> m, n) {
    vvi in(n, vi(n));
    vvi pic(m, vi(m));
    rep(i, n) rep(j, n) cin >> in[i][j];
    rep(i, m) rep(j, m) cin >> pic[i][j];
    pii out(inf, inf);
    rep(e, 4) {
      rep(i, n - m + 1) rep(j, n - m + 1) {
        pii t(inf, inf);
        rep(k, m) rep(l, m) if (pic[k][l] != -1 &&
                                in[i + k][j + l] != pic[k][l]) goto en;
        else if (pic[k][l] != -1) t = min(t, pii(i + k, j + l));
        out = min(out, t);
        goto end;
      en:;
      }
    end:;
      vvi tmp = pic;
      rep(i, m) rep(j, m) pic[i][j] = tmp[j][m - i - 1];
    }
    if (out.first == inf)
      cout << "NA" << endl;
    else
      cout << out.second + 1 << " " << out.first + 1 << endl;
  }
}
accuracy: 1
time_ms: 30
memory_kb: 3204
final_rank: 7

code_snippet: #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e9;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
int main() {
  int n, m;
  while (scanf("%d %d", &n, &m), n) {
    vvi in(n, vi(n));
    vvi pic(m, vi(m));
    rep(i, n) rep(j, n) scanf("%d", &in[i][j]);
    rep(i, m) rep(j, m) scanf("%d", &pic[i][j]);
    pii out(inf, inf);
    rep(e, 4) {
      rep(i, n - m + 1) rep(j, n - m + 1) {
        pii t(inf, inf);
        rep(k, m) rep(l, m) if (pic[k][l] != -1 &&
                                in[i + k][j + l] != pic[k][l]) goto en;
        else if (pic[k][l] != -1) t = min(t, pii(i + k, j + l));
        out = min(out, t);
        goto end;
      en:;
      }
    end:;
      vvi tmp = pic;
      rep(i, m) rep(j, m) pic[i][j] = tmp[j][m - i - 1];
    }
    if (out.first == inf)
      cout << "NA" << endl;
    else
      cout << out.second + 1 << " " << out.first + 1 << endl;
  }
}
accuracy: 1
time_ms: 10
memory_kb: 3264
final_rank: 6

code_snippet: #include <algorithm>
#include <iostream>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
int pic[50][50];
int all[100][100];
int n, m;
void rotate() {
  rep(i, 0, m) rep(j, 0, i) swap(pic[i][j], pic[j][i]);
  rep(i, 0, m) reverse(pic[i], pic[i] + m);
}
int main() {
  bool flag = 0;
  int X, Y, f_x, f_y, first;
  while (cin >> n >> m && n && m) {
    X = Y = (1 << 21);
    rep(i, 0, n) rep(j, 0, n) { cin >> all[i][j]; }
    rep(i, 0, m) rep(j, 0, m) { cin >> pic[i][j]; }
    rep(y, 0, n - m + 1) {
      rep(x, 0, n - m + 1) {
        rep(b, 0, 4) {
          first = flag = true;
          rep(i, 0, m) rep(j, 0, m) {
            if (pic[i][j] == -1)
              continue;
            if (pic[i][j] != all[y + i][x + j]) {
              flag = false;
            } else if (first) {
              f_y = y + i;
              f_x = x + j;
              first = false;
            }
          }
          if (flag) {
            if (f_y < Y) {
              Y = f_y;
              X = f_x;
            } else if (f_y == Y) {
              if (f_x < X) {
                X = f_x;
              }
            }
          }
          rotate();
        }
      }
    }
    if (X == (1 << 21))
      cout << "NA" << endl;
    else
      cout << X + 1 << " " << Y + 1 << endl;
  }
}
accuracy: 1
time_ms: 270
memory_kb: 3132
final_rank: 18

code_snippet: #include <iostream>
using namespace std;
int f[100][100];
int e[100][100];
int main() {
  int n, m;
  while (cin >> n >> m && n) {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        cin >> f[i][j];
    for (int i = 0; i < m; i++)
      for (int j = 0; j < m; j++)
        cin >> e[i][j];
    pair<int, int> ans(1e7, 1e7);
    for (int r = 0; r < 4; r++) {
      for (int i = 0; i < n - m + 1; i++) {
        for (int j = 0; j < n - m + 1; j++) {
          int flag = 1;
          int X = -1, Y;
          for (int k = 0; k < m; k++) {
            for (int l = 0; l < m; l++) {
              if (f[i + k][j + l] != e[k][l] && e[k][l] != -1) {
                flag = false;
              }
              if (e[k][l] != -1 && X == -1) {
                Y = i + k;
                X = j + l;
              }
            }
          }
          if (flag)
            ans = min(ans, make_pair(Y, X));
        }
      }
      for (int i = 0; i < m; i++)
        for (int j = i + 1; j < m; j++)
          swap(e[i][j], e[j][i]);
      for (int i = 0; i < m; i++)
        for (int j = 0; j < m / 2; j++)
          swap(e[i][j], e[i][m - j - 1]);
    }
    if (ans.first > 1e6)
      cout << "NA" << endl;
    else
      cout << ans.second + 1 << " " << ans.first + 1 << endl;
  }
}
accuracy: 1
time_ms: 220
memory_kb: 3132
final_rank: 15

code_snippet: #include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define reep(i, f, t) for (int i = f; i < int(t); ++i)
#define rep(i, n) reep(i, 0, n)
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
pii fit(const vvi &window, const vvi &pic, int y, int x) {
  int m = pic.size();
  pii ans(-1, -1);
  rep(i, m) rep(j, m) {
    if (pic[i][j] == -1)
      continue;
    if (window[y + i][x + j] != pic[i][j]) {
      return pii(-1, -1);
    } else if (ans.first == -1)
      ans = pii(y + i, x + j);
  }
  return ans;
}
void rotate(vvi &p) {
  int m = p.size();
  vvi next(m, vi(m));
  rep(i, m) rep(j, m) next[i][j] = p[m - j - 1][i];
  p.swap(next);
}
int main() {
  int n, m;
  while (scanf("%d%d", &n, &m), n) {
    vvi window(n, vi(n));
    rep(i, n) rep(j, n) scanf("%d", &window[i][j]);
    vvi pic(m, vi(m));
    rep(i, m) rep(j, m) scanf("%d", &pic[i][j]);
    pii ans(-1, -1);
    rep(i, n - m + 1) rep(j, n - m + 1) {
      rep(k, 4) {
        ans = fit(window, pic, i, j);
        if (ans.first != -1) {
          i = n - m + 1;
          j = n - m + 1;
          break;
        }
        rotate(pic);
      }
    }
    if (ans.first == -1)
      puts("NA");
    else
      printf("%d %d\n", ans.second + 1, ans.first + 1);
  }
  return 0;
}
accuracy: 1
time_ms: 170
memory_kb: 2656
final_rank: 8

code_snippet: #include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define reep(i, f, t) for (int i = f; i < int(t); ++i)
#define rep(i, n) reep(i, 0, n)
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
int m;
pii fit(const int window[][100], const int pic[][50], int y, int x) {
  pii ans(-1, -1);
  rep(i, m) rep(j, m) {
    if (pic[i][j] == -1)
      continue;
    if (window[y + i][x + j] != pic[i][j]) {
      return pii(-1, -1);
    } else if (ans.first == -1)
      ans = pii(y + i, x + j);
  }
  return ans;
}
void rotate(int p[][50]) {
  int next[50][50];
  rep(i, m) rep(j, m) next[i][j] = p[m - j - 1][i];
  rep(i, m) rep(j, m) p[i][j] = next[i][j];
}
int main() {
  int n;
  while (scanf("%d%d", &n, &m), n) {
    int window[100][100];
    rep(i, n) rep(j, n) scanf("%d", &window[i][j]);
    int pic[50][50];
    rep(i, m) rep(j, m) scanf("%d", &pic[i][j]);
    pii ans(-1, -1);
    rep(i, n - m + 1) rep(j, n - m + 1) {
      rep(k, 4) {
        ans = fit(window, pic, i, j);
        if (ans.first != -1) {
          i = n - m + 1;
          j = n - m + 1;
          break;
        }
        rotate(pic);
      }
    }
    if (ans.first == -1)
      puts("NA");
    else
      printf("%d %d\n", ans.second + 1, ans.first + 1);
  }
  return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 2748
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, m;
    auto rotate90 = [&](vector<vector<int>> A){
        int n = A.size();
        vector<vector<int>> B(n, vector<int>(n));
        for(int y = 0; y < n; y++){
            for(int x = 0; x < n; x++){
                B[n - 1 - x][y] = A[y][x];
            }
        }
        return B;
    };
    while(cin >> n >> m, n){
        vector<vector<int>> A(n, vector<int>(n)), B(m, vector<int>(m));
        for(int y = 0; y < n; y++){
            for(int x = 0; x < n; x++){
                cin >> A[y][x];
            }
        }
        for(int y = 0; y < m; y++){
            for(int x = 0; x < m; x++){
                cin >> B[y][x];
            }
        }
        pair<int,int> ans = {n + 1, n + 1};
        for(int i = 0; i < 4; i++){
            for(int y = 0; y + m <= n; y++){
                for(int x = 0; x + m <= n; x++){
                    bool flag = true;
                    pair<int,int> p = {n + 1, n + 1};
                    for(int cy = 0; cy < m; cy++){
                        for(int cx = 0; cx < m; cx++){
                            if(B[cy][cx] == -1)continue;
                            if(B[cy][cx] != A[y + cy][x + cx]){
                                flag = false;
                                break;
                            }
                            p = min(p, make_pair(y + cy + 1, x + cx + 1));
                        }
                    }
                    if(flag){
                        ans = min(ans, p);
                    }
                }
            }
            B = rotate90(B);
        }
        if(ans.first == n + 1)cout << "NA" << '\n';
        else cout << ans.second << " " << ans.first << '\n';
    }
}
accuracy: 1
time_ms: 60
memory_kb: 3300
final_rank: 12

code_snippet: #include "bits/stdc++.h"

#define REP(i,num) for(ll i=0;i<(num);++i)
#define FOR(i,c,num) for(ll (i)=(c);(i)<(num);++(i))
#define LOOP(i) while(i--)
#define ALL(c) c.begin(),c.end()
#define PRINTALL(c) for(auto pitr=c.begin();pitr!=c.end();++pitr){cout<<*pitr;if(next(pitr,1)!=c.end())cout<<' ';}cout<<endl;
#define PAIRCOMP(c,comp) [](const pair<ll,ll>& lhs,const pair<ll,ll>& rhs){return lhs.c comp rhs.c;}

using namespace std;
using ll = long long;
using ull = unsigned long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;

constexpr ll atcoder_mod = 1e9+7;

template<typename T=ll>
T in(){ T x; cin >> x; return (x); }
template<typename T=ll,typename C=vector<T>>
C vecin(int N){ C x(N);REP(i,N){ x[i]=in<T>(); }return x; }
template<typename T=ll,size_t N>
array<T,N> arrin(){array<T,N> x;REP(i,N){ x[i]=in<T>(); }return x;}

void vout(){ cout << endl; }
template<typename Head,typename... Tail>
void vout(Head&& h,Tail&&... t){ cout << ' ' << h;vout(forward<Tail>(t)...); }
void out(){ cout << endl; }
template<typename Head,typename... Tail>
void out(Head&& h,Tail&&... t){ cout << h;vout(forward<Tail>(t)...); }

template<typename T>
bool chmax(T& a,T b){ if(a<b){ a=b;return true; }return false; }
template<typename T>
bool chmin(T& a,T b){ if(a>b){ a=b;return true; }return false; }

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);

	while(true){
		auto N=in(),M=in();
		if(!N && !M) break;

		vvll W(N,vll(N));
		vvll P[4];
		ll X[4],Y[4];
		REP(i,N){
			REP(j,N){
				W[i][j]=in();
			}
		}

		REP(i,4){
			P[i].resize(M,vll(M));
		}
		REP(i,M){
			REP(j,M){
				P[0][i][j]=in();
			}
		}
		REP(i,M){
			REP(j,M){
				P[1][i][j]=P[0][M-1-j][i];
				P[2][i][j]=P[0][M-1-i][M-1-j];
				P[3][i][j]=P[0][j][M-1-i];
			}
		}
		REP(k,4){
			X[k]=-1,Y[k]=-1;
			REP(i,M){
				REP(j,M){
					if(P[k][i][j]!=-1){
						X[k]=j;
						break;
					}
				}
				if(X[k]!=-1){
					Y[k]=i;
					break;
				}
			}
		}
		bool finish = false;
		REP(i,N-M+1){
			REP(j,N-M+1){
				REP(k,4){
					bool flag = true;
					REP(v,M){
						REP(u,M){
							if(P[k][v][u]!=-1 && W[i+v][j+u]!=P[k][v][u]){
								flag &= false;
							}
						}
					}
					if(flag){
						out(j+X[k]+1,i+Y[k]+1);
						finish = true;
						break;
					}
				}
				if(finish) break;
			}
			if(finish) break;
		}
		if(!finish) out("NA");
	}
	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3652
final_rank: 14

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

template<typename T>
vector<vector<T>> rotate_2dvec(vector<vector<T>> &v){
    int n = v.size();
    int m = v[0].size();
    vector<vector<T>> res(m, vector<T>(n));
    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++){
            res[m-j-1][i] = v[i][j];
        }
    }
    return res;
}

int main(){
    while(1){
        int n,m;
        cin >> n >> m;
        if(n == 0) break;

        vector<vector<int>> a(n, vector<int>(n));
        for(int i=0; i<n; i++){
            for(int j=0; j<n; j++){
                cin >> a[i][j];
            }
        }
        vector<vector<int>> b(m, vector<int>(m));
        for(int i=0; i<m; i++){
            for(int j=0; j<m; j++){
                cin >> b[i][j];
            }
        }
        int y=n,x=n;
        for(int i=0; i<n-m+1; i++){
            for(int j=0; j<n-m+1; j++){
                for(int d=0; d<4; d++){
                    bool ok = true;
                    int ty=-1,tx=-1;
                    for(int k=0; k<m; k++){
                        for(int l=0; l<m; l++){
                            if(b[k][l]!=-1 and ty==-1){
                                ty = i+k;
                                tx = j+l;
                            }
                            if(b[k][l]!=-1 and a[i+k][j+l]!=b[k][l]){
                                ok = false;
                                break;
                            }
                        }
                    }
                    if(ok){
                        if(ty < y){
                            y = ty;
                            x = tx;
                        }else if(ty == y){
                            x = min(x, tx);
                        }
                    }
                    b = rotate_2dvec<int>(b);
                }
            }
        }
        if(y == n){
            cout << "NA" << endl;
        }else{
            cout << x+1 << " " << y+1 << endl;
        }
    }
    return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 3316
final_rank: 17

code_snippet: #include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

template <class T, class U>
vector<T> make_v(U size, const T& init){ return vector<T>(static_cast<size_t>(size), init); }

template<class... Ts, class U>
auto make_v(U size, Ts... rest) { return vector<decltype(make_v(rest...))>(static_cast<size_t>(size), make_v(rest...)); }

template<class T> void chmin(T &a, const T &b){ a = (a < b ? a : b); }
template<class T> void chmax(T &a, const T &b){ a = (a > b ? a : b); }

void solve(int n, int m){
    auto A = make_v(n, n, 0);
    auto B = make_v(m, m, 0);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            cin >> A[i][j];
        }
    }
    for (int i = 0; i < m; ++i) {
        for (int j = 0; j < m; ++j) {
            cin >> B[i][j];
        }
    }
    vector<pair<int, int>> ans;
    for (int k = 0; k < 4; ++k) {
        for (int i = 0; i+m <= n; ++i) {
            for (int j = 0; j+m <= n; ++j) {
                int ok = 1;
                for (int y = 0; y < m; ++y) {
                    for (int x = 0; x < m; ++x) {
                        if(~B[y][x] && A[i+y][j+x] != B[y][x]){
                            ok = 0;
                            y = x = m;
                        }
                    }
                }
                if(ok){
                    for (int y = 0; y < m; ++y) {
                        for (int x = 0; x < m; ++x) {
                            if(~B[y][x]){
                                ans.emplace_back(y+i, x+j);
                                i = j = x = y = n;
                            }
                        }
                    }
                }
            }
        }
        auto BB = B;
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < m; ++j) {
                B[i][j] = BB[m-j-1][i];
            }
        }
    }
    if(ans.empty()){
        puts("NA");
    }else {
        sort(ans.begin(),ans.end());
        printf("%d %d\n", ans[0].second+1, ans[0].first+1);
    }
}

int main() {
    int n, m;
    while(cin >> n >> m, n){
        solve(n, m);
    }
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3328
final_rank: 9

code_snippet: #include<iostream>
#include<algorithm>
#include <map>
#include <vector>
#include <stack>
#include<string>
#include<queue>
#include <regex>
#include<iomanip>

using namespace std;

int w[101][101];
int p[51][51];

void Rotate(int p[51][51],int m)
{
	int temp[51][51];
	for (int i = 1; i <= m; i++)
		for (int j = 1; j <= m; j++)
			temp[m + 1 - j][i] = p[i][j];
	for (int i = 1; i <= m; i++)
		for (int j = 1; j <= m; j++)
			p[j][i] = temp[j][i];
}
bool Compare(int x, int y, int m)
{
	for (size_t i = 1; i <= m; i++)
	{
		for (size_t j = 1; j <= m; j++)
		{
			if (p[j][i] != -1 && p[j][i] != w[x + j - 1][y + i - 1])
				return false;
		}
	}
	return true;
}
pair<int,int> Find(int w[101][101],int n,int p[51][51],int m)
{
	pair<int, int> pos = make_pair(0, 0);
	for (size_t i = 1; i <= n - m + 1; i++)
	{
		for (size_t j = 1; j <= n - m + 1; j++)
		{
			for (size_t k = 0; k < 4; k++)
			{
				if (Compare(j, i, m))
				{
					for (size_t k = 1; k <= m; k++)
						if (p[k][1] != -1)
						{
							int x = j + k - 1;
							int y = i;
							if (pos.first == 0 || (pos.first > x))
							{
								pos = make_pair(x, y);
							}
						}
					return pos;
				}
				Rotate(p, m);
			}
		}
	}
	return make_pair(0, 0);
}

int main()
{
	int n,m;
	cin >> n >> m;
	while (n && m)
	{
		for (size_t i = 1; i <= n; i++)
			for (size_t j = 1; j <= n; j++)
				cin >> w[j][i];
		for (size_t i = 1; i <= m; i++)
			for (size_t j = 1; j <= m; j++)
				cin >> p[j][i];
		auto pos = Find(w, n, p, m);
		if (pos.first != 0)
			cout << pos.first << " " << pos.second << endl;
		else
			cout << "NA" << endl;
		cin >> n >> m;
	}
}
accuracy: 1
time_ms: 120
memory_kb: 3044
final_rank: 11

code_snippet: #include <iostream>
#include <algorithm>
using namespace std;
#define INF 100000

struct point{
  int x;
  int y;
};

void find(int** w,int** p, int n, int m, point* retval);

bool match(int i, int j, int** w, int** p, int m);

int main(void){
  int n,m;
  while(true){
    cin>>n>>m;
    if(n==0 and m==0){break;}
    int** w=(int**)malloc(sizeof(int*)*n);
    for(int i=0;i<n;i++){
      w[i]=(int*)malloc(sizeof(int)*n);
      for(int j=0;j<n;j++){
        cin>>w[i][j];
      }
    }
    int** p=(int**)malloc(sizeof(int*)*m);
    for(int i=0;i<m;i++){
      p[i]=(int*)malloc(sizeof(int)*m);
      for(int j=0;j<m;j++){
        cin>>p[i][j];
      }
    }
    point retval;
    find(w,p,n,m,&retval);
    if(retval.x==INF and retval.y==INF){
      cout<<"NA"<<endl;
    }
    else{
      cout<<retval.x+1<<" "<<retval.y+1<<endl;
    }
  }
}

void find(int** w,int** p, int n, int m, point* retval){
  retval->x=INF;
  retval->y=INF;

  int** p1=p;
  int** p2=(int**)malloc(sizeof(int*)*m);
  for(int i=0;i<m;i++){
    p2[i]=(int*)malloc(sizeof(int)*m);
    for(int j=0;j<m;j++){
      p2[i][j]=p1[m-1-j][i];
    }
  }
  int** p3=(int**)malloc(sizeof(int*)*m);
  for(int i=0;i<m;i++){
    p3[i]=(int*)malloc(sizeof(int)*m);
    for(int j=0;j<m;j++){
      p3[i][j]=p2[m-1-j][i];
    }
  }

  int** p4=(int**)malloc(sizeof(int*)*m);
  for(int i=0;i<m;i++){
    p4[i]=(int*)malloc(sizeof(int)*m);
    for(int j=0;j<m;j++){
      p4[i][j]=p3[m-1-j][i];
    }
  }

  for(int i=0;i<=n-m;i++){
    for(int j=0;j<=n-m;j++){
      if(match(i,j,w,p1,m)){
        for(int _y=0;_y<m;_y++){
          for(int _x=0;_x<m;_x++){
            if(p1[_y][_x]!=-1){
              int y=_y+i;
              int x=_x+j;
              if(retval->y>y or (retval->y==y and retval->x>x)){
                retval->y=y;
                retval->x=x;
              }
            }
          }
        }
      }
      if(match(i,j,w,p2,m)){
        for(int _y=0;_y<m;_y++){
          for(int _x=0;_x<m;_x++){
            if(p2[_y][_x]!=-1){
              int y=_y+i;
              int x=_x+j;
              if(retval->y>y or (retval->y==y and retval->x>x)){
                retval->y=y;
                retval->x=x;
              }
            }
          }
        }
      }
      if(match(i,j,w,p3,m)){
        for(int _y=0;_y<m;_y++){
          for(int _x=0;_x<m;_x++){
            if(p3[_y][_x]!=-1){
              int y=_y+i;
              int x=_x+j;
              if(retval->y>y or (retval->y==y and retval->x>x)){
                retval->y=y;
                retval->x=x;
              }
            }
          }
        }
      }
      if(match(i,j,w,p4,m)){
        for(int _y=0;_y<m;_y++){
          for(int _x=0;_x<m;_x++){
            if(p4[_y][_x]!=-1){
              int y=_y+i;
              int x=_x+j;
              if(retval->y>y or (retval->y==y and retval->x>x)){
                retval->y=y;
                retval->x=x;
              }
            }
          }
        }
      }
    }
  }
}

bool match(int i, int j, int** w, int** p, int m){
  for(int y=0;y<m;y++){
    for(int x=0;x<m;x++){
      if(p[y][x]!=-1 and p[y][x]!=w[y+i][x+j]){return false;}
    }
  }
  return true;
}
accuracy: 1
time_ms: 20
memory_kb: 4016
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;

int n, m;
int w[110][110];
int p[60][60];
int tmp[60][60];
int ans_x, ans_y;

void rotate() {
    for (int i=0; i<m; i++) {
        for (int j=0; j<m; j++) {
            tmp[j][m-1-i] = p[i][j];
        }
    }
    
    for (int i=0; i<m; i++) {
        for (int j=0; j<m; j++) {
            p[i][j] = tmp[i][j];
        }
    }
}

void check() {
    for (int i=0; i<=n-m; i++) {
        for (int j=0; j<=n-m; j++) {
            bool flag = true;
            
            for (int k=0; k<m; k++) {
                for (int l=0; l<m; l++) {
                    if (p[k][l]==-1) {
                        continue;
                    }
                    
                    if (p[k][l]!=w[i+k][j+l]) {
                        flag = false;
                    }
                }
            }
            
            if (flag) {
                for (int k=0; k<m; k++) {
                    for (int l=0; l<m; l++) {
                        if (p[k][l]!=-1) {
                            if (ans_x>i+k) {
                                ans_x = i+k, ans_y = j+l;
                            }
                            else if (ans_x==i+k && ans_y>j+l) {
                                ans_y = j+l;
                            }
                        }
                    }
                }
            }
        }
    }
}

int main() {
    while (true) {
        cin >> n >> m;
        
        if (n==0 && m==0) {
            break;
        }
        
        for (int i=0; i<n; i++) for (int j=0; j<n; j++) cin >> w[i][j];
        for (int i=0; i<m; i++) for (int j=0; j<m; j++) cin >> p[i][j];
        
        ans_x = 1000, ans_y = 1000;
        
        for (int i=0; i<4; i++) {
            check();
            rotate();
        }
        
        if (ans_x==1000) {
            cout << "NA" << endl;
        }
        else {
            cout << ans_y+1 << " " << ans_x+1 << endl;
        }
    }
}
accuracy: 1
time_ms: 80
memory_kb: 3176
final_rank: 10

code_snippet: #include<iostream>
using namespace std;
int N,M;
int W[100][100],p[50][50];
int get(int i,int j,int r)
{
	return r==0?p[i][j]:r==1?p[j][M-i-1]:r==2?p[M-i-1][M-j-1]:p[M-j-1][i];
}
main()
{
	while(cin>>N>>M,N)
	{
		for(int i=0;i<N;i++)for(int j=0;j<N;j++)cin>>W[i][j];
		for(int i=0;i<M;i++)for(int j=0;j<M;j++)cin>>p[i][j];
		int ansx=N,ansy=N;
		for(int i=0;i<=N-M;i++)for(int j=0;j<=N-M;j++)
		{
			for(int r=0;r<4;r++)
			{
				bool now=true;
				int x=-1,y=-1;
				for(int I=0;now&&I<M;I++)for(int J=0;J<M;J++)
				{
					int a=W[i+I][j+J],b=get(I,J,r);
					if(b>=0&&b!=a)
					{
						now=false;
						break;
					}
					if(b>=0&&x<0)x=i+I,y=j+J;
				}
				if(now)
				{
					if(ansx>x||ansx==x&&ansy>y)ansx=x,ansy=y;
				}
			}
		}
		if(ansx==N)cout<<"NA"<<endl;
		else cout<<ansy+1<<" "<<ansx+1<<endl;
	}
}
accuracy: 1
time_ms: 30
memory_kb: 3024
final_rank: 2

code_snippet: #include<iostream>
#include<cstring>
using namespace std;

int n, m;
int scene[100][100], pict[50][50];
int x, y;

bool check0(int si, int sj){
    bool first = true;
    for(int i = 0; i < m; i++){
        for(int j = 0; j < m; j++){
            if(pict[i][j] == -1)    continue;

            if(pict[i][j] == scene[si+i][sj+j]){
                if(first){
                    first = false;
                    x = sj+j; y = si+i;
                }
            }else{
                return false;
            }
        }
    }
    return true;
}

bool check90(int si, int sj){
    bool first = true;
    for(int i = 0; i < m; i++){
        for(int j = 0; j < m; j++){
            if(pict[m-1-j][i] == -1)    continue;

            if(pict[m-1-j][i] == scene[si+i][sj+j]){
                if(first){
                    first = false;
                    x = sj+j; y = si+i;
                }
            }else{
                return false;
            }
        }
    }
    return true;
}

bool check180(int si, int sj){
    bool first = true;
    for(int i = 0; i < m; i++){
        for(int j = 0; j < m; j++){
            if(pict[m-1-i][m-1-j] == -1)    continue;

            if(pict[m-1-i][m-1-j] == scene[si+i][sj+j]){
                if(first){
                    first = false;
                    x = sj+j; y = si+i;
                }
            }else{
                return false;
            }
        }
    }
    return true;
}

bool check270(int si, int sj){
    bool first = true;
    for(int i = 0; i < m; i++){
        for(int j = 0; j < m; j++){
            if(pict[j][m-1-i] == -1)    continue;

            if(pict[j][m-1-i] == scene[si+i][sj+j]){
                if(first){
                    first = false;
                    x = sj+j; y = si+i;
                }
            }else{
                return false;
            }
        }
    }
    return true;
}

int main(){
    while(1){
        cin >> n >> m;
        if(n+m == 0)    break;

        memset(scene, -1, sizeof(scene));
        memset(pict, -1, sizeof(pict));

        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                cin >> scene[i][j];
            }
        }

        for(int i = 0; i < m; i++){
            for(int j = 0; j < m; j++){
                cin >> pict[i][j];
            }
        }

        bool cont = true;
        
        // for(int i = 0; i < m; i++){
        //     for(int j = 0; j < m; j++){
        //         cout << pict[m-1-i][m-1-j] << " ";
        //     }
        //     cout << endl;
        // }

        for(int i = 0; i <= n-m && cont; i++){
            for(int j = 0; j <= n-m && cont; j++){
                if(check0(i,j)){
                    cout << x+1 << " " << y+1 << endl;
                    cont = false;
                    break;
                }
                if(check90(i,j)){
                    cout << x+1 << " " << y+1 << endl;
                    cont = false;
                    break;
                }
                if(check180(i,j)){
                    cout << x+1 << " " << y+1 << endl;
                    cont = false;
                    break;
                }
                if(check270(i,j)){
                    cout << x+1 << " " << y+1 << endl;
                    cont = false;
                    break;
                }
            }
        }

        if(cont)    cout << "NA" << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3140
final_rank: 4

code_snippet: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;

vector<vector<int>>rot(vector<vector<int>>v) {
	vector<vector<int>>rotated(v);
	for (int i = 0; i < v.size(); ++i) {
		for (int j = 0; j < v.size(); ++j) {
			rotated[i][j]=v[v.size()-j-1][i];
		}
	}
	return rotated;
}

pair<int,int> check(vector<vector<int>>&f, vector<vector<int>>&p, int l, int u) {
	pair<int,int>ans(10000,10000);;
	for (int x = l; x < l + p.size(); ++x) {
		for (int y = u; y < u + p.size(); ++y) {
			if (p[y - u][x - l] != -1) {
				ans=min(ans,make_pair(y,x));
				if (f[y][x] != p[y - u][x - l])return make_pair(10000, 10000);

			}
		}
	}
	return ans;
}
int main() {
	while (true) {
		int N,M;cin>>N>>M;
		if(!N)break;
		vector<vector<int>>field(N,vector<int>(N));
		for (int i = 0; i < N; ++i) {
			for (int j = 0; j < N; ++j) {
				cin>>field[i][j];
			}
		}
		vector<vector<int>>picture(M,vector<int>(M));
		for (int i = 0; i < M; ++i) {
			for (int j = 0; j < M; ++j) {
				cin >> picture[i][j];
			}
		}
		pair<int,int>ans(10000,10000);
		for (int l = 0; l <= N - M; ++l) {
			for (int u = 0; u <= N - M; ++u) {
				for (int k = 0; k < 4; ++k) {
					picture=rot(picture);
					ans=min(ans,check(field,picture,l,u));
				}
			}
		}
		if (ans.first == 10000) {
			cout<<"NA"<<endl;
		}
		else {
			cout<<ans.second+1<<" "<<ans.first+1<<endl;
		}
	}
	return 0;
}
accuracy: 1
time_ms: 340
memory_kb: 3140
final_rank: 20