Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

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


h,w,k = map(int, input().split())
x1,y1,x2,y2 = map(lambda x: int(x)-1, input().split())
s = [None]*h
for i in range(h):
    s[i] = input()
    
from heapq import heappop as hpp, heappush as hp
q = []
hp(q, (0,x1,y1,(0,0),0))

dx = [0,1,0,-1]
dy = [1,0,-1,0]
dist = [[float("inf")]*w for _ in range(h)]
dist[x1][y1] = 0

while q:
    d, x, y, t, td = hpp(q)
    for xx, yy in zip(dx,dy):
        if (-xx,-yy)==t:
            continue
        nx, ny = xx+x, yy+y
        if nx<0 or nx>=h or ny<0 or ny>=w:
            continue
        if s[nx][ny]=="@":
            continue
        if (xx,yy)==t and td<k:
            nd = d
            ntd = td+1
        else:
            nd = d+1
            ntd = 1
        if nd<=dist[nx][ny]:
            hp(q, (nd,nx,ny,(xx,yy),ntd))
            dist[nx][ny] = nd
if dist[x2][y2] >10**10:
    print(-1)
else:
    print(dist[x2][y2])

### FIXED CODE ###

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

h,w,k = map(int, input().split())
x1,y1,x2,y2 = map(lambda x: int(x)-1, input().split())
s = [None]*h
for i in range(h):
    s[i] = input()
    
from queue import deque
q0 = deque([(0,x1,y1,(0,0),0)])
q1 = deque()

dx = [0,1,0,-1]
dy = [1,0,-1,0]
# dist = [[None]*w for _ in range(h)]
dist = [None]*(w*h)
dist[x1*w+y1] = 0
# done = [[0]*w for _ in range(h)]
done = [0]*(w*h)
done[x1*w+y1] = 15

while q0 or q1:
    if not q0:
        q0 = q1
        q1 = deque()
    d, x, y, t, td = q0.popleft()
    for i,(xx, yy) in enumerate(zip(dx,dy)):
        if (-xx,-yy)==t:
            continue
        nx, ny = xx+x, yy+y
        if nx<0 or nx>=h or ny<0 or ny>=w or s[nx][ny]=="@":
            continue
        if (xx,yy)==t and td<k:
            nd = d
            ntd = td+1
            q = q0
        else:
            nd = d+1
            ntd = 1
            q = q1
        if dist[nx*w+ny] is None or nd<dist[nx*w+ny]:
            q.append((nd,nx,ny,(xx,yy),ntd))
            dist[nx*w+ny] = nd
            done[nx*w+ny] = 1<<(i)
        elif nd==dist[nx*w+ny]and ((done[nx*w+ny]&1<<(i))==0):
            q.append((nd,nx,ny,(xx,yy),ntd))
            done[nx*w+ny] |= 1<<(i)
            
if dist[x2*w+y2] is None:
    print(-1)
else:
    print(dist[x2*w+y2])
### BUGGY CODE ###

import collections

def main():
    n,m = map(int,input().split())
    f = [False for _ in range(n)]
    a = [[] for _ in range(n)]
    res = 0

    for _ in range(m):
        x,y,z = map(int,input().split())
        a[x-1].append(y-1)
        a[y-1].append(x-1)

    for i in range(n):
        if not f[i]:
            res += 1
            q=collections.deque()
            q.append(i)
            while q:
                t = q.pop()
                f[t] = True
                for g in a[t]:
                    if not f[g]:
                        q.append(g)
    print(res)
main()


### FIXED CODE ###

import sys
sys.setrecursionlimit(10**7)
n,m = map(int,input().split())
f = [False for _ in range(n)]
a = [[] for _ in range(n)]
res = 0

def dfs(k):
    f[k] = True
    for g in a[k]:
        if not f[g]:
            dfs(g)

for _ in range(m):
    x,y,z = map(int,input().split())
    a[x-1].append(y-1)
    a[y-1].append(x-1)

