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
#include <bits/stdc++.h>

using namespace  std;

constexpr int CYCLE = 3 * 5 * 7 * 8;
constexpr int LOGMAXTIME = 22; // log(7 * 15000) + 1
constexpr int MAXDIM = 15000 + 8;
constexpr int MAXPAT = 8;

typedef bitset<CYCLE> Bitset;

Bitset masks[1 << (MAXPAT + 1)];

vector<Bitset> horiz_block, vert_block;

Bitset CycleMask(int len, uint m)
{
    Bitset b;
    for (int i = 0; i < CYCLE; ++i)
        if (m & (1 << (i % len)))
            b.set(i);
    return b;
}

void CreateMasks()
{
    for (int len = 2; len <= MAXPAT; ++len)
        for (uint m = 1; m < 1U << len; ++m)
            masks[(1 << len) | m] = CycleMask(len, m);
}

void ReadCross(int i, int j)
{
    char buf[16];
    scanf("%s", buf);
    int len = strlen(buf);
    uint m = 0;
    for (int i = 0; i < len; ++i)
        m |= (buf[i] - '0') << i;
    Bitset b = masks[(1 << len) | m];
    // 1 blocks horiz cross, 0 block vert cross
    horiz_block[i] &= b;
    vert_block[j] &= ~b;
}

struct Query
{
    int t, a, b, c, d;
    int ans = 0;
    void Read() { scanf("%d%d%d%d%d", &t, &a, &b, &c, &d); }
};

vector<Query> queries;

struct Jumps { uint16_t jump[LOGMAXTIME]; };

Jumps graph[MAXDIM][CYCLE];

template <bool forward>
void CreateGraph(const vector<Bitset>& block)
{
    int dim = (int) block.size();
    for (int t = 0; t < CYCLE; ++t) {
        if (forward)
            for (int i = dim; i >= 0; --i) {
                graph[i][t].jump[0] = i;
                if (i < dim && !block[i][t])
                    graph[i][t].jump[0] = graph[i + 1][t].jump[0];
            }
        else
            for (int i = 0; i <= dim; ++i) {
                graph[i][t].jump[0] = i;
                if (i > 0 && !block[i - 1][t])
                    graph[i][t].jump[0] = graph[i - 1][t].jump[0];
            }
    }
    for (int logt = 1; logt < LOGMAXTIME; ++logt) {
        int dt = logt == 1 ? 1 : 1 << (logt - 2);
        for (int t = 0; t < CYCLE; ++t) {
            int tt = (t + dt) % CYCLE;
            for (int i = 0; i <= dim; ++i)
                graph[i][t].jump[logt] = graph[graph[i][t].jump[logt - 1]][tt].jump[logt - 1];
        }
    }
}

template <bool forward>
int Time(int t, int a, int b)
{
    t %= CYCLE;
    int res = 0;
    for (int logt = LOGMAXTIME - 1; logt >= 0; --logt) {
        int c = graph[a][t].jump[logt];
        bool reachable = forward ? b <= c : b >= c;
        if (reachable)
            continue;
        int dt = logt == 0 ? 1 : 1 << (logt - 1);
        res += dt;
        t = (t + dt) % CYCLE;
        a = c;
    }
    return res;
}

void ProcessQueries()
{
    CreateGraph<true>(horiz_block);
    for (auto& q : queries) if (q.a < q.c) q.ans = max(q.ans, Time<true>(q.t, q.a, q.c));
    CreateGraph<false>(horiz_block);
    for (auto& q : queries) if (q.a > q.c) q.ans = max(q.ans, Time<false>(q.t, q.a, q.c));
    CreateGraph<true>(vert_block);
    for (auto& q : queries) if (q.b < q.d) q.ans = max(q.ans, Time<true>(q.t, q.b, q.d));
    CreateGraph<false>(vert_block);
    for (auto& q : queries) if (q.b > q.d) q.ans = max(q.ans, Time<false>(q.t, q.b, q.d));
}

int main()
{
    CreateMasks();
    int n, m, q;
    scanf("%d%d%d", &n, &m, &q);
    horiz_block.resize(n, ~Bitset());
    vert_block.resize(m, ~Bitset());
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < m; ++j)
            ReadCross(i, j);
    queries.resize(q);
    for (auto& query : queries)
        query.Read();
    ProcessQueries();
    for (auto& query : queries)
        printf("%d\n", query.t + query.ans);
    return 0;
}