lib C fun strtoll(s : UInt8*, p : UInt8**, b : Int32) : Int64 end class String def to_i64 C.strtoll(self, nil, 10) end end class UnionFind @d : Array(Int32) def initialize(n : Int32) @d = Array.new(n, -1) end def root(x : Int32) @d[x] < 0 ? x : (@d[x] = root(@d[x])) end def unite(x : Int32, y : Int32) x = root(x) y = root(y) return false if x == y x, y = y, x if @d[x] > @d[y] @d[x] += @d[y] @d[y] = x true end def same?(x : Int32, y : Int32) root(x) == root(y) end def size(x : Int32) -@d[root(x)] end end n, m, q = read_line.split.map(&.to_i) uf = UnionFind.new(n * 7) s = (1..n).map { read_line.chars } (0...n).each do |i| (0...7).each do |j1| [1, -1].each do |k| j2 = (j1 + k) % 7 if s[i][j1] == '1' && s[i][j2] == '1' uf.unite(i * 7 + j1, i * 7 + j2) end end end end g = (1..n).map { [] of Int32 } m.times do |i| u, v = read_line.split.map(&.to_i.pred) g[u] << v g[v] << u (0...7).each do |j| if s[u][j] == '1' && s[v][j] == '1' uf.unite(u * 7 + j, v * 7 + j) end end end q.times do com, x, y = read_line.split.map(&.to_i.pred) if com == 0 s[x][y] = '1' g[x].each do |i| if s[i][y] == '1' uf.unite(x * 7 + y, i * 7 + y) end end (0...7).each do |j1| [1, -1].each do |k| j2 = (j1 + k) % 7 if s[x][j1] == '1' && s[x][j2] == '1' uf.unite(x * 7 + j1, x * 7 + j2) end end end elsif com == 1 puts uf.size(x * 7) end end