1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int MAX = 15'00 + 123;

#define cerr if (0) clog 

vector<vector<string>> tab;

int N, M, Q;

int odw[MAX][MAX];

int DY[] = {0, 0, 1, 1};
int DX[] = {0, 1, 0, 1};

int DDY[] = {-1, -1, 1, 1};
int DDX[] = {-1, 1 ,-1, 1};

int getkoszt(int y, int x, int time)
{
    int siz = tab[y][x].size();
    int koszt = 0;
    while (tab[y][x][(time + koszt) % siz] == tab[y][x][time % siz])
    {
        koszt++;
    }
    return koszt;
}

ll solve(int y, int x, int y_t, int x_t, ll time)
{
    priority_queue<pair<ll, pair<int, int>>> kolejka;
    for (int i = 0; i <= N; ++i)
        for (int j = 0; j <= M; ++j)
            odw[i][j] = 0;

    kolejka.push({-time, {y, x}});

    while (!kolejka.empty())
    {
        auto [time, yx] = kolejka.top();
        auto [y, x] = yx; time = -time;
        kolejka.pop();

        cerr << "Jestem na polu y=" << y << " x=" << x << " w czasie= " << time << "\n";

        if (odw[y][x])
            continue;
        odw[y][x] = 1;

        if (y == y_t && x == x_t)
        {
            return time;
        }

        for (int i = 0; i < 4; ++i)
        {
            int ny = y + DY[i];
            int nx = x + DX[i];


            if (ny <= N && 1 <= ny && nx <= M && 1 <= nx)
            {
                cerr << "Skrzyzowanie y=" << ny << " x= " << nx << " stany= "<< tab[ny][nx] << "\n"; 
                int koszt0 = 0;
                int koszt1 = 0;
                if (tab[ny][nx][time % tab[ny][nx].size()] == '0')
                {
                    cerr << "Zbieram koszt 1\n";
                    koszt1 = getkoszt(ny, nx, time);
                }
                else
                {
                    cerr << "Zbieram koszt 0\n";
                    koszt0 = getkoszt(ny, nx, time);
                }
                
                int nnx = x + DDX[i];
                int nny = y + DDY[i];

                cerr << "Koszt1= " << koszt1 << " koszt0= " << koszt0 << "\n";

                if (0 <= nny && nny <= N)
                {
                    kolejka.push({-(time + koszt0), {nny, x}});
                }

                if (0 <= nnx && nnx <= M)
                {
                    kolejka.push({-(time + koszt1), {y, nnx}});
                }
            }

            cerr << "\n";
        }

        cerr << "\n\n\n";
    }

    return -1;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);


    cin >> N >> M >> Q;

    tab.resize(N + 1);

    for (int i = 1; i <= N; ++i)
    {
        tab[i].resize(M + 1);
        for (int j = 1; j <= M; ++j)
            cin >> tab[i][j];
    }

    while (Q--)
    {
        int a, b, c, d, t;
        cin >> t >> a >> b >> c >> d;

        ll res = solve(a, b, c, d, t);

        cout << res << "\n";
    }

    

    return 0;
}