Harryxun/stf_run huggingface dataset:
H,W=map(int,input().split())
Map=[[int(i) for i in input().split()] for i in range(H)]
Tell=[]
for y in range(H): 
  for x in range(W):
    if Map[y][x]%2==1:
      if x==W-1:
        Map[y][x]=1
      elif Map[y][x+1]%2==0:
        Map[y][x]=0
        Map[y][x+1]=1
        Tell.append(str(y+1)+" "+str(x+1)+" "+str(y+1)+" "+str(x+2))
      else:
        Map[y][x]=0
        Map[y][x+1]=0
        Tell.append(str(y+1)+" "+str(x+1)+" "+str(y+1)+" "+str(x+2))
for y in range(H):
  if Map[y][W-1]==1:
    if y==H-1:
      pass
    if Map[y+1][W-1]==0:
      Map[y][W-1]=0
      Map[y+1][W-1]=1
      Tell.append(str(y+1)+" "+str(x+1)+" "+str(y+2)+" "+str(x+1))
    if Map[y+1][W-1]==1:
      Map[y][W-1]=0
      Map[y+1][W-1]=0
      Tell.append(str(y+1)+" "+str(x+1)+" "+str(y+2)+" "+str(x+1))
print(len(Tell))
for t in Tell:
  print(t)
   
      
     
    
     

### FIXED CODE ###

H,W=map(int,input().split())
Map=[[int(i) for i in input().split()] for i in range(H)]
Tell=[]
for y in range(H): 
  for x in range(W):
    if Map[y][x]%2==1:
      if x==W-1:
        Map[y][x]=1
      elif Map[y][x+1]%2==0:
        Map[y][x]=0
        Map[y][x+1]=1
        Tell.append(str(y+1)+" "+str(x+1)+" "+str(y+1)+" "+str(x+2))
      else:
        Map[y][x]=0
        Map[y][x+1]=0
        Tell.append(str(y+1)+" "+str(x+1)+" "+str(y+1)+" "+str(x+2))
    else:
      Map[y][x]=0
for y in range(H):
  if Map[y][W-1]==1:
    if y==H-1:
      pass
    elif Map[y+1][W-1]==0:
      Map[y][W-1]=0
      Map[y+1][W-1]=1
      Tell.append(str(y+1)+" "+str(x+1)+" "+str(y+2)+" "+str(x+1))
    elif Map[y+1][W-1]==1:
      Map[y][W-1]=0
      Map[y+1][W-1]=0
      Tell.append(str(y+1)+" "+str(x+1)+" "+str(y+2)+" "+str(x+1))
print(len(Tell))
for t in Tell:
  print(t)
import math
A, B, C, D = map(int, input().split())

dc = math.floor(B/C) - math.floor((A-1)/C)
dd = math.floor(B/D) - math.floor((A-1)/D)
lcmCD = (C*D)/math.gcd(C,D)
dcd = math.floor(B/lcmCD) - math.floor((A-1)/lcmCD)

print(B-A+1 - (dc + dd - dcd))

### FIXED CODE ###

a, b, c, d = map(int, input().split())
import fractions
 
def num_divide(p, q, r):
    if p % r == 0:
        rmin = p
    else:
        rmin = p + (r-p%r)
    rmax = q - q%r
    return (rmax-rmin)//r + 1
 
