import sys sys.setrecursionlimit(10**7) def MI(): return map(int,sys.stdin.readline().rstrip().split()) def LI2(): return list(map(int,sys.stdin.readline().rstrip())) class UnionFind: def __init__(self,n): self.par = [i for i in range(n+1)] # 親のノード番号 self.rank = [0]*(n+1) self.X = [1]*(n+1) def find(self,x): # xの根のノード番号 if self.par[x] == x: return x else: self.par[x] = self.find(self.par[x]) return self.par[x] def same_check(self,x,y): # x,yが同じグループか否か return self.find(x) == self.find(y) def unite(self,x,y): # x,yの属するグループの併合 x = self.find(x) y = self.find(y) if x == y: return if self.rank[x] < self.rank[y]: x,y = y,x if self.rank[x] == self.rank[y]: self.rank[x] += 1 self.par[y] = x self.X[x] += self.X[y] N,M,Q = MI() A = [[]] for i in range(N): A.append([0]+LI2()) Graph = [[] for _ in range(N+1)] for _ in range(M): u,v = MI() Graph[u].append(v) Graph[v].append(u) # 都市i,色jを 7*(i-1)+j に対応させる UF = UnionFind(7*N) for i in range(1,N+1): for j in range(1,8): w = 7*(i-1)+j if A[i][j] == 1: for k in Graph[i]: if A[k][j] == 1: UF.unite(w,7*(k-1)+j) for i in range(1,N+1): for j in range(1,7): if A[i][j] == A[i][j+1] == 1: UF.unite(7*(i-1)+j,7*(i-1)+j+1) if A[i][1] == A[i][7] == 1: UF.unite(7*(i-1)+1,7*(i-1)+7) for _ in range(Q): r,x,y = MI() if r == 1: A[x][y] = 1 w = 7*(x-1)+y if y > 1: if A[x][y-1] == 1: UF.unite(w,7*(x-1)+y-1) if y < 7: if A[x][y+1] == 1: UF.unite(w,7*(x-1)+y+1) if y == 1: if A[x][7] == 1: UF.unite(w,7*(x-1)+7) if y == 7: if A[x][1] == 1: UF.unite(w,7*(x-1)+1) for k in Graph[x]: if A[k][y] == 1: UF.unite(w,7*(k-1)+y) else: print(UF.X[UF.find(7*(x-1)+1)])