#include using namespace std; int main() { long long N, Q; cin >> N >> Q; string S; cin >> S; vector rui_l(N, 0), rui_r(N, 0); for (int i = 0; i < N; i++) { if (i != 0) rui_l[i] = rui_l[i - 1]; if (S[i] == '0') rui_l[i] += 1; else rui_l[i] -= 1; if (i != 0) rui_r[N - i - 1] = rui_r[N - i]; if (S[N - i - 1] == '0') rui_r[N - i - 1] += 1; else rui_r[N - i - 1] -= 1; } for (int i = 0; i < Q; i++) { long long L, R, K; cin >> L >> R >> K; long long now = 0; bool flag = false; for (int i = L - 1; i < R; i++) { if (S[i] == '0') { now++; if (now >= K) flag = true; } else now = 0; } now = 0; for (int i = 0; i < N; i++) { if (S[i] == '1') { now++; if (now >= K) flag = true; } else now = 0; } if (!flag) cout << R - L + 1 << endl; else { long long sum = rui_l[N - 1]; if (L != 1) sum -= rui_l[L - 2]; if (R != N) sum -= rui_r[R]; long long amari = (sum + 1000000 * (2 * K - 1)) % (2 * K - 1); if (amari == 0) cout << 2 * (K - 1) << endl; else if (amari < K) cout << 2 * K - 2 - amari << endl; else cout << amari - 1 << endl; } } }