print(b - a + 1 - (num_divide(a, b, c) + num_divide(a, b, d) - num_divide(a, b, c * d // fractions.gcd(c, d))))
from statistics import mean, median,variance,stdev
import numpy as np
import sys
import math
import fractions
import itertools
import copy
from operator import itemgetter
#以下てんぷら
def j(q):
    if q==1: print("Yes")
    else:print("No")
    exit(0)


def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")

def ip():
    return int(input())

def pne(n):
    print(n,end='')

rem = pow(10,9)+7

#n = ip()                                     #入力整数1つ
#x,y= (int(i) for i in input().split())       #入力整数横2つ以上
#a = [int(i) for i in input().split()]        #入力整数配列
a = input()                                  #入力文字列
#a = input().split()                          #入力文字配列
#n = ip()                      #入力セット(整数改行あり)(1/2)
#a=[input() for i in range(n)]    #入力セット(整数改行あり)(2/2)

#jの変数はしようできないので注意
#全足しにsum変数使用はsum関数使用できないので注意
#こっから本文
b = ["A","C","T","G"]
s = []
c = 0
for i in range(len(a)):
    if a[i] in b:
        c+=1
    else:
        s.append(c)
        c=0
print(max(s))

### FIXED CODE ###

from statistics import mean, median,variance,stdev
import numpy as np
import sys
import math
import fractions
import itertools
import copy
from operator import itemgetter
#以下てんぷら
def j(q):
    if q==1: print("Yes")
    else:print("No")
    exit(0)


def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")

def ip():
    return int(input())

def pne(n):
    print(n,end='')

rem = pow(10,9)+7

#n = ip()                                     #入力整数1つ
#x,y= (int(i) for i in input().split())       #入力整数横2つ以上
#a = [int(i) for i in input().split()]        #入力整数配列
a = input()                                  #入力文字列
#a = input().split()                          #入力文字配列
#n = ip()                      #入力セット(整数改行あり)(1/2)
#a=[input() for i in range(n)]    #入力セット(整数改行あり)(2/2)

#jの変数はしようできないので注意
#全足しにsum変数使用はsum関数使用できないので注意
#こっから本文
b = ["A","C","T","G"]
s = [0]
c = 0
for i in range(len(a)):
    if a[i] in b:
        c+=1
    else:
        s.append(c)
        c=0
s.append(c)
print(max(s))
n,k=map(int,input().split())
if n<k:
  print(1)
  exit()
for i in range (100):
  if k**i==n:
    print(i)
  else:
     k**i < n <k**(i+1)
    print(i+1)

### FIXED CODE ###

n,k=map(int,input().split())
ans=0
while n>0:
  n = n//k
  ans +=1
print(ans)
print(int(input())**3/27)

### FIXED CODE ###

s=list(input())
n=len(s)
flag1=True
flag2=True

b=s[:(n-1)//2]
c=s[(n+1)//2:]

if b==b[::-1] and c==c[::-1] and s==s[::-1]:
    print("Yes")
else:
    print("No")
N, K  = map(int, input().split())
M = 10 ** 9 + 7
from math import factorial as f
def c(n, r):
    return f(n)//(f(r)*f(n-r))

def h(s, n):
    return c(s-1, n-1)

for i in range(1, K+1):
    a = h(K, i)
    b = c(N-K+1, i)
    print(a*b%M)


### FIXED CODE ###

N, K  = map(int, input().split())
M = 10 ** 9 + 7

C = [[0 for j in range(N+3)] for i in range(N+3)]
C[1][1] = 1

for i in range(2, N+2):
    for j in range(1, i+2):
        C[i][j] = C[i-1][j] + C[i-1][j-1]

for i in range(1, K+1):
    print(C[N-K+2][i+1] * C[K][i] %  M)
n=int(input())
dp=[1 for i in range(n+1)]
dp[0]=dp[1]=dp[2]=0
modd=1e9+7
for i in range(3,n):
    for j in range(3,n-i+1):
        dp[i+j]=(dp[i+j]+dp[i])%modd
print(int(dp[n]))

### FIXED CODE ###

n=int(input())
dp=[1 for i in range(n+2)]
dp[0]=dp[1]=dp[2]=0
modd=1e9+7
for i in range(3,n):
    for j in range(3,n-i+1):
        dp[i+j]=(dp[i+j]+dp[i])%modd
print(int(dp[n]))
x=int(intput())
if x==3||x==5||x==7:
  print ("YES")
else :
	print("NO")

### FIXED CODE ###

x=int(input())
if x==3 or x==5 or x==7:
  print ("YES")
else :
	print("NO")
"""
手順
1. 必要になる桁数を求め，アルファベットの順番を保存するリスト「ans」を作成する
2. 
"""


N = int(input())  # 変換したい10進数の値
K = 26  # 進数

al = [chr(i) for i in range(97, 97+K)]


def change(N):
    # 手順１の実装
    keta = 0
    # 桁が切り替わる時は26のi乗の時ではなく26のi乗にそれまでの数値を足したものである
    # tmpはi-1乗までの数値を記録する変数として使用
    tmp = 0
    for i in range(10**9):
        if N < K**i + tmp:
            keta = i
            break
        tmp += K**i
    ans = [0] * keta  # 桁数の配列を準備する
    print(tmp)
    # print(ans)  # デバッグ用

    # 手順2の実装
    check = 0
    for i in range(1, keta+1):
        j = N // (K ** (keta - i))
        print(j, N - (j * (K ** (keta - i))))
        if i != keta and N - (j * (K ** (keta - i))) < tmp:
            j -= 1
        if j == 27:
            j -= 1
        N -= j * (K ** (keta - i))
        tmp -= K**(keta - i)
        ans[check] = j
        check += 1
    # print(ans)
    return ans


for i in change(N):
    print(al[i-1], end='')


### FIXED CODE ###



def main():
    N = int(input())
    digits = []

    while N != 0:
        digits.append(N % 26)
        if digits[-1] == 0:
            digits[-1] = 26
            N //= 26
            N -= 1
        else:
            N //= 26
    digits.reverse()
    for d in digits:
        print(chr(96 + d), end='')
    print()  # 改行


if __name__ == '__main__':
    main()
from math import gcd

a, b, c, d = map(int, input().split())
cx = b//c - (a-1)//c
dx = b//d - (a-1)//d
lcm = (c * d) // gcd(c, d)
lcmx = b//lcm - (a-1)//lcm
print(b-a+1-cx-dx+lcmx)

### FIXED CODE ###

from fractions import gcd

a, b, c, d = map(int, input().split())
cx = b//c - (a-1)//c
dx = b//d - (a-1)//d
lcm = (c * d) // gcd(c, d)
lcmx = b//lcm - (a-1)//lcm
print(b-a+1-cx-dx+lcmx)
import math


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(inputs):
    N = int(inputs[0])
    power = 1
    for i in range(1, N + 1):
        power = (i * power) % (10 ** 9 + 7)
    return power


if __name__ == "__main__":
    ret = solve(inputs(1))
    print(ret)

### FIXED CODE ###

def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(inputs):
    [N, K] = list(map(lambda x: int(x), inputs[0].split()))
    pattern = K
    for i in range(1, N):
        pattern *= K - 1
    return pattern


if __name__ == "__main__":
    ret = solve(inputs(1))
    print(ret)
n,m = map(int,input().split(' '))

bar = [[] for _ in range(m)]

for i in range(n):
    a,b = map(lambda x:int(x)-1,input().split(' '))
    bar[a].append(b)
    bar[b].append(a) 


result = 0

def search(root=[0]):
    global result
    if len(root) >= n:
        result += 1
    nxt = set(bar[root[-1]])
    nxt = nxt - set(root)
    for nxt_ in nxt:
        search(root+[nxt_])

search()
print(result)

### FIXED CODE ###

n,m = map(int,input().split(' '))

bar = [[] for _ in range(n)]

for i in range(m):
    a,b = map(lambda x:int(x)-1,input().split(' '))
    bar[a].append(b)
    bar[b].append(a)

result = 0

def search(root=[0]):
    global result
    if len(root) >= n:
        result += 1
        return
    nxt = set(bar[root[-1]])
    nxt = nxt - set(root)
    for nxt_ in nxt:
        search(root+[nxt_])

search()
print(result)
import sys
import collections as c
sys.setrecursionlimit(10**6)

class UnionFind():
	def __init__(self, n):
		self.size = n
		self.root = [-1 for _ in range(n+1)]
		self.rnk = [0 for _ in range(n+1)]

	def findRoot(self, a):
		if self.root[a] < 0:
			return a
		else:
			return self.findRoot(self.root[a])

	def uniteTree(self, a, b):
		a = self.findRoot(a)
		b = self.findRoot(b)

		if a == b:
			return

		elif self.rnk[a] > self.rnk[b]:
			self.root[a] += self.root[b]
			self.root[b] = a

		elif self.rnk[b] > self.rnk[a]:
			self.root[b] += self.root[a]
			self.root[a] = b

		elif self.rnk[a] == self.rnk[b]:
			self.root[b] += self.rnk[a]
			self.root[a] = b
			self.rnk[b] += 1

	def judgeTree(self, a, b):
		return self.findRoot(a) == self.findRoot(b)

	def count(self, a):
		return self.rnk[self.findRoot(a)] * (-1)

N, K, L = map(int,input().split())
p = []
q = []
l = []
r = []

for _ in range(K):
	p_tmp, q_tmp = map(int ,input().split())
	p.append(p_tmp)
	q.append(q_tmp)

for _ in range(L):
	l_tmp, r_tmp = map(int, input().split())
	l.append(l_tmp)
	r.append(r_tmp)

road = UnionFind(N)
train = UnionFind(N)

for i in range(K):
	road.uniteTree(p[i], q[i])

for i in range(L):
	train.uniteTree(l[i], r[i])

ar = ["[-1,-1]"]

for i in range(1,N+1):
	a = road.findRoot(i)
	b = train.findRoot(i)
	ar.append(str([a,b]))

Car = c.Counter(ar)

for i in range(1,N+1):
	print(Car[ar[i]], end = " ")

### FIXED CODE ###

import sys
import collections as c
sys.setrecursionlimit(10**6)

import sys
sys.setrecursionlimit(10**7)

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.root = [-1]*(n+1)
        self.rnk = [0]*(n+1)

    def Find_Root(self, x):
        if(self.root[x] < 0):
            return x
        else:
            self.root[x] = self.Find_Root(self.root[x])
            return self.root[x]
    def Unite(self, x, y):
        x = self.Find_Root(x)
        y = self.Find_Root(y)
        if(x == y):
            return 
        elif(self.rnk[x] > self.rnk[y]):
            self.root[x] += self.root[y]
            self.root[y] = x

        else:
            self.root[y] += self.root[x]
            self.root[x] = y
            if(self.rnk[x] == self.rnk[y]):
                self.rnk[y] += 1
    def isSameGroup(self, x, y):
        return self.Find_Root(x) == self.Find_Root(y)

    def Count(self, x):
        return -self.root[self.Find_Root(x)]

N, K, L = map(int,input().split())
p = []
q = []
l = []
r = []

for _ in range(K):
	p_tmp, q_tmp = map(int ,input().split())
	p.append(p_tmp)
	q.append(q_tmp)

for _ in range(L):
	l_tmp, r_tmp = map(int, input().split())
	l.append(l_tmp)
	r.append(r_tmp)

road = UnionFind(N)
train = UnionFind(N)

for i in range(K):
	road.Unite(p[i], q[i])

for i in range(L):
	train.Unite(l[i], r[i])

ar = []

for i in range(1,N+1):
	a = road.Find_Root(i)
	b = train.Find_Root(i)
	ar.append(str([a,b]))

Car = c.Counter(ar)

for i in range(N):
	print(Car[ar[i]], end = " ")
x = [int(input()) for i in range(4)]
print(min(x[0],x[1]) + min(x[2],x[3]))

### FIXED CODE ###

a,b,c = map(int,input().split())
if a + b >= c:
    print("Yes")
else:
    print("No")
N = int(input())
M = int(input())

if N==M:
  print("Yes")
else:
  print("No")


### FIXED CODE ###

N, M = map(int, input().split())

if N==M:
  print("Yes")
else:
  print("No")
if Y != 1:
	print(X * (Y-1))
else:
	print(-1)

### FIXED CODE ###

X,Y = map(int,input().split(" "))
if Y == 1:
	print(-1)
	exit(0)

if X < Y:
	print(X)
	exit(0)

if X % Y == 0:
	print(-1)
else:
	print(X)
import numpy as np
x,y,z,k = map(int,input().split())
a = np.array(list(map(int,input().split())))
b = np.array(list(map(int,input().split())))
c = np.array(list(map(int,input().split())))
a2,b2,c2 = np.meshgrid(a,b,c)
abc = np.c_[a2.flatten(),b2.flatten(),c2.flatten()]
ans = []
for i in range(x*y*z):
    ans.append(sum(abc[i]))
ans = np.sort(ans)[::-1]
for i in range(k):
    print(ans[i])

### FIXED CODE ###

x,y,z,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))
ans = []
for i in range(x):
    for j in range(y):
        ans.append(a[i]+b[j])
ans.sort(reverse=True)
ans = ans[:k]
ans2 = []
for i in range(len(ans)):
    for j in range(z):
        ans2.append(ans[i]+c[j])
ans2.sort(reverse=True)
for i in range(k):
    print(ans2[i])
n, m = map(int, input().split())
a = [int(input()) for _ in range(m)]
s = [1]*(n+1)

if a[0] == 1:
    s[1] = 0

for i in range(n-1):
    if i+2 in a:
        s[i+2] = 0
    else:
        s[i+2] = (s[i+1] + s[i]) % (10**9+7)

print(s[-1])

### FIXED CODE ###

n, m = map(int, input().split())
a = set([int(input()) for _ in range(m)])

s = [1]*(n+1)

if 1 in a:
    s[1] = 0

for i in range(n-1):
    if i+2 in a:
        s[i+2] = 0
        continue
    s[i+2] = (s[i+1] + s[i]) % (10**9+7)
    
print(s[-1])
X, t = map(int, input().split())
print(x-t)

### FIXED CODE ###

X, t = map(int, input().split())
D = X -t
if(D>0):
    print(D)

else:
    print('0')
M, K = map(int, input().split())
if K>1<<M-1:
    print(-1)
elif M == 1 and K == 1:
    print(-1)
elif M == 1 and K == 0:
    print(0, 0, 1, 1)
elif M == 0 and K == 0:
    print(0, 0)
elif M == 0:
    print(-1)
else:
    N = 1<<M
    ans = list(range(K))+list(range(K+1, N))+[K]+list(range(K+1, N))[::-1]+list(range(K))[::-1]+[K]
    print(*ans)

### FIXED CODE ###

M, K = map(int, input().split())
if K>(1<<M)-1:
    print(-1)
elif M == 1 and K == 1:
    print(-1)
elif M == 1 and K == 0:
    print(0, 0, 1, 1)
elif M == 0 and K == 0:
    print(0, 0)
elif M == 0:
    print(-1)
else:
    N = 1<<M
    ans = list(range(K))+list(range(K+1, N))+[K]+list(range(K+1, N))[::-1]+list(range(K))[::-1]+[K]
    print(*ans)
N, K = map(int,input().split())
a = list(map(int,input().split()))

total = 0
ans = 0
r = 0

for l in range(N):
    while total < K:

        if r >= N:
            break

        else:
            total += a[r]
            r += 1

    if total < K:
        break

    ans += N-r+1
    total -= a[l]

print(ans)

### FIXED CODE ###

X, A = map(int,input().split())

if X < A:
    print(0)
else:
    print(10)
li = list(map(int, input().split()))
if (len(li) == 2:
    print("Yes")
else:
    print("No")


### FIXED CODE ###

li = list(map(int, input().split()))
if (len(set(li))) == 2:
    print("Yes")
else:
    print("No")
N,M = input().split()
N = int(N)
M = int(M)
list = []
A_list = []

for i in range(M):
    x,y,z = input().split()
    x,y,z = int(x),int(y),int(z)
    list.append([x,y,z])

A_list.append([list[0][0]])
for data in list:
    check = 0
    for A_data in A_list:
        if data[0] in A_data:
            A_data.append(data[1])
            check = 0
        elif data[1] in A_data:
            A_data.append(data[0])
            check = 0
        elif not data[0] in A_data and not data[1] in A_data:
            check = 1
    if check:
        A_list.append([data[0],data[1]])

print(len(A_list))

### FIXED CODE ###

import sys

N,K = input().split()
S = input()

N = int(N)
K = int(K)

S_new = S[:K-1] + S[K-1].lower() + S[K:]

print("{}".format(S_new))
a, b, c = map(int, input().split())
k = int(input())

m = max(a, b, c)
ans = sum(a, b, c) - m + (2**k) * m

print(ans)


### FIXED CODE ###

a, b, c = map(int, input().split())
k = int(input())

m = max(a, b, c)
ans = a + b + c - m + (2**k) * m

print(ans)
As,Bs=input().split()
A=int(As)
B=float(Bs)
B100=int((Bs+0.001)*100)
print(A*B100//100)

### FIXED CODE ###

As,Bs=input().split()
A=int(As)
Bi,Bf=map(int,Bs.split("."))

print(A*Bi+A*Bf//100)
K=input()
S=input()

if len(S)>=K:
  print(s)
else:
  print(s[:K]+"...")

### FIXED CODE ###

K=int(input())
S=input()

if len(S)<=K:
  print(S)
else:
  print(S[:K]+"...")
p, q, r = list(map(int, input().split()))
pqr.sort()
print(pqr[0] + pqr[1])

### FIXED CODE ###

pqr = list(map(int, input().split()))
pqr.sort()
print(pqr[0] + pqr[1])
a,b = map(double,input().split())
print(a/b)

### FIXED CODE ###

a,b = map(float,input().split())
print(a/b)
import math
a, b, c, d = map(int, input().split())

large_c = b // c
large_d = b // d
large_cd = b // ((c * d) / math.gcd(c, d))
large = large_c + large_d - large_cd

small_c = (a - 1) // c
small_d = (a - 1) // d
small_cd = (a - 1) // ((c * d) / math.gcd(c, d))
small = small_c + small_d - small_cd

print(int((b - (a - 1)) - (large - small)))


### FIXED CODE ###


a, b, c, d = map(int, input().split())

def gcd (a, b):
    while b != 0:
        a, b = b, a % b
    return a
gcd = gcd(c, d)

large_c = b // c
large_d = b // d
large_cd = b // ((c * d) // gcd)
large = large_c + large_d - large_cd

small_c = (a - 1) // c
small_d = (a - 1) // d
small_cd = (a - 1) // ((c * d) // gcd)
small = small_c + small_d - small_cd

print(int((b - (a - 1)) - (large - small)))
input = sys.stdin.readline
N, Q = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N-1)]
P = [0] * Q
X = [0] * Q
V = [0] * (N + 1)
for i in range(Q):
    P, X = map(int, input().split())
    V[P] += X

tr = Tree(N, AB)

### FIXED CODE ###

from collections import deque

N, Q = map(int, input().split())
AB = {i: set() for i in range(1, N+1)}
for i in range(N - 1):
    tmpA, tmpB = map(int, input().split())
    AB[tmpA].add(tmpB)
    AB[tmpB].add(tmpA)
P = [0] * Q
X = [0] * Q
V = [0] * (N + 1)
for i in range(Q):
    P, X = map(int, input().split())
    V[P] += X

check = [0]*(N + 1)
q = deque()
q.append(1)
check[1] = 1
while q != deque():
    node = q.pop()
    for i in AB[node]:
        if check[i] == 1:
            continue
        V[i] += V[node]
        check[i] = 1
        q.appendleft(i)
print(*V[1:])
import math
import collections

n = int(input())
d = list(map(int,input().split()))
MOD = 998244353
  
#同じ長さの位置に何個頂点があるか
cnt = dict(collections.Counter(d))
#print(cnt)
try:
  if cnt[0]!=0:
    ans = 1
    for i in range(len(dict(cnt))-1):
      val_1 = cnt[i]
      val_2 = cnt[i+1]
      ans *= (val_1**val_2)%MOD

    print(ans%MOD)
except KeyError:
  print(0)
  


### FIXED CODE ###

n = int(input())
if n % 2 == 0:
  print(n//2-1)
else:  
  print(n//2)
n=int(input())
s=input()
c=0
for i in range(len(s)):
    if s[i]=="A":
        if s[i+1]=="B":
            if s[i+2]=="C":
                c+=1
                i+=2

print(c)

### FIXED CODE ###

n=int(input())
s=input()
c=0
for i in range(len(s)-2):
    if s[i]=="A":
        if s[i+1]=="B":
            if s[i+2]=="C":
                c+=1
                i+=2

print(c)
n,k = map(int,input().split())
a = list(map(int,input().split()))
flag = True
went = {1}
go = [1]
i = 0
while flag:
  if a[i] not in went:
    go.append(a[i])
    went.add(a[i])
    i = a[i]-1
  else:
    flag = False
loop = go[go.index(a[i]):]
if len(go) >= k:
  print(go[k])
else:
  k -= len(go)-len(loop)
  ans = k % len(loop)
  print(loop[ans])

### FIXED CODE ###

n,k = map(int,input().split())
a = list(map(int,input().split()))
flag = True
went = {1}
go = [1]
i = 0
while flag:
  if a[i] not in went:
    go.append(a[i])
    went.add(a[i])
    i = a[i]-1
  else:
    flag = False
loop = go[go.index(a[i]):]
if len(go) > k:
  print(go[k])
else:
  k -= len(go)-len(loop)
  ans = k % len(loop)
  print(loop[ans])
a, b, c = input()
count = 1
if a == "1":
  count = count +1
else:
  count = count
  if b == "1":
  count = count +1
else:
  count = count
  if c == "1":
  count = count +1
else:
  count = count

print(count)

### FIXED CODE ###

a, b, c = input()
d = 0
if a == "1":
  d = d + 1
else:
  d = d
if b == "1":
  d = d + 1
else:
  d = d
if c == "1":
  d = d + 1
else:
  d = d
 
print(d)
import bisect

N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
A.sort()

cnt = 1
while cnt <= M:
    x = bisect.bisect_right(A, A[-1]/2)
    for i in range(x + 1):
        A[-i -1] /= 2
        cnt += 1
        if cnt > M:
            break

print(sum(map(int, A)))

### FIXED CODE ###

import bisect

N, M = list(map(int, input().split()))
A = list(map(lambda x: int(x) * -1, input().split()))
A.sort()

cnt = 1
while cnt <= M:
    x = bisect.bisect(A, A[0]/2)
    for i in range(x):
        A[i] /= 2
        cnt += 1
        if cnt > M:
            break
    if x != 0 or x != len(A):
        A.sort()
print(sum(map(lambda x: int(-x), A)))
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n,m,q = list(map(int, input().split()))
s = [None]*n
for i in range(n):
    s[i] = list(map(int, input()))
e1 = [[0]*(m) for _ in range(n-1)]
e2 = [[0]*(m-1) for _ in range(n)]
for i in range(n):
    for j in range(m):
        if i<n-1 and s[i][j]==s[i+1][j]==1:
            e1[i][j] = 1
        if j<m-1 and s[i][j]==s[i][j+1]==1:
            e2[i][j] = 1
def cumsum(a):
    """l[i] = sum(a[:i]) なるlを返す
    sum(a[i:j]) == l[j+1] - l[i]
    """
    c = 0
    l = [None] * (len(a)+1)
    for i,num in enumerate(a):
        l[i] = c
        c += num
    l[-1] = c
    return l

def cumsum2(v):
    # v: h*wの2次元リスト
    h,w = len(v), len(v[0])
    ans = [[0]*(w+1) for _ in range(h+1)]
    val = [cumsum(vv) for vv in v]
    for y in range(w+1):
        tmp = cumsum([val[i][y] for i in range(h)])
        for x in range(h+1):
            ans[x][y] = tmp[x]
    return ans
s = cumsum2(s)
e1 = cumsum2(e1)
e2 = cumsum2(e2)
ans = [None]*q
for i in range(q):
    x1,y1,x2,y2 = map(lambda x: int(x)-1, input().split())
    val = s[x2+1][y2+1] - s[x1][y2+1] - s[x2+1][y1] + s[x1][y1]
    val2 = e1[x2][y2+1] - e1[x2][y1] - e1[x1][y2+1] + e1[x1][y1]
    val3 = e2[x2+1][y2] - e2[x2+1][y1] - e2[x1][y2] + e2[x1][y1]
    ans[i] = val - (val2+val3)
write("\n".join(map(str, ans)))

### FIXED CODE ###

import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n,m,q = list(map(int, input().split()))
s = [None]*n
for i in range(n):
    s[i] = list(map(int, input()))
e1 = [[0]*(m) for _ in range(n-1)]
e2 = [[0]*(m-1) for _ in range(n)]
for i in range(n):
    for j in range(m):
        if i<n-1 and s[i][j]==s[i+1][j]==1:
            e1[i][j] = 1
        if j<m-1 and s[i][j]==s[i][j+1]==1:
            e2[i][j] = 1
def cumsum2(v):
    # v: h*wの2次元リスト
    h,w = len(v), len(v[0])
    v.insert(0, [0]*w)
    for l in v:
        l.insert(0, 0)
    for i in range(1,h+1):
        for j in range(1,w+1):
            v[i][j] = v[i-1][j] + v[i][j-1] - v[i-1][j-1] + v[i][j]
    return v
s = cumsum2(s)
if e1:
    e1 = cumsum2(e1)
if e2:
    e2 = cumsum2(e2)
ans = [None]*q
for i in range(q):
    x1,y1,x2,y2 = map(lambda x: int(x)-1, input().split())
    val = s[x2+1][y2+1] - s[x1][y2+1] - s[x2+1][y1] + s[x1][y1]
    if e1:
        val2 = e1[x2][y2+1] - e1[x2][y1] - e1[x1][y2+1] + e1[x1][y1]
    else:
        val2 = 0
    if e2:
        val3 = e2[x2+1][y2] - e2[x2+1][y1] - e2[x1][y2] + e2[x1][y1]
    else:
        val3 = 0
    ans[i] = val - (val2+val3)
write("\n".join(map(str, ans)))