for i in range(n):
    if not f[i]:
        res += 1
        dfs(i)
print(res)
### BUGGY CODE ###

num = list(map(int,input().split()))
string = input()
s_key=[]
count=[]
for i in range(num[1]):
    s_key.append(list(map(int,input().split())))
for i in range(num[1]):
    count.append(0)
    for j in range(s_key[i][0]-1,s_key[i][1]-1):
        if (string[j]=='A' and string [j+1]=='C'):
            count[i]+=1
            j+=1
for k in range(num[1]):
    print(count[k])

### FIXED CODE ###

num = list(map(int,input().split()))
string = input()
s_key=[]
count=[]
for i in range(num[1]):
    s_key.append(list(map(int,input().split())))
for i in range(num[0]):
    if i==0:
        count.append(0)
    else:
        if (string[i-1]=='A' and string [i]=='C'):
            count.append(count[i-1]+1)
        else:
            count.append(count[i-1])
for k in range(num[1]):
    result=count[s_key[k][1]-1]-count[s_key[k][0]-1]
    print(result)
### BUGGY CODE ###

n = int(input())
h = [int(i) for i in input().split()]


def main():
    print("Yes" if detect() else "No")


def detect():
    m = min(h)+1
    for i in range(n):
        if h[i] <= m:
            continue
        if (h[i] - min(h[i::])) > 1:
            return False
    
    return True


main()


### FIXED CODE ###

n = int(input())
h = [int(i) for i in input().split()]


def main():
    h[0] -= 1
    print("Yes" if detect() else "No")


def detect():
    for i in range(1, n):
        if h[i] > h[i-1]:
            h[i] -= 1
        elif h[i] < h[i-1]:
            return False

    return True


main()
### BUGGY CODE ###

n = int(input())
a = [list(map(int, input().split())) for _ in range(n)]
mod = 10 ** 9 + 7

dp = [0] * (1 << n)
dp[0] = 1

for bit in range((1 << n) - 1):
    i = bin(bit).count('1')
    for j in range(n):
        newbit = bit | (1 << j)
        if a[i][j] and not((bit >> j) & 1):
            dp[newbit] += dp[bit]
            dp[newbit] %= mod
print(dp[(1 << n) - 1])


### FIXED CODE ###

mod = 10**9+7
n = int(input())
a = [list(map(int, input().split())) for i in range(n)]
dp = [0]*(1 << n)
dp[0] = 1
for i in range(1, 1 << n):
    k = bin(i).count("1")
    for j in range(n):
        if i >> j & 1 and a[k-1][j]:
            dp[i] += dp[i ^ (1 << j)]
    dp[i] %= mod
print(dp[(1 << n)-1])
### BUGGY CODE ###

N, K = map(int, input().split())
x_list = [0]*N
y_list = [0]*N
for i in range(N):
    x_list[i], y_list[i] = map(int, input().split())

x_sort_list = sorted(x_list)
y_sort_list = sorted(y_list)
ans = float("inf")

for i in range(len(x_sort_list)):
    for j in range(len(y_sort_list)):
        for k in range(i+1, len(x_sort_list)):
            for l in range(j+1, len(y_sort_list)):
                num = 0
                for m in range(N):
                    if x_sort_list[i] <= x_list[m] <= x_sort_list[k] and y_sort_list[j] <= y_list[m] <= y_sort_list[l]:
                        num += 1
                if num >= K:
                    ans = min((x_sort_list[k] - x_sort_list[i]) * (y_sort_list[l] - y_sort_list[j]), ans)

print(ans)


### FIXED CODE ###

N, K = map(int, input().split())
x_list = [0]*N
y_list = [0]*N
for i in range(N):
    x_list[i], y_list[i] = map(int, input().split())

x_element = sorted(set(x_list))
y_element = sorted(set(y_list))
ans = float("inf")

