local n, m, q = io.read("*n", "*n", "*n", "*l") local parent = {} local canuse = {} local size = {} for i = 1, n * 7 do parent[i] = i canuse[i] = false size[i] = 1 end local function uf_findroot(idx) local idx_update = idx while parent[idx] ~= idx do idx = parent[idx] end while parent[idx_update] ~= idx do parent[idx_update], idx_update = idx, parent[idx_update] end return idx end local function uf_unite(a, b) local ra, rb = uf_findroot(a), uf_findroot(b) if ra ~= rb then size[ra] = size[ra] + size[rb] parent[rb], parent[b] = ra, ra end end local function encode(idx, color) return (idx - 1) * 7 + color end for i = 1, n do local s = io.read() for j = 1, 7 do if s:byte(j) == 49 then canuse[encode(i, j)] = true end end for j = 1, 6 do local z = encode(i, j) if canuse[z] and canuse[z + 1] then uf_unite(z, z + 1) end end do local y, z = encode(i, 1), encode(i, 7) if canuse[y] and canuse[z] then uf_unite(y, z) end end end local edge = {} for i = 1, n do edge[i] = {} end for i = 1, m do local a, b = io.read("*n", "*n") table.insert(edge[a], b) table.insert(edge[b], a) end for a = 1, n do for j = 1, #edge[a] do local b = edge[a][j] if a < b then for c = 1, 7 do local y, z = encode(a, c), encode(b, c) if canuse[y] and canuse[z] then uf_unite(y, z) end end end end end for iq = 1, q do local tp, a, c = io.read("*n", "*n", "*n") if tp == 1 then local z = encode(a, c) canuse[z] = true if 1 < c and canuse[z - 1] then uf_unite(z, z - 1) end if c < 7 and canuse[z + 1] then uf_unite(z, z + 1) end if c == 1 and canuse[z + 6] then uf_unite(z, z + 6) end if c == 7 and canuse[z - 6] then uf_unite(z, z - 6) end for j = 1, #edge[a] do local b = edge[a][j] local y = encode(b, c) if canuse[y] then uf_unite(z, y) end end else local r = uf_findroot(encode(a, 1)) print(size[r]) end end