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

vector<int> getVisionPattern1d() {
    return vector<int>{1, 3, 1, 1, 1, 2};
}
int getMove1d(vector<int> v) {
    if (v.size() == 3) {
        for (int i = 0; i < (int) v.size(); ++i) {
            if (v[i] != 1) {
                return i;
            }
        }
        return 0;
    } else if (v.size() == 5) {
        for (int i = 0; i < (int) v.size(); ++i) {
            if (v[i] == 3) {
                return i;
            }
        }
    } else {
        if (v[1] == 2) {
            return 6;
        } else {
            return 0;
        }
    }
}

vector<vector<int>> visvis;

std::vector<std::vector<int>> getVisionPattern2d() {
    #ifdef LOCAL
    int n, m;
    cin >> n >> m;
    visvis.resize(n, vector<int>(m));
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            cin >> visvis[i][j];
        }
    }
    return visvis;
    #endif
    return vector<vector<int>>{
        {1, 1, 1, 1},
        {1, 1, 2, 1},
        {1, 1, 2, 2},
        {1, 3, 1, 1}
    };
}
std::pair<int, int> getMove2d(std::vector<std::vector<int>> v) {
    #ifdef LOCAL
    vector<vector<int>> vis = visvis;
    #else
    vector<vector<int>> vis = {
        {1, 1, 1, 1},
        {1, 1, 2, 1},
        {1, 1, 2, 2},
        {1, 3, 1, 1}
    };
    #endif
    int C = vis.size();
    auto getCell = [&](int i, int j) {
        return vis[(i % C + C) % C][(j % C + C) % C];
    };
    auto revLines = [&](vector<vector<int>> v) {
        reverse(v.begin(), v.end());
        return v;
    };
    auto revCols = [&](vector<vector<int>> v) {
        for (auto &ln : v) {
            reverse(ln.begin(), ln.end());
        }
        return v;
    };
    auto revAll = [&](vector<vector<int>> v) {
        return revLines(revCols(v));
    };
    auto checkEq = [&](vector<vector<int>> v, int i, int j) {
        if (v[0].size() != getCell(i, j) * 2 + 1) {
            return false;
        }
        for (int shI = -getCell(i, j); shI <= getCell(i, j); ++shI) {
            for (int shJ = -getCell(i, j); shJ <= getCell(i, j); ++shJ) {
                if (getCell(i + shI, j + shJ) != v[shI + getCell(i, j)][shJ + getCell(i, j)]) {
                    return false;
                }
            }
        }
        return true;
    };
    // int ii = -1, jj = -1;
    // int ansI = -1, ansJ = -1;
    int num_ok = 0;
    int ans1 = 0, ans2 = 0, ans3 = 0, ans4 = 0;
    for (int i = 0; i < C; ++i) {
        for (int j = 0; j < C; ++j) {
            if (checkEq(v, i, j)) ans1 = 1;
            if (checkEq(revCols(v), i, j)) ans2 = 1;
            if (checkEq(revLines(v), i, j)) ans3 = 1;
            if (checkEq(revAll(v), i, j)) ans4 = 1;
        }
    }
    assert(ans1 + ans2 + ans3 + ans4 == 1);
    for (int i = 0; i < C; ++i) {
        for (int j = 0; j < C; ++j) {
            if (checkEq(v, i, j)) return {v.size() - 1, v.size() - 1};
            if (checkEq(revCols(v), i, j)) return {v.size() - 1, 0};
            if (checkEq(revLines(v), i, j)) return {0, v.size() - 1};
            if (checkEq(revAll(v), i, j)) return {0, 0};
        }
    }
}