for i in range(len(x_element)):
    for j in range(len(y_element)):
        for k in range(i+1, len(x_element)):
            for l in range(j+1, len(y_element)):
                num = 0
                for m in range(N):
                    if x_element[i] <= x_list[m] <= x_element[k] and y_element[j] <= y_list[m] <= y_element[l]:
                        num += 1
                if num >= K:
                    ans = min((x_element[k] - x_element[i]) * (y_element[l] - y_element[j]), ans)

print(ans)
### BUGGY CODE ###

n=int(input())
d=[]
e=[]
for i in range(n):
    p=[0,0]
    s=input()
    for j in s:
        if j==")":
            if p[0]!=0:
                p[0]-=1
            else:
                p[1]+=1
        else:
            p[0]+=1
    if p[0]-p[1]>=0:
        d.append(p)
    else:
        e.append(p)
d=sorted(d,key=lambda x:x[1])
e=sorted(e,key=lambda x:-x[0])
s=d+e
q=0
for i,j in s:
    if q<j:
        print("No")
        exit()
    else:
        q=q-j+i
if q==0:
    print("Yes")
else:
    print("No")

### FIXED CODE ###

n=int(input())
d=[]
e=[]
for i in range(n):
    p=[0,0]
    s=input()
    for j in s:
        if j==")":
            if p[0]!=0:
                p[0]-=1
            else:
                p[1]+=1
        else:
            p[0]+=1
    if p[0]-p[1]>=0:
        d.append(p)
    else:
        e.append(p)
d=sorted(d,key=lambda x:x[1])
e=sorted(e,key=lambda x:-x[0])
s=d+e
q=0
for i,j in s:
    if q<j:
        print("No")
        exit()
    else:
        q=q-j+i
if q==0:
    print("Yes")
else:
    print("No")
### BUGGY CODE ###

from collections import defaultdict
import sys
sys.setrecursionlimit(10**7)
INF = 10**18
t = int(input())
for _ in range(t):
    n, a, b, c, d = map(int, input().split())
    memo = defaultdict(lambda: INF)

    def dfs(n, cost):

        if memo[n] <= cost:
            return 0

        memo[n] = cost
        if n == 0:
            return 0

        dfs(0, cost+n*d)

        r = n % 2
        dfs((n-r)//2, cost+a+r*d)
        if r != 0:
            dfs((n+(2-r))//2, cost+a+(2-r)*d)

        r = n % 3
        dfs((n-r)//3, cost+b+r*d)
        if r != 0:
            dfs((n+(3-r))//3, cost+b+(3-r)*d)

        r = n % 5
        dfs((n-r)//5, cost+c+r*d)
        if r != 0:
            dfs((n+(5-r))//5, cost+c+(5-r)*d)
            
    dfs(n, 0)
    print(memo[0])


### FIXED CODE ###

from collections import defaultdict
import sys
sys.setrecursionlimit(10**7)
INF = 10**18
t = int(input())
for _ in range(t):
    n, a, b, c, d = map(int, input().split())
    memo = defaultdict(lambda: INF)

    def dfs(n, cost):

        if memo[n] <= cost:
            return 0

        memo[n] = cost
        if n == 0:
            return 0

        dfs(0, cost+n*d)

        r = n % 2
        dfs((n-r)//2, cost+a+r*d)
        if r != 0:
            dfs((n+(2-r))//2, cost+a+(2-r)*d)

        r = n % 3
        dfs((n-r)//3, cost+b+r*d)
        if r != 0:
            dfs((n+(3-r))//3, cost+b+(3-r)*d)

        r = n % 5
        dfs((n-r)//5, cost+c+r*d)
        if r != 0:
            dfs((n+(5-r))//5, cost+c+(5-r)*d)
            
    dfs(n, 0)
    print(memo[0])
### BUGGY CODE ###

from collections import defaultdict

n, m = map(int, input().split())
ps_list = [[i for i in input().split()] for j in range(m)]

ac_list = []
wa_dict = defaultdict(int)
num_wa = 0

for ps in ps_list:
    p = int(ps[0])
    s = ps[1]
    
    if s == 'AC' and p not in ac_list:
        ac_list.append(p)
        num_wa += wa_dict[p]
    elif p not in ac_list:
        wa_dict[p] += 1
            
num_ac = len(ac_list)
        
print(num_ac, num_wa)

### FIXED CODE ###

n, m = map(int, input().split())
ps_list = [[i for i in input().split()] for j in range(m)]

ac_list = []
wa_list = [0 for i in range(n)]
num_wa = 0
num_ac = 0

for ps in ps_list:
    p = int(ps[0])
    s = ps[1]
    
    if s == 'AC' and wa_list[p-1] != -1:
        ac_list.append(p)
        num_wa += wa_list[p-1]
        num_ac += 1
        wa_list[p-1] = -1
    elif wa_list[p-1] != -1:
        wa_list[p-1] += 1
        
print(num_ac, num_wa)
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10**9)
from functools import reduce
from operator import mul

def two(a,ans):
    if a%2==0:
        return two(a//2,ans+1)
    else:
        return ans

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

ans1=0
#ans2=0
p=reduce(mul,A)
ans1=two(p,ans1)
#for i in range(N):
#    ans2=two(A[i],ans2)
print(ans1)

### FIXED CODE ###

from functools import reduce
from operator import mul

def two(a,ans):
    if a%2==0:
        return two(a//2,ans+1)
    else:
        return ans

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

#ans1=0
ans2=0
#p=reduce(mul,A)
#ans1=two(p,ans1)
for i in range(N):
    ans2=two(A[i],ans2)
print(ans2)
### BUGGY CODE ###

S = int(input())
count = 0

for i in range(1,len(str(S))+1):
    for j in range(i+4,len(str(S))+1):
        N = S%10**(len(str(S))-i+1)-S%10**(len(str(S))-j)
        if N%2019 == 0:
            count += 1

print(count)
  

### FIXED CODE ###

S = input()
count = 0
l = [0]*2019
l[0] = 1
mod = 2019
tmp = 0

for i in range(1,len(S)+1):
    tmp = (tmp + int(S[len(S)-i])*pow(10,i-1,mod))%mod
    l[tmp] += 1
    

for j in range(2019):
    if l[j] >= 2:
        count += l[j]*(l[j]-1)//2

print(count)
### BUGGY CODE ###

l = input().split()
N = int(l[0])
Y = int(l[1])

k10 = 0
k5 = 0
k1 = 0

max_k10 = Y // 10000
for i in reversed(range(max_k10 + 1)):
    remain10 = Y - i * 10000
    if remain10 == 0 and i == N:
        print(i, 0, 0)
        exit()
    max_k5 = remain10 // 5000
    for j in reversed(range(max_k5 + 1)):
        remain5 = remain10 - j * 5000
        if remain5 == 0 and i + j == N:
            print(i, j, 0)
            exit()
        max_k1 = remain5 // 1000
        for k in reversed(range(max_k1 + 1)):
            remain1 = remain5 - k * 1000
            if remain1 == 0 and i + j + k == N:
                print(i, j, k)
                exit()

print(-1, -1, -1)


### FIXED CODE ###

l = input().split()
N = int(l[0])
Y = int(l[1])

k10 = 0
k5 = 0
k1 = 0

max_k10 = min(N, Y // 10000)
for i in reversed(range(max_k10 + 1)):
    remain10 = Y - i * 10000
    if remain10 == 0 and i == N:
        print(i, 0, 0)
        exit()
    max_k5 = min(N, remain10 // 5000)
    for j in reversed(range(max_k5 + 1)):
        remain5 = remain10 - j * 5000
        if remain5 == 0 and i + j == N:
            print(i, j, 0)
            exit()
        z = N - i - j
        if remain5 - z * 1000 == 0:
            print(i, j, z)
            exit()

print(-1, -1, -1)
### BUGGY CODE ###

n,m=map(int,input().split())
root=[i for i in range(n+1)]
rank=[0]*(n+1)
def find(x):
    if x==root[x]:
        return x 
    else:
        return find(root[x])

def union(x1,y1):
    x=find(x1)
    y=find(y1)
    if rank[x]>rank[y]:
        root[y]=x 
    elif rank[x]<=rank[y]:
        root[x]=y
        if rank[x]==rank[y]:
            rank[y]+=1
def k_sub():    
    p=list(map(int,input().split()))
    p=[-1]+p 
    for _ in range(m):
        x,y=map(int,input().split())
        union(x,y)
    d=[[] for i in range(n+1)]
    u=[[] for i in range(n+1)]
    for i in range(1,n+1):
        d[find(i)].append(i)
        u[find(i)].append(p[i])
    cnt=0
    for i in range(1,n+1):
        #print(d[i])
        for candidate in d[i]:
            if candidate in u[i]:
                cnt+=1
    print(cnt)
if __name__=='__main__':
    k_sub()
    

### FIXED CODE ###

n,m=map(int,input().split())
p=list(map(int,input().split()))
uf={i:i for i in range(1,n+1)}
def get_p(i):
    lst=[]
    while uf[i]!=i:
        lst.append(i)
        i=uf[i]
    for l in lst[:-1]:
        uf[l]=i 
    return i 

def unite(i,j):
    uf[get_p(i)]=get_p(j)
def same(i,j):
    return get_p(i)==get_p(j)

def main():
    for i in range(m):
        x,y=map(int,input().split())
        unite(x,y)
    cnt=0
    for i in range(1,n+1):
        cnt+=same(p[i-1],uf[i])
    print(cnt)    
if __name__=='__main__':
    main()
### BUGGY CODE ###

x=input()
ans=len(x)
ind=x.find("ST")
while(ind!=-1):
    ans-=2
#     s_ind=ind-1
#     t_ind=ind+2
#     while(0<=s_ind and t_ind<len(x) and x[s_ind]=="S" and x[t_ind]=="T"):
#         ans-=2
#         s_ind-=1
#         t_ind+=1
    x=x[:ind]+x[ind+2:]
    ind=x.find("ST")
print(ans)

### FIXED CODE ###

x=input()
ans=[]
for i in range(len(x)):
    if(x[i]=='S'):
        ans.append(x[i])
    elif(len(ans)>0 and ans[-1]=='S'):
        ans.pop()
    else:
        ans.append(x[i])
print(len(''.join(ans)))
### BUGGY CODE ###

N = int(input())

A = [0] * 5

human_in_city = [0]*6
human_in_city[0] = N
moving_human = [0]*5

for i in range(5):
    A[i] = int(input())

count = 0
while human_in_city[-1] != N:
    count += 1
    for i in range(5):
        if human_in_city[i] >= A[i]:
            moving_human[i] = A[i]
        else:
            moving_human[i] = human_in_city[i]
    for i in range(5):
        human_in_city[i+1] += moving_human[i]
        human_in_city[i] -= moving_human[i]

print(count)

### FIXED CODE ###

import math

N = int(input())

A = [0] * 5

human_in_city = [0]*6
human_in_city[0] = N
moving_human = [0]*5

for i in range(5):
    A[i] = int(input())

# count = 0
# while human_in_city[-1] != N:
#     count += 1
#     for i in range(5):
#         if human_in_city[i] >= A[i]:
#             moving_human[i] = A[i]
#         else:
#             moving_human[i] = human_in_city[i]
#     for i in range(5):
#         human_in_city[i+1] += moving_human[i]
#         human_in_city[i] -= moving_human[i]

min_A = min(A)

count = 5 + math.ceil(N / min_A) -1

print(count)
### BUGGY CODE ###

N = int(input())
A=list(map(int,input().split()))
maxi=10**18
ans=1
for i in A:
    ans*=i
if ans>maxi:
    print(-1)
else:
    print(int(ans))

### FIXED CODE ###

import sys
N = int(input())
A=list(map(int,input().split()))
A.sort()
maxi=10**18
ans=1
for i in A:
    ans*=i
    if ans==0:
        print(0)
        sys.exit()
    if ans>maxi:
        print(-1)
        sys.exit()
print(ans)
### BUGGY CODE ###

N, M = map(int, input().split())
ans = 0
while N>=1 and M>=2 or M>=4:
    if N >=1 and M >= 2:
        N -= 1
        M -= 2
        ans += 1
    elif M >=4:
        M -= 4
        ans += 1
print(ans)

### FIXED CODE ###

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

if M <= 1:
    ans = 0

elif N >= M:
    ans = M//2

elif N < M:
    if N*2 >= M:
        ans = M//2
    else:
        ans += N
        ans += (M-N*2)//4
print(ans)
### BUGGY CODE ###

import math

a,b,n = map(int, input().split())
ans = 0
N = [x for x in range(1,n+1) if a*(x/b - int(x/b))>1]

for x in N:
  k = x/b
  f = math.floor(a*k) - math.floor(k)*a
  if ans<f:
    ans = f

print(ans)

### FIXED CODE ###

a,b,n = map(int, input().split())
yuka = lambda a,b: (a-(a%b))//b

x = min(n,b-1)
score = yuka(a*(x%b),b)
ans = score

print(ans)
### BUGGY CODE ###

from collections import Counter
def read_line(*types): return [f(a) for a, f in zip(input().split(), types)]

n, m, r = read_line(int, int, int)

rs = [int(r) for r in input().split()]

cities = Counter()

routes = {}
costs = {}
for _ in range(m):
    a, b, c = read_line(int, int, int)

    cities[a] = 1
    cities[b] = 1

    if a in routes:
        routes[a][b] = c
    else:
        routes[a] = { b : c }

    if b in routes:
        routes[b][a] = c
    else:
        routes[b] = { a : c }

for a in cities.keys():
    for b in cities.keys():
        if a not in costs:
            costs[a] = {}
        if b not in costs[a]:
            if a in routes and b in routes[a]:
                costs[a][b] = routes[a][b]
            else:
                costs[a][b] = 100000 + 1

for k in cities.keys():
    for a in cities.keys():
        for b in cities.keys():
            if costs[a][b] > costs[a][k] + costs[k][b]:
                costs[a][b] = costs[a][k] + costs[k][b]

def cost(selected):
    c = 0
    for i in range(len(selected) - 1):
        a = selected[i]
        b = selected[i + 1]
        c += costs[a][b]
    return c

smallest = 100000 + 1

def passing_all_cities(selected):
    for r in rs:
        if r not in selected:
            return False
    return True

def f(selected, rest):
    global smallest
    if len(rest) == 0:
        c = cost(selected)
        if smallest > c:
            smallest = c
    else:
        for i in range(len(rest)):
            r = rest[i]
            rest_copy = rest[:]
            del rest_copy[i]
            if cost(selected + [r]) < smallest:
                f(selected + [r], rest_copy)

f([], list(rs))
print(smallest)


### FIXED CODE ###

from itertools import permutations

def read_line(*types): return [f(a) for a, f in zip(input().split(), types)]

n, m, r = read_line(int, int, int)

rs = [int(r) for r in input().split()]

costs = {}
for i in range(n + 1):
    costs[i] = {}
    for j in range(n + 1):
        costs[i][j] = float('inf')

for _ in range(m):
    a, b, c = read_line(int, int, int)

    costs[a][b] = costs[b][a] = c

for k in range(n + 1):
    for i in range(n + 1):
        for j in range(n + 1):
            c = costs[i][k] + costs[k][j]
            if costs[i][j] > c:
                costs[i][j] = c

def cost(selected):
    c = 0
    for i in range(len(selected) - 1):
        a = selected[i]
        b = selected[i + 1]
        c += costs[a][b]
    return c

smallest = float('inf')

for p in permutations(rs):
    c = cost(p)
    if smallest > c:
        smallest = c

print(smallest)
### BUGGY CODE ###

S=list(input())
a=0
for i in range(len(S)):
  if S[i]=='W':
    a+=S[:i].count('B')
print(a)

### FIXED CODE ###

S=input()[::-1]
ans,tmp=0,0
for i in S:
  if i=='W':
    tmp+=1
  else:
    ans+=tmp
print(ans)
### BUGGY CODE ###

H, W = list(map(int, input().split(' ')))
xs = []
for i in range(H):
    xs.append(list(map(int, input().split(' '))))
# best でなくても良い？
ans = []  # x,y -> p, q
seeds = []
for i in range(H):  # 500
    for j in range(W):  # 500
        if xs[i][j] % 2 != 0:
            seeds.append((i, j))
used = {}
def f(st, ed):
    res = []
    while(st != ed):
        while(st[0] > ed[0]):
            if (st[0]-1, st[1]) in used:
                break
            res.append('%d %d %d %d' % (st[0]+1, st[1]+1, st[0]+1-1, st[1]+1))
            st = (st[0]-1, st[1])
            used[st] = True
        while(st[0] < ed[0]):
            if (st[0]+1, st[1]) in used:
                break
            res.append('%d %d %d %d' % (st[0]+1, st[1]+1, st[0]+1+1, st[1]+1))
            st = (st[0]+1, st[1])
            used[st] = True
        while(st[1] > ed[1]):
            if (st[0], st[1]-1) in used:
                break
            res.append('%d %d %d %d' % (st[0]+1, st[1]+1, st[0]+1, st[1]+1-1))
            st = (st[0], st[1]-1)
            used[st] = True
        while(st[1] < ed[1]):
            if (st[0], st[1]+1) in used:
                break
            res.append('%d %d %d %d' % (st[0]+1, st[1]+1, st[0]+1, st[1]+1+1))
            st = (st[0], st[1]+1)
            used[st] = True
    return res

ans = []
for i in range(0, len(seeds)-1, 2):
    st = seeds[i]
    ed = seeds[i+1]
    ans += f(st, ed)
print(len(ans))
print('\n'.join(ans))


### FIXED CODE ###

H, W = list(map(int, input().split(' ')))
xs = []
for i in range(H):
    xs.append(list(map(int, input().split(' '))))
# best でなくても良い？
ans = []  # x,y -> p, q
seeds = []
for i in range(H):  # 500
    for j in range(W):  # 500
        if xs[i][j] % 2 != 0:
            seeds.append((i, j))
used = {}
def f(st, ed):
    res = []
    while(st != ed):
        old = st
        if (st[0] % 2 == 0):
            if st[1] < W - 1:
                st = (st[0], st[1] + 1)
            else:
                st = (st[0]+1, st[1])
        else:
            if st[1] > 0:
                st = (st[0], st[1] - 1)
            else:
                st = (st[0]+1, st[1])
        res.append('%d %d %d %d' % (old[0]+1, old[1]+1, st[0]+1, st[1]+1))
    return res

ans = []
# print (seeds)
seeds = sorted(seeds, key=lambda x:(x[0] * 9999999 + x[1] * (-1 if x[0]%2 == 1 else 1)))
# print (seeds)
for i in range(0, len(seeds)-1, 2):
    st = seeds[i]
    ed = seeds[i+1]
    ans += f(st, ed)
print(len(ans))
print('\n'.join(ans))