A binary string is a string that consists of characters $0$ and $1$.

Let $\operatorname{MEX}$ of a binary string be the smallest digit among $0$, $1$, or $2$ that does not occur in the string. For example, $\operatorname{MEX}$ of $001011$ is $2$, because $0$ and $1$ occur in the string at least once, $\operatorname{MEX}$ of $1111$ is $0$, because $0$ and $2$ do not occur in the string and $0 < 2$.

A binary string $s$ is given. You should cut it into any number of substrings such that each character is in exactly one substring. It is possible to cut the string into a single substring — the whole string.

A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.

What is the minimal sum of $\operatorname{MEX}$ of all substrings pieces can be?


-----Input-----

The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Description of the test cases follows.

Each test case contains a single binary string $s$ ($1 \le |s| \le 10^5$).

It's guaranteed that the sum of lengths of $s$ over all test cases does not exceed $10^5$.


-----Output-----

For each test case print a single integer — the minimal sum of $\operatorname{MEX}$ of all substrings that it is possible to get by cutting $s$ optimally.


-----Examples-----

Input
6
01
1111
01100
101
0000
01010
Output
1
0
2
1
1
2


-----Note-----

In the first test case the minimal sum is $\operatorname{MEX}(0) + \operatorname{MEX}(1) = 1 + 0 = 1$.

In the second test case the minimal sum is $\operatorname{MEX}(1111) = 0$.

In the third test case the minimal sum is $\operatorname{MEX}(01100) = 2$.

Source: codeforces | Difficulty: EASY
Tags: Dynamic programming

Bit manipulation

Constructive algorithms

Greedy algorithms


Solutions:
t = int(input())
for test in range(t):
	s = input()
	zeroes = s.count('0')
	if zeroes == 0:
		print(0)
		continue
	first = s.find('0')
	last = s.rfind('0')
	if last - first + 1 == zeroes:
		print(1)
	else:
		print(2)


for _ in range(int(input())):
	s = input() + '1'
	c = 0
	for i in range(len(s) - 1):
		if s[i] == '0' and s[i + 1] == '1':
			c = c + 1
	print(min(c, 2))


import math

def int_input():
	a = int(input())
	return a

def int_list_input():
	a = list(map(int, input().split()))
	return a

def solve():
	s = input()
	i = 0
	n = len(s)
	ans = 0
	while i < n:
		j = i + 1
		while j < n and s[j] == s[i]:
			j += 1
		if s[i] == '0':
			ans += 1
		i = j
		if ans > 2:
			break
	ans = min(2, ans)
	print(ans)
for _ in range(int(input())):
	solve()


import sys, math

def solve():
	s = input()
	ans = 0
	for i in range(len(s)):
		if i != len(s) - 1 and s[i] == s[i + 1]:
			continue
		else:
			if s[i] == '0':
				ans = ans + 1
			if s[i] == '1':
				ans = ans + 0
	ans = min(2, ans)
	print(ans)
t = int(input())
while t != 0:
	solve()
	t -= 1


from collections import Counter, deque, defaultdict
from heapq import *
import math

def inp():
	return int(input())

def mapInput():
	return map(int, input().split())

def arrInput():
	return list(map(int, input().split()))

def task():
	s = input()
	c = defaultdict(int)
	n = len(s)
	for x in s:
		c[x] += 1
	if c['1'] == n:
		return 0
	if c['0'] == n:
		return 1
	zCount = 0
	for x in s:
		if x == '0':
			zCount += 1
		elif zCount > 0:
			if zCount == c['0']:
				return 1
			return 2
	return 1
for _ in range(inp()):
	print(task())


for _ in range(int(input())):
	Str = input()
	Zero = Str.count('0')
	if Zero == 0:
		print(Zero)
	else:
		St = Zero * '0'
		if St in Str:
			print(1)
		else:
			print(2)


import copy
import math
fin = None
fout = None
uabet = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
abet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
INF = 10 ** 18

def start(filename):
	filename = filename.replace('.py', '')
	global fin
	global fout
	infile = filename + '.in'
	outfile = filename + '.out'
	fin = open(infile)
	fout = open(outfile, 'w')

def permutation(lst):
	if len(lst) == 0:
		return []
	if len(lst) == 1:
		return [lst]
	l = []
	for i in range(len(lst)):
		m = lst[i]
		remLst = lst[:i] + lst[i + 1:]
		for p in permutation(remLst):
			l.append([m] + p)
	return l

def subsets(x):
	retset = []
	if len(x) == 0:
		retset = [[]]
	else:
		first = x[-1]
		cop = x.copy()
		cop.remove(first)
		y = subsets(cop)
		retset.extend(copy.deepcopy(y))
		m = copy.deepcopy(y)
		for i in range(len(y)):
			k = m[0]
			m.remove(k)
			k.append(first)
			m.append(k)
		retset.extend(m.copy())
	return retset

class Point:

	def __init__(self, x, y):
		self.x = x
		self.y = y

	def __str__(self):
		return str(self.x) + str(self.y)

	def __hash__(self):
		return hash(str(self))

	def __eq__(self, other):
		return self.x == other.x and self.y == other.y

class frac:

	def __init__(self, n, d):
		self.n = n
		self.d = d

	def __str__(self):
		return str(self.n) + '/' + str(self.d)

	def __hash__(self):
		return hash(str(self))

	def simp(self):
		div = math.gcd(self.n, self.d)
		self.n = self.n / div
		self.d = self.d / div

	def __eq__(self, other):
		return self.n / self.d == other.n / other.d

	def __lt__(self, other):
		return self.n / self.d < other.n / other.d

class Interval:

	def __init__(self, start, end):
		self.start = start
		self.end = end

	def __str__(self):
		return str(self.start) + str(self.end)

	def __hash__(self):
		return hash(str(self))

	def __eq__(self, other):
		return self.start == other.start and self.end == other.end

	def __lt__(self, other):
		return self.start < other.start

def overlap(verl1, verl2):
	if verl1.end >= verl2.start:
		return True
	else:
		return False

def merge(verl1, verl2):
	retverl = Interval(min(verl1.start, verl2.start), max(verl1.end, verl2.end))
	return retverl

def changeBase(num, base):
	if base == 2:
		return '{0:b}'.format(num)
	power = math.floor(math.log(num, base))
	remainder = num
	end = []
	iter = list(range(power + 1))
	iter.reverse()
	for i in iter:
		if remainder >= base ** i:
			(q, r) = divmod(remainder, base ** i)
			end.append(q)
			remainder = r
		else:
			end.append(0)
	retlist = []
	for i in end:
		if i > 9:
			retlist.append(uabet[i - 10])
		else:
			retlist.append(i)
	retlist = map(str, retlist)
	return ''.join(retlist)

def prime(num):
	if num == 2:
		return True
	if num % 2 == 0:
		return False
	for i in range(2, math.ceil(num ** 0.5) + 1):
		if num % i == 0:
			return False
	return True

def solve(s):
	z = s.count('0')
	o = s.count('1')
	if z == 0:
		return 0
	if o == 0:
		return 1
	x = s.split('1')
	c = 0
	for i in x:
		if i != '':
			c += 1
	if c < 2:
		return c
	return 2

def main():
	t = int(input())
	for i in range(t):
		s = input()
		print(solve(s))
main()


for _ in range(int(input())):
	s = input()
	zeros = s.count('0')
	if zeros == 0:
		print(0)
		continue
	ones = s.count('1')
	if ones == 0:
		print(1)
		continue
	first_occurence = s.find('0')
	last_occurence = s.rfind('0')
	if last_occurence - first_occurence + 1 == zeros:
		print(1)
	else:
		print(2)


for case in range(int(input())):
	string = [int(x) for x in input()]
	if not 1 in string:
		print(1)
	elif not 0 in string:
		print(0)
	else:
		ohone = False
		oneoh = False
		for i in range(len(string) - 1):
			if ohone and oneoh:
				break
			if string[i] == 0 and string[i + 1] == 1:
				ohone = True
			elif string[i] == 1 and string[i + 1] == 0 and ohone:
				oneoh = True
		if oneoh and ohone:
			print(2)
		else:
			print(1)


t = int(input(''))
result_holder = list()
for i in range(t):
	temp = input('')
	first_zero = temp.find('0')
	last_zero = temp.rfind('0')
	if temp.count('0') == 0:
		result_holder.append(0)
	elif last_zero + 1 - first_zero == temp.count('0'):
		result_holder.append(1)
	else:
		result_holder.append(2)
for res in result_holder:
	print(res)


MEX = lambda s: 0 if '0' not in s else 1 if '1' not in s else 2 if '2' not in s else 3
for s in [*open(0)][1:]:
	S = 0
	is_zero = False
	for c in s.strip():
		if c == '0':
			if is_zero:
				continue
			else:
				is_zero = True
		elif is_zero:
			S += 1
			is_zero = False
		else:
			continue
	if is_zero:
		S += 1
	print(min(S, MEX(s)))


t = int(input())
for j in range(t):
	s = input()
	(n0, n1) = (0, 0)
	ans = 0
	f = 0
	for i in range(len(s)):
		if s[i] == '0':
			n0 += 1
		elif s[i] == '1':
			n1 += 1
		if s[i] == '0':
			if i == 0:
				if len(s) == 1:
					ans += 1
				elif s[i + 1] != '0':
					ans += 1
			else:
				if s[i - 1] != '0':
					ans += 1
					f = 1
				if i != len(s) - 1:
					if s[i + 1] != '0':
						if f == 0:
							ans += 1
						else:
							f = 0
	if n1 == 0:
		ans = 1
	else:
		ans = min(ans, 2)
	print(ans)


result = []
for _ in range(int(input())):
	s = input()
	if '0' not in s:
		result.append(0)
	elif '1' not in s:
		result.append(1)
	else:
		if s[0] == '0':
			cnt = 0
		else:
			cnt = -1
		for i in range(len(s)):
			if i != len(s) - 1:
				if s[i] != s[i + 1]:
					cnt += 1
		if cnt > 1:
			result.append(2)
		else:
			result.append(1)
print(*result, sep='\n')


t = int(input())
while t:
	t -= 1
	s = input()
	a = s.replace('1', ' ').split().__len__()
	print(min(a, 2))


t = int(input())
for _ in range(t):
	s = input()
	flag = True
	ans = 0
	for char in s:
		if char == '1':
			flag = True
		elif char == '0' and flag == True:
			ans += 1
			flag = False
		else:
			continue
	if ans > 2:
		ans = 2
	print(ans)


t = int(input())
answer = []
for i in range(0, t):
	n = input()
	arr = [char for char in n]
	cnt = 0
	i = 0
	cnt1 = 0
	while i < len(arr):
		if arr[i] == '0':
			cnt = cnt + 1
			i = i + 1
			if i != len(arr):
				while arr[i] == '0' and i != len(arr):
					i = i + 1
					if i == len(arr):
						break
		elif arr[i] == '1':
			cnt1 = cnt1 + 1
			i = i + 1
	if cnt == 0:
		sum = 0
	elif cnt == 1:
		sum = 1
	else:
		sum = 2
	answer.append(sum)
for j in range(0, len(answer)):
	print(answer[j])


for _ in [0] * int(input()):
	s = input()
	k = len(s)
	if s == '1' * k:
		print(0)
	else:
		for i in range(k):
			if s[i] == '0':
				a = i
				break
		for j in range(k - 1, -1, -1):
			if s[j] == '0':
				b = j
				break
		print(2 - (s[a:b + 1] == '0' * (b - a + 1)))


import sys

def move(string, i):
	val = string[i]
	size = len(string)
	while True:
		i += 1
		if i == size:
			break
		if val != string[i]:
			break
	return i
inp = sys.stdin.readline()
test = int(inp.strip('\n'))
result = ''
for i in range(test):
	inp = sys.stdin.readline()
	string = inp.strip('\n')
	size = len(string)
	z0 = z1 = o0 = o1 = i = 0
	while True:
		if string[i] == '0':
			if z0:
				z1 = 1
			z0 = 1
			i = move(string, i)
		elif string[i] == '1':
			if o0:
				o1 = 1
			o0 = 1
			i = move(string, i)
		if size == i:
			break
		if z1 and o1:
			break
	if z1 == 0 and o1 == 0:
		if z0 == 0:
			sys.stdout.write('0\n')
		elif o0 == 0:
			sys.stdout.write('1\n')
		else:
			sys.stdout.write('1\n')
	elif z1 == 1 and o1 == 0:
		sys.stdout.write('2\n')
	elif z1 == 0 and o1 == 1:
		sys.stdout.write('1\n')
	else:
		sys.stdout.write('2\n')


def solution():
	for t in range(int(input())):
		s = input()
		ans = 0
		fizero = s.find('0')
		if fizero != -1:
			ans += 1
			fione = s[fizero:].find('1') + fizero
			if fione - fizero != -1:
				sezero = s[fione:].find('0') + fione
				if sezero - fione != -1:
					ans += 1
		print(ans)
	return
solution()


import sys

def mix_mex(s):
	zero_count = 0
	last = None
	for ch in s:
		if ch == '0':
			if last == '0':
				continue
			zero_count += 1
		last = ch
	if zero_count == 0:
		return 0
	if zero_count == 1:
		return 1
	return 2
for _ in range(int(input())):
	s = input()
	sys.stdout.write(f'{mix_mex(s)}\n')


n = int(input())
for i in range(n):
	s = input()
	zero = s.count('0')
	if zero == 0:
		print(0)
		continue
	first = s.find('0')
	last = s.rfind('0')
	if last - first + 1 == zero:
		print(1)
	else:
		print(2)


import sys
import bisect
from bisect import bisect_left as lb
from bisect import bisect_right as rb
input_ = lambda : sys.stdin.readline().strip('\r\n')
from math import log
from math import gcd
from math import atan2, acos
from random import randint
sa = lambda : input_()
sb = lambda : int(input_())
sc = lambda : input_().split()
sd = lambda : list(map(int, input_().split()))
sflo = lambda : list(map(float, input_().split()))
se = lambda : float(input_())
sf = lambda : list(input_())
flsh = lambda : sys.stdout.flush()
mod = 10 ** 9 + 7
mod1 = 998244353
gp = []
cost = []
dp = []
mx = []
ans1 = []
ans2 = []
special = []
specnode = []
a = 0
kthpar = []

def dfs2(root, par):
	if par != -1:
		dp[root] = dp[par] + 1
	for i in range(1, 20):
		if kthpar[root][i - 1] != -1:
			kthpar[root][i] = kthpar[kthpar[root][i - 1]][i - 1]
	for child in gp[root]:
		if child == par:
			continue
		kthpar[child][0] = root
		dfs(child, root)
ans = 0
b = []
vis = []
tot = 0
time = []
time1 = []
adj = []
mx = -1
eps = 1e-07

def hnbhai(tc):
	s = sa()
	x = s.split('1')
	tot = 0
	for i in x:
		if len(i) < 1:
			continue
		tot += 1
	print(min(tot, 2))
for _ in range(sb()):
	hnbhai(_ + 1)


for _ in range(int(input())):
	s = input()
	if '1' not in s:
		print(1)
	elif '0' not in s:
		print(0)
	else:
		zflag = False
		oflag = False
		flag = False
		for i in s:
			if zflag and oflag and (i == '0'):
				flag = True
				break
			if i == '0':
				zflag = True
			if zflag and i == '1':
				oflag = True
		if flag:
			print(2)
		else:
			print(1)


t = int(input())
while t > 0:
	ans = 0
	s = list(input())
	n = len(s)
	c = s[0]
	for i in s:
		if i != c:
			if c == '0':
				ans = ans + 1
			c = i
	if c == '0':
		ans = ans + 1
	if ans > 2:
		print(2)
	else:
		print(ans)
	t = t - 1


t = int(input())
for _ in range(t):
	s = input() + '1'
	d = 0
	k = 0
	flag = 0
	for val in s:
		if val == '0':
			d = 1
		elif d == 1:
			k += 1
			d = 0
			if k > 1:
				flag = 1
				break
	if flag == 1:
		print(2)
	else:
		print(k)


n = int(input())
for i in range(n):
	t = input()
	seenZero = False
	seenOneAfterZero = False
	seenZOZ = False
	for j in range(len(t)):
		if t[j] == '0':
			seenZero = True
			if seenOneAfterZero == True:
				seenZOZ = True
				break
		elif seenZero == True:
			seenOneAfterZero = True
	if seenZero == False:
		print(0)
	elif seenZOZ == False:
		print(1)
	else:
		print(2)


def main():
	s = input()
	zero = s.count('0')
	ones = s.count('1')
	if zero == 0:
		print(0)
		return
	n = len(s)
	i = 1
	k = 0
	p = 1
	for i in range(n):
		if p == 1 and s[i] == '0':
			k += 1
			p = 0
		if p == 0 and s[i] == '1':
			p = 1
	print(min(k, 2))
for _ in range(int(input())):
	main()


mod = 1000000007
from math import factorial, ceil, pow, sqrt, floor
from sys import stdin, stdout
from collections import defaultdict, Counter, deque
for _ in range(int(stdin.readline())):
	s = stdin.readline().strip('\n')
	if s.find('0') == -1:
		print(0)
	elif s.find('1') == -1:
		print(1)
	else:
		li = s.split('1')
		c = 0
		for ele in li:
			if ele != '':
				c += 1
		print(min(c, 2))


import os
import sys
from io import BytesIO, IOBase

def main():
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()
T = int(input())
for _ in range(T):
	s = list(input())
	n = len(s)
	ans = 0
	flag = 2
	for i in range(n):
		if s[i] == '0':
			if flag != 0:
				ans = ans + 1
			flag = 0
		else:
			flag = 1
	print(min(ans, 2))


import sys
first = True
for line in sys.stdin:
	if first:
		first = False
		continue
	line = line.rstrip()
	if '1' not in line:
		print('1')
	elif '0' not in line:
		print('0')
	else:
		substrs = line.split('1')
		count_of_zero = 0
		for substr in substrs:
			if '0' in substr:
				count_of_zero += 1
		if count_of_zero >= 2:
			print('2')
		else:
			print('1')


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	s = input().strip()
	ans = 0
	curr = int(s[0])
	for i in s[1:]:
		i = int(i)
		if i == 1:
			if curr == 0:
				ans += 1
			curr = 1
		else:
			if curr == 1:
				ans += 0
			curr = 0
	ans += 1 - curr

	def mex(s):
		if '0' in s and '1' in s:
			return 2
		elif '0' in s:
			return 1
		else:
			return 0
	print(min(ans, mex(s)))


from sys import stdin
for testcase in range(int(stdin.readline())):
	binary_string = stdin.readline().strip()
	last0Index = first1Index = -1
	for index in range(len(binary_string)):
		if binary_string[index] == '0':
			last0Index = index
		elif first1Index == -1 and binary_string[index] == '1':
			first1Index = index
	zeroGroupsCount = 0
	index = 0
	while index < len(binary_string):
		while index < len(binary_string) - 1 and binary_string[index] == binary_string[index + 1] == '0':
			index += 1
		zeroGroupsCount += binary_string[index] == '0'
		index += 1
	print(min(zeroGroupsCount, 2))


for _ in range(int(input())):
	s = input().strip('1')
	print(('1' in s) + bool(s))


t = int(input())
for i in range(t):
	s = input()
	a = list(s.split('1'))
	c = 0
	for i in a:
		if i != '':
			c += 1
	if c >= 2:
		print('2')
	elif c == 1:
		print('1')
	else:
		print(0)


import sys
input = sys.stdin.readline

def process(s):
	res = [s[0]]
	for i in range(1, len(s)):
		if s[i] != res[-1]:
			res.append(s[i])
	if res[-1] == '1':
		res.pop()
	if res and res[0] == '1':
		res = res[1:]
	if not res:
		return 0
	if len(res) == 1:
		return 1
	return 2
t = int(input())
for _ in range(t):
	s = input().rstrip()
	print(process(s))


for _ in range(int(input())):
	s = input()
	count = 0
	if s[0] == '0':
		count += 1
	for i in range(1, len(s)):
		if s[i] != s[i - 1]:
			if s[i] == '0':
				count += 1
			if count == 2:
				break
	print(count)


import sys
input = sys.stdin.readline

def solve():
	s = input().strip()
	n = len(s)
	i = s.find('0')
	if i == -1:
		print(0)
		return
	if s.find('1') == -1:
		print(1)
		return
	j = s.find('1', i)
	if j == -1:
		print(1)
		return
	if s.find('0', j) == -1:
		print(1)
		return
	print(2)
for i in range(int(input())):
	solve()


def main():
	test = int(input())
	for _ in range(test):
		s = input().strip('\r\n')
		n = len(s)
		if n == 1:
			print(1 - int(s[0]))
			continue
		last = s[0]
		ans = 0
		for i in range(1, n):
			if s[i] != last:
				ans += 1 - int(last)
				last = s[i]
			if i == n - 1:
				ans += 1 - int(s[i])
		print(min(ans, 2))
	return
main()


import sys
import getpass
import math, random
import functools, itertools, collections, heapq, bisect
from collections import Counter, defaultdict, deque
input = sys.stdin.readline
M9 = 10 ** 9 + 7
(yes, no) = ('YES', 'NO')
MAXINT = sys.maxsize
OFFLINE_TEST = getpass.getuser() == 'hkmac'

def log(*args):
	if OFFLINE_TEST:
		print('\x1b[36m', *args, '\x1b[0m', file=sys.stderr)

def solve(*args):
	if OFFLINE_TEST:
		log('----- solving ------')
		log(*args)
		log('----- ------- ------')
	return solve_(*args)

def read_matrix(rows):
	return [list(map(int, input().split())) for _ in range(rows)]

def read_strings(rows):
	return [input().strip() for _ in range(rows)]

def minus_one(arr):
	return [x - 1 for x in arr]

def minus_one_matrix(mrr):
	return [[x - 1 for x in row] for row in mrr]

def solve_(srr):
	if '0' not in srr:
		return 0
	if '1' not in srr:
		return 1
	if ('01' in srr) + ('10' in srr) == 1:
		return 1
	if srr.count('10') == 1 and srr.count('01') == 1 and (srr.index('10') < srr.index('01')):
		return 1
	return 2
for case_num in range(int(input())):
	srr = input().strip()
	res = solve(srr)
	print(res)


t = int(input())
for _ in range(t):
	s = input()
	var = 0
	bool = False
	i = 0
	while i < len(s):
		if s[i] == '0':
			while i < len(s) and s[i] == '0':
				i = i + 1
			var = var + 1
		else:
			while i < len(s) and s[i] == '1':
				i = i + 1
	if var >= 2:
		print('2')
	else:
		print(var)


for _ in range(int(input())):
	s = list(input().strip())
	n = len(s)
	a = []
	for i in range(1, n):
		if s[i] != s[i - 1]:
			a.append(s[i - 1])
	a.append(s[-1])
	print(min(2, a.count('0')))


import sys
import math
count = int(sys.stdin.readline())
while count > 0:
	count = count - 1
	line = sys.stdin.readline()
	currnetElement = line[0]
	sections = 0
	if currnetElement == '0':
		sections += 1
	for element in line:
		if element != currnetElement:
			if element == '0':
				sections += 1
			currnetElement = element
	print(min(sections, 2))


import sys
input = sys.stdin.readline
from collections import deque
from itertools import combinations
t = int(input())
for i in range(t):
	s = input().strip()
	lst = [s[0]]
	for j in range(1, len(s)):
		if s[j] != s[j - 1]:
			lst.append(s[j])
	if len(lst) == 1:
		if lst[0] == '1':
			print(0)
		else:
			print(1)
	else:
		ans = 0
		for j in range(len(lst)):
			if lst[j] == '0':
				ans += 1
		print(min(ans, 2))


for x in range(int(input())):
	s = input()
	if s.count('0') == len(s):
		print(1)
	elif s.count('1') != len(s):
		for i in range(len(s)):
			if s[i] != '1':
				if s[i:].count('0') > 1:
					if '10' in s[i:]:
						print(2)
						break
					else:
						print(1)
						break
				else:
					print(1)
					break
	else:
		print(0)


number = int(input())
ans = []
for i in range(number):
	value = []
	string = input()
	if len(string) < 2:
		value.append(string[0])
	else:
		for j in range(len(string) - 1):
			if string[j] != string[j + 1]:
				value.append(string[j])
			if j + 1 == len(string) - 1:
				value.append(string[j + 1])
	count = 0
	lst1 = ['0', '1', '2']
	for i in value:
		if i in lst1:
			lst1.remove(i)
		if i == '0':
			count += 1
	if count <= int(lst1[0]):
		ans.append(count)
	else:
		ans.append(lst1[0])
for i in ans:
	print(i)


import sys
try:
	sys.stdin = open('inputf.in', 'r')
	sys.stdout = open('outputf.in', 'w')
except:
	pass
import math
import sys
from math import sqrt
from collections import defaultdict
from collections import Counter
import string
from collections import deque
from functools import cmp_to_key
import bisect
from collections import OrderedDict
import heapq as hq
from queue import PriorityQueue
MOD = 10 ** 9 + 7

def ii():
	return int(sys.stdin.readline().strip())

def si():
	return sys.stdin.readline().strip()

def li():
	return list(map(int, sys.stdin.readline().strip().split()))

def lsi():
	return list(map(str, sys.stdin.readline().strip().split()))

def mi():
	return map(int, sys.stdin.readline().strip().split())

def dparr(m, n):
	return [[0] * n for i in range(m)]
t = ii()
for _ in range(t):
	s = si()
	sl = [s[0]]
	for i in range(1, len(s)):
		if s[i] != sl[-1]:
			sl.append(s[i])
	z = sl.count('0')
	if z == len(sl):
		print(1)
	elif z == 0:
		print(0)
	elif z >= 2:
		print(2)
	else:
		print(1)


l = [0, 1, 2]
p = []
for _ in range(int(input())):
	s = input()
	(c, k) = (0, 0)
	for i in range(len(s)):
		if s[i] == '0':
			c = 1
		elif s[i] == '1' and c == 1:
			k += 1
			c = 0
	if s[len(s) - 1] == '0':
		k += 1
	print(min(k, 2))


for _ in range(int(input())):
	s = input()
	i = 0
	ct = 0
	if '1' not in s:
		print(1)
	elif '0' not in s:
		print(0)
	else:
		while i < len(s):
			if s[i] == '0':
				while i < len(s) and s[i] == '0':
					i += 1
				ct += 1
			else:
				i += 1
		print(min(2, ct))


t = int(input())
for tt in range(t):
	s = input()
	znum = s.count('0')
	if znum == 0:
		print(0)
		continue
	first = s.find('0')
	last = s.rfind('0')
	if last - first + 1 == znum:
		print(1)
	else:
		print(2)


for _ in range(int(input())):
	s = input()
	if '0' not in s:
		print(0)
	else:
		start = s.find('0')
		end = s.rfind('0')
		if end - start + 1 == s.count('0'):
			print(1)
		else:
			print(2)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
sys.setrecursionlimit(100000000)

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from collections import defaultdict, deque
from math import ceil, floor, sqrt, log2, gcd, pi
from heapq import heappush, heappop
from fractions import Fraction
from bisect import bisect_left, bisect
import sys
abc = 'abcdefghijklmnopqrstuvwxyz'
ABC = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
for t in range(int(input())):
	s = input()
	one = s.count('1')
	zero = s.count('0')
	if zero == 0:
		print(0)
	elif one == 0:
		print(1)
	elif one != 0 and zero != 0:
		arr = s.split('1')
		cnt = 0
		for i in arr:
			if '0' in i:
				cnt += 1
		print(min(cnt, 2))


def function():
	count = 0
	rcount = 0
	s = input()
	for i in range(len(s)):
		if s[i] == '0':
			count += 1
		elif s[i] != '0' and count != 0:
			count = 0
			rcount += 1
	if count != 0:
		count = 0
		rcount += 1
	if rcount > 2:
		print(2)
	else:
		print(rcount)
i = input()
for j in range(int(i)):
	function()


for j in range(int(input())):
	s = input()
	sum = 0
	if '1' not in s:
		print(1)
		continue
	for i in range(len(s) - 1):
		if s[i] == '0' and s[i + 1] == '1':
			sum += 1
	if s[-1] == '0':
		sum += 1
	print(min(2, sum))


import sys
import math
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def inltstr():
	return list(map(str, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def factorial(n):
	if n == 1:
		return n
	elif n < 1:
		return 'NA'
	else:
		return n * factorial(n - 1)

def coeffBin(k):
	return factorial(k + 1) / (factorial(2) * factorial(k - 1))

def solve(s):
	count = 0
	flag = False
	for c in s:
		if c == '0' and flag == False:
			flag = True
		elif c == '1' and flag == True:
			count += 1
			flag = False
	if flag:
		count += 1
	if count == 1:
		return 1
	if count == 0:
		return 0
	if count > 1:
		return 2
	return 0
res = []
iter = inp()
i = 0
while i < iter:
	arr = inltstr()
	case = ''
	for n in arr:
		case += str(n)
	result = solve(case)
	res.append(result)
	i += 1
for r in res:
	print(r)


import sys
input = sys.stdin.readline
t = int(input())
while t > 0:
	s = input()
	z = s.count('0')
	if z == 0:
		print(0)
	else:
		f = s.find('0')
		l = s.rfind('0')
		if l - f + 1 == z:
			print(1)
		else:
			print(2)
	t -= 1


def MEX(s):
	if len(s) == 0:
		return 0
	current = s[0]
	number_of_ones = 0
	number_of_zeros = 0
	if current == '0':
		number_of_zeros = 1
	else:
		number_of_ones = 1
	for k in s:
		if k != current:
			current = k
			if k == '0':
				number_of_zeros += 1
			else:
				number_of_ones += 1
	if number_of_zeros >= 2:
		return 2
	elif number_of_zeros == 0:
		return 0
	else:
		return 1
tests = int(input())
for i in range(tests):
	s = input()
	print(MEX(s))


for _ in range(int(input())):
	s = input().split('1')
	print(min(len([i for i in s if i != '']), 2))


for _ in range(int(input())):
	s = input().split('1')
	b = 0
	for i in s:
		if i != '':
			b += 1
	print(min(b, 2))


for _ in range(int(input())):
	s = input()
	if set(s) == {'1'}:
		print('0')
	else:
		a = list(s.split('1'))
		a = [i for i in a if i]
		if len(a) == 1:
			print('1')
		else:
			print('2')


t = int(input())
for i in range(t):
	s = input()
	k = 0
	k1 = 0
	k0 = 0
	for j in range(1, len(s)):
		if s[j] != s[j - 1]:
			k = k + 1
		if s[j] == '1':
			k1 += 1
		else:
			k0 += 1
	if s[0] == '1':
		k1 += 1
	else:
		k0 += 1
	if k0 == 0:
		print(0)
	elif k1 == 0:
		print(1)
	elif s[0] == '1' and k > 2 or (s[0] == '0' and k > 1):
		print(2)
	else:
		print(1)


for _ in range(int(input())):
	s = input()
	n = len(s)
	zs = s.count('0')
	if n == zs:
		print(1)
		continue
	if zs == 0:
		print(0)
		continue
	li = s.find('0')
	ri = s.rfind('0')
	if ri - li + 1 == zs:
		print(1)
	else:
		print(2)


for _ in range(int(input())):
	s = input()
	(group_0, group_1) = (0, 0)
	x = 0
	while x < len(s):
		if s[x] == '0':
			group_0 += 1
			while x < len(s) and s[x] == '0':
				x += 1
		else:
			group_1 += 1
			while x < len(s) and s[x] == '1':
				x += 1
	if group_0 >= 2:
		print(2)
	elif group_0 == 1:
		print(1)
	else:
		print(0)


import math
import sys
import collections
import bisect
import time
import random
import itertools
import string
from collections import Counter, defaultdict

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_string():
	return sys.stdin.readline().strip()
for t in range(int(input())):
	s = get_string()
	n = len(s)
	if s == '1' * n:
		print(0)
		continue
	elif s == '0' * n:
		print(1)
	else:
		s = list(s)
		prev = s[0]
		ans = 0
		for i in s:
			if i == '1':
				if prev == i:
					continue
				else:
					ans += 1
				prev = i
				continue
			else:
				if i == prev:
					continue
				prev = i
		if prev == '0':
			ans += 1
		ans = min(ans, 2)
		print(ans)


t = int(input())
out = list()
for _ in range(t):
	s = input()
	first_zero_block = True
	ans = -1
	for (i, c) in enumerate(s):
		if c == '0':
			if first_zero_block:
				first_zero_block = False
			elif s[i - 1] != c:
				ans = 2
				break
	if first_zero_block:
		ans = 0
	elif ans == -1:
		ans = 1
	out.append(str(ans))
print('\n'.join(out))


for _ in range(int(input())):
	s = input()
	if s.count('0') == 0:
		print(0)
	elif s.count('1') == 0:
		print(1)
	elif '10' not in s[s.index('0'):]:
		print(1)
	else:
		print(2)


t = int(input())
for _ in range(t):
	s = list(input())
	count = 0
	a = s[0]
	if a == '0':
		count += 1
	for e in s:
		if count >= 2:
			count = 2
			break
		if e != a:
			a = e
			if e == '0':
				count += 1
	print(count)


t = int(input())
while t:
	s = input().split('1')
	li = list(filter(None, s))
	if len(li) >= 3:
		print(2)
	else:
		print(len(li))
	t -= 1


import math
t = int(input())
cases = []
for i in range(t):
	cases.append(input())

def answer(s):
	count = 0
	nocount = False
	for i in s:
		if i == '0' and nocount == False:
			count += 1
			nocount = True
		elif i == '0':
			continue
		else:
			nocount = False
	if count == 0:
		return 0
	if count >= 2:
		return 2
	return 1
for i in cases:
	print(answer(i))


def solve(s):
	L = s.split('1')
	return min(2, sum((1 for x in L if x)))
tests = int(input())
for _ in range(tests):
	print(solve(input()))


res = ''
for _ in range(int(input())):
	s = input()
	prev = s[0]
	blocks_zero = prev == '0'
	for i in s[1:]:
		if i != prev and prev == '1':
			blocks_zero += 1
		if blocks_zero == 2:
			break
		prev = i
	res += '%d\n' % min(blocks_zero, 2)
print(res)


import sys

def input():
	return sys.stdin.readline()[:-1]

def getInt():
	return int(input())

def getIntIter():
	return map(int, input().split())

def getIntList():
	return list(getIntIter())

def flush():
	sys.stdout.flush()
for _ in range(getInt()):
	s = input()
	zeros = int(s[0] == '0')
	last_char = s[0]
	for char in s:
		if char != last_char and char == '0':
			zeros += 1
		last_char = char
	if zeros < 2:
		print(int(zeros))
	else:
		print(2)


import math
import heapq
import string
from collections import deque
from bisect import bisect, bisect_left, bisect_right, insort
import sys
input = sys.stdin.readline
S = lambda : input().rstrip()
I = lambda : int(S())
M = lambda : map(int, S().split())
L = lambda : list(M())
H = 1000000000 + 7
for _ in range(I()):
	s = S()
	n = len(s)
	if s == '0' * n:
		print(1)
	elif s == '1' * n:
		print(0)
	else:
		i = s.find('0')
		s1 = s[i + 1:]
		j = s1.find('1')
		if j == -1:
			print(1)
			continue
		s2 = s1[j + 1:]
		k = s2.find('0')
		if k != -1:
			print(2)
		else:
			print(1)


t = int(input())
for tt in range(t):
	s = input()
	grp0 = 0
	for i in range(len(s)):
		if i == 0 and s[i] == '0':
			grp0 += 1
		elif s[i] == '0' and s[i - 1] == '1':
			grp0 += 1
	print(grp0 if grp0 <= 2 else 2)


for _ in range(int(input())):
	s = input()
	if s.count('0') == 0:
		print(0)
	else:
		tmp = s[0]
		if s[0] == '0':
			cnt = 1
		else:
			cnt = 0
		for i in range(1, len(s)):
			if s[i] != tmp:
				tmp = s[i]
				if s[i] == '0':
					cnt += 1
		if cnt > 1:
			print(2)
		else:
			print(1)


from sys import stdin, stdout
t = int(stdin.readline())
while t > 0:
	str = stdin.readline()
	c = 0
	if str[0] == '0':
		c += 1
	for i in range(1, len(str)):
		if str[i] == '0' and str[i - 1] == '1':
			c = c + 1
	if c == 0:
		print('0')
	elif c == 1:
		print('1')
	elif c == len(str) - 1:
		print('1')
	else:
		print('2')
	t = t - 1


t = int(input())
while t:
	t -= 1
	s = input()
	start = False
	ans = 0
	for i in range(len(s)):
		if start == False:
			if s[i] == '0':
				start = True
		elif s[i] == '1':
			start = False
			ans += 1
	if start:
		ans += 1
	print(min(2, ans))


from collections import *
import math
from sys import *
import bisect
input = stdin.readline

def inp():
	return map(int, input().split())

def print_l(a):
	return stdout.write(' '.join(map(str, a)) + '\n')

def print_s(a):
	return stdout.write(str(a) + '\n')
for _ in range(int(input())):
	s = input()
	c = 0
	k = 0
	for i in range(len(s)):
		if s[i] == '0':
			c = 1
		elif s[i] != '0' and c == 1:
			k += 1
			c = 0
	if s[len(s) - 1] == '0':
		k += 1
	print(min(k, 2))


def zero_set(string):
	zero = 0
	x = False
	for i in range(len(string)):
		if string[i] == '0':
			if x:
				continue
			elif x == False:
				x = True
				zero = zero + 1
		else:
			x = False
	return zero

def mex(string):
	if '1' not in string:
		print(1)
	elif '0' not in string:
		print(0)
	else:
		z = zero_set(string)
		if z > 1:
			print(2)
		elif z < 2:
			print(1)
t = int(input())
str_list = []
for i in range(t):
	string = input()
	str_list.append(string)
for i in str_list:
	mex(i)


import sys
import math
from bisect import bisect_right as br

def int_arr():
	return list(map(int, sys.stdin.readline().split()))

def str_arr():
	return list(map(str, sys.stdin.readline().split()))

def input():
	return sys.stdin.readline().strip()
for _ in range(int(input())):
	st = input().split('1')
	ch = 0
	for i in st:
		if i != '':
			ch += 1
	print(min(ch, 2))


for _ in range(int(input())):
	n = input()
	p = 0
	s = n[0]
	for i in range(len(n)):
		if n[p] != n[i]:
			s += n[i]
		if s == '010' or s == '1010':
			print(2)
			break
		p = i
	else:
		if s == '01' or s == '101' or s == '0' or (s == '10'):
			print(1)
		else:
			print(0)


k = int(input())
for i in range(k):
	q = input()
	w = 0
	if q[0] == '0':
		w = q.find('1')
		if w != -1:
			q = q[w:len(q)]
			w = q.find('0')
			if w != -1:
				print(2)
			else:
				print(1)
		else:
			print(1)
	elif q[0] == '1':
		w = q.find('0')
		if w != -1:
			q = q[w:len(q)]
			w = q.find('1')
			if w != -1:
				q = q[w:len(q)]
				w = q.find('0')
				if w != -1:
					print(2)
				else:
					print(1)
			else:
				print(1)
		else:
			print(0)


for _ in range(int(input())):
	s = input()
	if s.count('0') == 0:
		print(0)
		continue
	if s.count('1') == 0:
		print(1)
		continue
	last = s.rfind('0')
	first = s.find('0')
	if last - first + 1 == s.count('0'):
		print(1)
	else:
		print(2)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	s = input()
	c = 0
	slen = len(s)
	while c < slen:
		if s[c] == '0':
			break
		c += 1
	if c == slen:
		print(0)
		continue
	while c < slen:
		if s[c] == '1':
			break
		c += 1
	if c == slen:
		print(1)
		continue
	while c < slen:
		if s[c] == '0':
			break
		c += 1
	if c == slen:
		print(1)
		continue
	else:
		print(2)


t = int(input())
for _ in range(t):
	(s, j, u) = (input(), 0, 0)
	n = len(s)
	while j < n:
		c = j
		while j < n and s[j] == '0':
			j += 1
		if j > c:
			u += 1
		j += 1
	h = set(s)
	if h == {'1'}:
		print(0)
	elif h == {'0'}:
		print(1)
	else:
		print(min(2, u))


for x in range(int(input())):
	s = input()
	a = s.find('0')
	if a < 0:
		print(0)
	elif '10' in s[a:]:
		print(2)
	else:
		print(1)


for __ in range(int(input())):
	s = str(input())
	(status, count) = (0, 0)
	for i in s:
		if i == '0':
			status = 1
		else:
			if status:
				count += 1
			status = 0
	if status:
		count += 1
	if count > 2:
		count = 2
	print(count)


t = int(input())

def F(a):
	schet = 0
	for i in range(len(a)):
		if a[i] == '0' and i == 0:
			schet += 1
		elif a[i] == '0':
			if a[i] != a[i - 1]:
				schet += 1
	if schet == 0:
		return 0
	elif schet == 1:
		return 1
	else:
		return 2
for i in range(t):
	s = ''
	s = input()
	print(F(s))


t = int(input())
for _ in range(t):
	s = input()
	i = 0
	ans = 0
	if s == '0' * len(s):
		print(1)
	else:
		while i < len(s):
			if s[i] == '0':
				j = 1
				while i + j < len(s):
					if s[i + j] == '1':
						ans += 1
						i = i + j
						break
					else:
						j += 1
				if s[-1] == '0':
					ans += 1
					i = i + j
			else:
				i += 1
		ans = min(ans, 2)
		print(ans)


import sys
input = sys.stdin.readline
for i in range(int(input())):
	s = list(filter(lambda x: x != '', input().strip().split('1')))
	if len(s) > 2:
		print(2)
	else:
		print(len(s))


t = int(input())
for abcd in range(t):
	s = input()
	if len(s) == 1:
		if s[0] == '1':
			print(0)
		else:
			print(1)
		continue
	ret = []
	for i in range(1, len(s)):
		if s[i] != s[i - 1]:
			ret.append(s[i - 1])
	if ret == []:
		ret.append(s[0])
	if s[-1] != ret[-1]:
		ret.append(s[-1])
	ret = ''.join(ret)
	k1 = ret.find('0')
	k2 = ret.rfind('0')
	l1 = ret.find('1')
	l2 = ret.rfind('1')
	if k1 == -1:
		print(0)
	elif l1 == -1:
		print(1)
	elif k1 == k2:
		print(1)
	else:
		print(2)


for _ in range(int(input())):
	s = input()
	if s[0] == '0':
		cnt = 1
	else:
		cnt = 0
	for i in range(1, len(s)):
		if s[i - 1] != '0' and s[i] == '0':
			cnt += 1
	print(min(cnt, 2))


import sys
pin = sys.stdin.readline
for T in range(int(pin())):
	S = pin().strip()
	Z = S[0] == '0'
	Z += S.count('10')
	print(min(2, Z))


for i in range(int(input())):
	s = input()
	s = s.strip('1')
	if ('1' in s) == True:
		print('2')
	elif ('0' in s) == True:
		print('1')
	else:
		print('0')


import collections

def solve(s):
	zeros = s.count('0')
	if zeros == 0:
		return 0
	f = s.find('0')
	l = s.rfind('0')
	if l - f + 1 == zeros:
		return 1
	return 2
for t in range(int(input())):
	s = input()
	print(solve(s))


import math as m
for i in range(int(input())):
	s = input()
	l = list(s)
	z = 0
	y = 0
	o = 0
	d = 0
	for i in s:
		if i == '0':
			z += 1
		else:
			y = max(z, y)
			z = 0
	y = max(z, y)
	for i in s:
		if i == '1':
			o += 1
		else:
			d = max(o, d)
			o = 0
	d = max(o, d)
	z = s.count('0')
	o = s.count('1')
	if z == len(l):
		print(1)
	elif o == len(l):
		print(0)
	elif y + d == len(l):
		print(1)
	elif y == z:
		print(1)
	else:
		print(2)


t = int(input())
for i in range(t):
	st = input()
	if '0' in st and '1' not in st:
		print(1)
	elif '0' not in st and '1' in st:
		print(0)
	elif st == '1' * st.find('0') + '0' * (st.rfind('0') - st.find('0') + 1) + '1' * (len(st) - st.rfind('0') - 1):
		print(1)
	elif st == '1' * st.find('0') + '0' * (len(st) - st.find('0')):
		print(1)
	elif st == '0' * st.find('1') + '1' * (len(st) - st.find('1')):
		print(1)
	else:
		print(2)


k = []
for _ in range(int(input())):
	s = input('')
	t = s.split('1')
	r = list((ele for ele in t if ele.strip()))
	k.append(min(len(r), 2))
for i in k:
	print(i)


def mex(s):
	if len(set(s)) == 1:
		if s[0] == '1':
			return 0
		else:
			return 1
	k = s.split('1')
	res = 0
	for i in k:
		if i != '':
			res += 1
	if res >= 2:
		return 2
	else:
		return 1
t = int(input())
for i in range(t):
	s = input()
	print(mex(s))


from sys import stdin, stdout

def INPUT():
	return list((int(i) for i in stdin.readline().split()))

def inp():
	return stdin.readline()

def out(x):
	return stdout.write(x)
from math import *
from decimal import *
INT_MAX = 10 ** 13
mod = 998244353

def ceil(a, b):
	return (a + b - 1) // b

def pow(a, n):
	res = 1
	while n:
		if n & 1:
			res = res * a % mod
		a = a * a
		a %= mod
		n >>= 1
	return res
for _ in range(int(input())):
	s = input()
	n = len(s)
	i = 0
	ans = 0
	if '1' in s and '0' in s:
		fin = 2
	else:
		fin = mod ** 2
	while i < n:
		if s[i] == '0':
			ans += 1
			while i < n and s[i] == '0':
				i += 1
		else:
			i += 1
	print(min(ans, fin))


import math
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	s = input()
	arr = list(s)
	arr2 = set(arr)
	if arr2 == {'0'}:
		print(1)
	elif arr2 == {'1'}:
		print(0)
	else:
		s2 = f'{arr[0]}'
		flag1 = False
		for k in range(1, len(arr)):
			if arr[k - 1] != arr[k]:
				s2 += arr[k]
			if '010' in s2:
				flag1 = True
				break
		if flag1:
			print(2)
		else:
			print(1)


t = int(input())
a = ''
for i in range(t):
	l = input().split('1')
	c = 0
	for j in l:
		if j != '':
			c += 1
			if c == 2:
				break
	a += str(c)
for i in a:
	print(i)


for _ in range(int(input())):
	s = input().strip()
	if '0' not in s:
		print(0)
		continue
	if '1' not in s:
		print(1)
		continue
	z = False
	o = False
	flag = False
	for i in s:
		if i == '0':
			if z == True and o == True:
				print(2)
				flag = True
				break
			z = True
			continue
		if z == True and i == '1':
			o = True
	if not flag:
		print(1)


def solve(s):
	n = len(s)
	ans = 0
	flag = False
	for i in range(n):
		if s[i] == '1':
			if flag:
				ans += 1
				flag = False
		else:
			flag = True
	if flag:
		ans += 1
	return min(ans, 2)
t = int(input())
for _ in range(t):
	s = input()
	print(solve(s))


t = int(input())
while t:
	t -= 1
	s = input()
	c = 0
	f = True
	for i in range(0, len(s)):
		if f and s[i] == '0':
			c += 1
			f = False
		if s[i] == '1':
			f = True
		if c >= 2:
			break
	print(c)


def solve(s):
	if '0' not in s:
		return 0
	if len((*filter(None, s.split('1')),)) == 1:
		return 1
	return 2
for _ in '_' * int(input()):
	print(solve(input()))


for i in range(int(input())):
	r = [0]
	for i in input():
		if i != r[-1]:
			r += [i]
	print(min(2, r.count('0')))


from __future__ import division, print_function
import math
import sys
import os
from io import BytesIO, IOBase
from collections import deque, Counter, OrderedDict, defaultdict
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def inp():
	return int(input())

def inps():
	return input().strip()

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input().strip()
	return list(s[:len(s)])

def invr():
	return map(int, input().split())
for _ in range(inp()):
	s = insr()
	g = Counter(s)
	cnt = 0
	n = len(s)
	flag = 0
	for i in range(n):
		if s[i] == '0':
			if flag:
				continue
			else:
				flag = 1
				cnt += 1
		else:
			flag = 0
	print(min(cnt, 2))


for _ in range(int(input())):
	s = input()
	count = 0
	x = s[0]
	if x == '0':
		count = count + 1
	for i in s:
		if x != i:
			if i == '0':
				count = count + 1
			x = i
	print(2 if count > 2 else count)


for i in range(int(input())):
	s = input()
	if len(s) == 1:
		print(1 - int(s))
		continue
	if '1' not in s:
		print(1)
	elif '0' not in s:
		print(0)
	else:
		o = 0
		z = 0
		i = 0
		while i < len(s):
			if s[i] == '0':
				while i + 1 < len(s) and s[i + 1] == '0':
					i += 1
				i += 1
				z += 1
			else:
				while i + 1 < len(s) and s[i + 1] == '1':
					i += 1
				i += 1
				o += 1
		if z == 1:
			print(1)
		else:
			print(2)


import itertools
for i in range(int(input())):
	b_str = input()
	uu = ''.join((c[0] for c in itertools.groupby(b_str)))
	len_uu = len(uu)
	if len_uu > 3:
		print('2')
	elif len_uu == 3:
		if uu[-1] == '0':
			print('2')
		else:
			print('1')
	elif len_uu == 2:
		print('1')
	elif uu == '0':
		print('1')
	else:
		print('0')


from sys import *
input = stdin.readline
import random
from bisect import *
import math
from collections import *
import operator
from heapq import *
from itertools import *
inf = 10 ** 18
mod = 10 ** 9 + 7
MOD = 998244353
for _ in range(int(input())):
	s = input()[:-1]
	n = len(s)
	res = ''
	i = 0
	while i < n:
		if s[i] == '1':
			while i < n and s[i] == '1':
				i += 1
			res += '1'
		else:
			while i < n and s[i] == '0':
				i += 1
			res += '0'
	cnt0 = res.count('0')
	cnt1 = res.count('1')
	if cnt0 == 0:
		print(0)
	elif cnt1 == 0:
		print(1)
	elif cnt0 <= 1:
		print(1)
	else:
		print(2)


def rajeev(a):
	if a[0] == '0':
		b = a.index('1')
		try:
			c = a.index('0', b)
			return False
		except:
			return True
	else:
		b = a.index('0')
		try:
			d = a.index('1', b)
			try:
				c = a.index('0', d)
				return False
			except:
				return True
		except:
			return True
t = int(input())
for tt in range(t):
	s = input()
	x = s.count('0')
	y = s.count('1')
	if x == 0:
		print(0)
	elif y == 0 or x == 1 or rajeev(s):
		print(1)
	else:
		print(2)


for _ in range(int(input())):
	print(min(2, len([t for t in input().split('1') if t != ''])))


t = int(input())
q = []
for i in range(t):
	s = str(input())
	d = 0
	if len(s) == 1:
		if s[0] == '1':
			q.append(0)
			continue
		else:
			q.append(1)
			continue
	else:
		for j in range(len(s) - 1):
			if s[j] == s[j + 1]:
				continue
			elif s[j] == '0':
				d = d + 1
			else:
				continue
		if s[len(s) - 2] == s[len(s) - 1] and s[len(s) - 2] == '0':
			d = d + 1
		if s[len(s) - 2] != s[len(s) - 1] and s[len(s) - 2] == '1':
			d = d + 1
		q.append(min(2, d))
for i in range(t):
	print(q[i])


for _ in range(int(input())):
	s = input().lstrip('1').rstrip('1')
	if len(s) == 0:
		print(0)
	elif all((i == '0' for i in s)):
		print(1)
	else:
		print(2)


for _ in range(int(input())):
	s = input()
	s = s + '1'
	count = 0
	flag = False
	A = ['0', '1', '2']
	for i in range(len(s)):
		if s[i] in A:
			A.remove(s[i])
		if s[i] == '0':
			flag = True
		elif flag == True:
			count += 1
			flag = False
	if len(A) == 0:
		print(0)
	elif int(min(A)) < count:
		print(min(A))
	else:
		print(count)


for _ in range(int(input())):
	s = input()
	count_0 = s.count('0')
	count_1 = s.count('1')
	if count_0 == 0:
		print(0)
	elif count_1 == 0:
		print(1)
	else:
		new_str = s
		for i in range(len(s)):
			if s[i] != '1':
				break
			count_1 -= 1
		for i in range(len(s) - 1, -1, -1):
			if s[i] != '1':
				break
			count_1 -= 1
		if count_0 == 0:
			print(0)
		elif count_1 == 0:
			print(1)
		else:
			print(2)


def solve(s):
	if '0' not in s:
		return 0
	if ('1' + s).count('10') <= 1:
		return 1
	return 2
t = int(input())
for _ in range(t):
	s = input().strip()
	print(solve(s))


t = int(input())
for _ in range(t):
	s = input()
	if '0' not in s:
		print(0)
	elif '1' not in s:
		print(1)
	else:
		i = s.index('0')
		j = s.rindex('0')
		s = s[i:j + 1]
		if '1' not in s:
			print(1)
		else:
			print(2)


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
from collections import Counter
from collections import defaultdict as dd
flush = lambda : stdout.flush()
stdstr = lambda : stdin.readline()
stdint = lambda : int(stdin.readline())
stdpr = lambda x: stdout.write(str(x))
stdmap = lambda : map(int, stdstr().split())
stdarr = lambda : list(map(int, stdstr().split()))
mod = 1000000007
for _ in range(stdint()):
	s = input()
	split = [[s[0]]]
	for i in range(1, len(s)):
		if s[i] == split[-1][-1]:
			split[-1].append(s[i])
		else:
			split.append([s[i]])
	if len(split) == 1:
		if split[0][0] == '1':
			print(0)
		else:
			print(1)
	elif len(split) == 2:
		print(1)
	else:
		zeroGroups = 0
		for i in split:
			if i[0] == '0':
				zeroGroups += 1
		print(min(2, zeroGroups))


import bisect
import heapq
import math
import sys
import itertools
input = lambda : sys.stdin.readline().rstrip()

def solve():
	s = list(input())
	if '1' not in s:
		print(1)
		return
	if '0' not in s:
		print(0)
		return
	zeroone = False
	one = False
	zerotwo = False
	for i in s:
		if i == '0':
			zeroone = True
		if zeroone and i == '1':
			one = True
		if zeroone and one and (i == '0'):
			zerotwo = True
	if zerotwo:
		print(2)
	else:
		print(1)
t = int(input())
for _ in range(t):
	solve()


import sys
LI = lambda : list(map(int, sys.stdin.readline().split()))
MI = lambda : map(int, sys.stdin.readline().split())
SI = lambda : sys.stdin.readline().strip('\n')
II = lambda : int(sys.stdin.readline())
for _ in range(II()):
	s = SI()
	t = ''
	z = 0
	for c in s:
		if not t or c != t[-1]:
			t += c
			z += c == '0'
	o = len(t) - z
	if not o:
		print(1)
	elif z < 3:
		print(z)
	else:
		print(2)


t = int(input())
ans = []
for _ in range(t):
	s = input()
	n = len(s)
	i = 0
	ret = 0
	while i < n:
		while i < n and s[i] == '1':
			i += 1
		if i < n:
			while i < n and s[i] == '0':
				i += 1
			ret += 1
			if ret == 2:
				break
	ans.append(str(ret))
print('\n'.join(ans))


import sys
input = sys.stdin.readline
(N,) = map(int, input().split())
for _ in range(N):
	s = input().strip()
	if not s.replace('1', ''):
		print(0)
		continue
	f = 0
	for i in range(len(s)):
		if f == 2 and s[i] == '0':
			f = 3
			continue
		if f == 1 and s[i] == '1':
			f = 2
			continue
		if f == 0 and s[i] == '0':
			f = 1
	if f <= 2:
		print(1)
	else:
		print(2)


import sys
input = sys.stdin.readline

def main():
	S = input().strip()
	if '0' not in S:
		print(0)
		return
	elif '1' not in S:
		print(1)
		return
	cnt = 0
	bef = '1'
	for s in S:
		if s != bef:
			cnt += 1
			bef = s
	if cnt >= 3:
		print(2)
	else:
		print(1)
for _ in range(int(input())):
	main()


for _ in range(int(input())):
	s = list(input())
	lst = [0, 0]
	start = False
	mid = False
	end = False
	for i in range(len(s)):
		if s[i] == '0':
			lst[0] += 1
			start = True
		if s[i] == '1' and start:
			lst[1] += 1
			mid = True
		if s[i] == '0' and mid:
			end = True
			print(2)
			break
	if lst[0] == 0:
		print(0)
	elif lst[1] == 0:
		print(1)
	elif not end:
		print(1)


cnt = int(input())
for _ in range(cnt):
	s = input()
	if '0' not in s:
		print(0)
	if '0' in s:
		(i1, i2) = (s.index('0'), s.rindex('0'))
		if s[i1:i2] == '0' * (i2 - i1):
			print(1)
		else:
			print(2)


n = int(input())
i = 0
list1 = []
while i < n:
	x = input()
	list1.append(x)
	i = i + 1
m = 0
while m < n:
	y = list1[m]
	a = y.count('0')
	c = y.find('0')
	d = y.rfind('0')
	e = y.find('1', c, d)
	if a < 1:
		print(0)
	if e > 0:
		print(2)
	if e < 0 and a > 0:
		print(1)
	m = m + 1


n = int(input())
for i in range(n):
	x = input()
	if len(x) == x.count('1'):
		print(0)
	else:
		v = x.find('0')
		b = x.rfind('0')
		if b - v + 1 == x.count('0'):
			print(1)
		else:
			print(2)


for s in [*open(0)][1:]:
	print(min(2, sum((t > '/' for t in s.split('1')))))


in1 = lambda : int(input())
in2 = lambda : list(map(int, input().split()))
for i in range(in1()):
	s = input()
	ar = []
	for c in s:
		if len(ar) == 0 or c != ar[-1]:
			ar.append(c)
	if len(ar) == 1:
		if ar[0] == '0':
			print(1)
		else:
			print(0)
	elif len(ar) == 2:
		print(1)
	elif len(ar) == 3:
		if ar[0] == '0':
			print(2)
		else:
			print(1)
	else:
		print(2)


t = int(input())
out = ''
while t > 0:
	ans = 0
	for i in '1' + input():
		if i == '0' and new:
			ans += 1
			new = False
		if i == '1':
			new = True
		if ans > 1:
			out += '2\n'
			break
	else:
		out += str(ans) + '\n'
	t -= 1
print(out[:-1])


n = int(input())
for i in range(n):
	s = input()
	p = ''.join(sorted(s))
	if s.count('1') == len(s):
		print(0)
	elif s.count('0') == len(s):
		print(1)
	else:
		r = s.split('1')
		m = 0
		for i in range(len(r)):
			if len(r[i]) != 0 and len(r[i]) != s.count('0'):
				m += 1
				break
		if m != 0:
			print(2)
		else:
			print(1)


for _ in range(int(input())):
	s = input()
	y = s[0]
	zero = one = False
	for i in s:
		if i == '0':
			zero = True
		if i == '1':
			one = True
		if zero and one:
			break
	if not (zero and one):
		print(1 if zero else 0)
	else:
		for i in range(1, len(s)):
			if y[-1] != s[i]:
				y += s[i]
		if y.count('0') == 1:
			print(1)
		else:
			print(2)


from sys import stdin, stdout
ip = lambda : stdin.readline().rstrip('\r\n')
ips = lambda : ip().split()
out = lambda x, end='\n': stdout.write(str(x) + end)
for _ in range(int(ip())):
	s = ip()
	c = 0
	for i in range(len(s) - 1):
		if s[i] == '0' and s[i + 1] == '1':
			c += 1
	if s[-1] == '0':
		c += 1
	print(min(c, 2))


for _ in range(int(input())):
	s = str(input()) + 'x'
	ans = 2
	check = 0
	temp_sum = 0
	for i in s:
		if i == '0':
			check = 1
		elif i != '0' and check == 1:
			temp_sum += 1
			check = 0
	if s[-1] == '0':
		temp_sum += 1
	print(min(ans, temp_sum))


t = int(input())
for _ in range(t):
	s = input()
	n = len(s)
	d = [s[0]]
	for i in range(1, n):
		if s[i] != d[-1]:
			d.append(s[i])
	count = 0
	n = len(d)
	for i in range(n):
		if d[i] == '0':
			count += 1
	print(min(count, 2))


def solve(s):
	ans = 0
	zero = 0
	one = 0
	for i in s:
		if i == '0' and zero == 0:
			zero = 1
		elif i == '1' and one == 0 and (zero == 1):
			one = 1
		elif one == 1 and zero == 1 and (i == '0'):
			return 2
	if zero == 0 and one == 1:
		return 0
	if zero == 1 and one == 0:
		return 1
	if zero == 1 and one == 1:
		return 1
	else:
		return 0
t = int(input())
for i in [0] * t:
	s = input()
	print(solve(s))


t = int(input())
for _ in range(t):
	s = input()
	ss = set(s)
	mv = int(min(set('012') - ss))
	cv = 0
	pv = ''
	s += ' '
	for i in range(len(s)):
		if s[i] == pv:
			continue
		cv += pv == '0'
		pv = s[i]
	print(min(mv, cv))


t = int(input())
for q in range(t):
	s = input()
	if s.find('1') == -1:
		print(1)
	elif s.find('0') == -1:
		print(0)
	elif s.find('1', s.find('0'), s.rfind('0')) == -1:
		print(1)
	else:
		print(2)


import sys
try:
	for t in range(int(sys.stdin.readline())):
		s = sys.stdin.readline()
		c_1 = s.count('1')
		c_0 = s.count('0')
		if c_0 == 0:
			print(0)
		elif c_1 == 0:
			print(1)
		elif c_1 >= 0 and c_0 == 1:
			print(1)
		else:
			cu_1 = 0
			cu_0 = 0
			for i in range(1, len(s) - 1):
				if s[i] == '1':
					if s[i] == s[i - 1]:
						pass
					else:
						cu_0 = cu_0 + 1
				elif s[i] == '0':
					if s[i] == s[i - 1]:
						pass
					else:
						cu_1 = cu_1 + 1
			if s[len(s) - 2] == '0':
				cu_0 = cu_0 + 1
			else:
				cu_1 = cu_1 + 1
			if cu_0 == 0:
				print(0)
			elif cu_1 == 0:
				print(1)
			elif cu_1 >= 1 and cu_0 == 1:
				print(1)
			else:
				print(2)
except EOFError as e:
	pass


import math
import os
import random
import re
import sys
t = int(input())
for _ in range(t):
	a = list(input())
	k = 0
	if len(a) > 1:
		for i in range(len(a) - 1):
			if a[i] == '0' and a[i + 1] == '1':
				k += 1
	if a[len(a) - 1] == '0':
		k += 1
	print(min(k, 2))


import sys
from math import ceil
input = sys.stdin.readline
for _ in range(int(input())):
	s = input().strip('\n')
	n = len(s)
	if '1' not in s:
		print(1)
	elif '0' not in s:
		print(0)
	else:
		meme = []
		last = s[0]
		for i in range(n):
			if s[i] != last:
				meme.append(last)
			last = s[i]
		if meme and meme[-1] != last:
			meme.append(last)
		print(min(meme.count('0'), 2))


for _ in range(int(input())):
	s = input()
	a = 1
	cnt = 1
	for i in range(1, len(s)):
		if s[i] != s[i - 1]:
			a += 1
	ans = 2
	if a == 1:
		if s[0] == '0':
			ans = 1
		else:
			ans = 0
	elif a == 2:
		ans = 1
	elif a == 3:
		if s[0] == '0':
			ans = 2
		else:
			ans = 1
	print(ans)


for i in range(int(input())):
	s = input()
	if s.count('01') + s.count('10') > 2:
		a = 2
	elif s.count('01') + s.count('10') == 2:
		if s[0] == '0':
			a = 2
		else:
			a = 1
	elif s.count('01') + s.count('10') == 1:
		a = 1
	elif s.count('01') + s.count('10') == 0:
		if s[0] == '0':
			a = 1
		else:
			a = 0
	print(a)


for _ in range(int(input())):
	s = input()
	if s.count('1') == len(s):
		print(0)
	else:
		z = s.count('0')
		if '0' * z in s:
			print(1)
		else:
			print(2)


import sys
input = lambda : sys.stdin.readline().rstrip()
from itertools import groupby

def solve():
	S = input()
	d = list(groupby(S))
	if len(d) == 1:
		if d[0][0] == '1':
			print(0)
		else:
			print(1)
	elif len(d) == 2:
		print(1)
	elif len(d) == 3:
		if d[0][0] == '1':
			print(1)
		else:
			print(2)
	else:
		print(2)
	ans = 0
	return ans
T = int(input())
for i in range(1, T + 1):
	solve()


from sys import stdin, setrecursionlimit, maxsize
from collections import deque
from heapq import *
from math import sqrt, gcd, ceil
input = stdin.readline
setrecursionlimit(10 ** 5)

def solution(s):
	zero = s.count('0')
	if zero == 0:
		return 0
	first = s.find('0')
	last = s.rfind('0')
	if last - first + 1 == zero:
		return 1
	else:
		return 2
for _ in range(int(input())):
	s = input().strip()
	print(solution(s))


import os, sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
try:
	(sys.stdin, sys.stdout) = (open('in.txt', 'r'), open('out.txt', 'w'))
except:
	pass
ii1 = lambda : int(sys.stdin.readline().strip())
is1 = lambda : sys.stdin.readline().strip()
iia = lambda : list(map(int, sys.stdin.readline().strip().split()))
isa = lambda : sys.stdin.readline().strip().split()
mod = int(1000000000.0 + 7)
from math import ceil
for _ in range(ii1()):
	s = is1()
	z = s.count('0')
	o = s.count('1')
	if z == 0:
		print(0)
	elif o == 0:
		print(1)
	else:
		i = 0
		n = len(s)
		o1 = 0
		while i < n and s[i] == '1':
			o1 += 1
			i += 1
		z1 = 0
		while i < n and s[i] == '0':
			z1 += 1
			i += 1
		o2 = 0
		while i < n and s[i] == '1':
			o2 += 1
			i += 1
		if z1 + o1 + o2 == n:
			print(1)
		else:
			print(2)


def inp():
	return int(input())

def st():
	return input().rstrip('\n')

def lis():
	return list(map(int, input().split()))

def ma():
	return map(int, input().split())
t = inp()
while t:
	t -= 1
	s = st()
	if s.count('1') == len(s):
		print(0)
	elif s.count('0') == len(s):
		print(1)
	else:
		co = 0
		p = -1
		for i in s:
			if p == i:
				continue
			if p == -1:
				if i == '0':
					co = 1
					p = i
				else:
					p = i
				continue
			if i == '1' and p == '0':
				p = i
			elif i == '0' and p == '1':
				co += 1
				p = i
		if co == 1:
			print(1)
		else:
			print(2)


for _ in range(int(input())):
	s = input()
	i = 0
	n = len(s)
	ans = 0
	while i < n:
		c = 0
		while i < n and s[i] == '0':
			c += 1
			i += 1
		if c > 0:
			ans += 1
		i += 1
	if ans >= 2:
		print(2)
	elif ans == 1:
		print(1)
	else:
		print(0)


import math as m
for _ in range(int(input())):
	s = input()
	z = s.count('0')
	o = s.count('1')
	n = len(s)
	if o == n:
		print(0)
		continue
	if z == n:
		print(1)
		continue
	x = s.count('10') + (s[0] == '0')
	print(min(x, 2))


for _ in range(int(input())):
	s = input()
	n = len(s)
	if s[0] == '0':
		on = False
		for i in range(1, n):
			if s[i] == '1':
				on = True
				z = False
				for j in range(i + 1, n):
					if s[j] == '0':
						print(2)
						z = True
						break
				if not z:
					print(1)
				break
		if not on:
			print(1)
	else:
		zc = s.count('0')
		z = False
		for i in range(1, n):
			if s[i] == '0':
				z = True
				t = 1
				for j in range(i + 1, n):
					if s[j] != '0':
						break
					else:
						t += 1
				if t == zc:
					print(1)
				else:
					print(2)
				break
		if not z:
			print(0)


try:
	T = int(input())
	for i in range(T):
		s = input()
		if int(s) == 0:
			print(1)
		else:
			cnt = 0
			for i in s:
				if int(i) == 1:
					cnt += 1
			if len(s) == cnt:
				print(0)
			else:
				cnt = 0
				if s[0] == '0':
					cnt += 1
				for i in range(1, len(s)):
					if s[i] != s[i - 1] and s[i] == '0':
						cnt += 1
				if cnt < 2:
					print(cnt)
				else:
					print(2)
except:
	pass


for _ in range(int(input())):
	a = input()
	s = 0
	d = 0
	b = []
	if '0' in a and '1' not in a:
		print(1)
	elif '1' in a and '0' not in a:
		print(0)
	elif a.index('1') > a.rfind('0') or a.rfind('1') < a.index('0') or '1' not in a[a.index('0'):a.rfind('0') + 1]:
		print(1)
	elif a.rfind('0') > a.rfind('1') > a.index('0') or a.rfind('1') > a.rfind('0') > a.index('1'):
		print(2)


import sys
import math
t = int(sys.stdin.readline())
for line in sys.stdin:
	line = line.strip()
	s = set(line)
	x = 0
	if len(s) == 1:
		x = (int(s.pop()) + 1) % 2
		print(x)
	else:
		i = 0
		n = line[i]
		f = len(line) - 1
		n2 = line[f]
		if int(n) == 0:
			while i < len(line) and line[i] == n:
				i += 1
			x += 1
		if int(n2) == 0:
			while f > i and line[f] == n2:
				f -= 1
			x += 1
		if int(n) == 1 and int(n2) == 1:
			while i < len(line) and line[i] == n:
				i += 1
			while f > i and line[f] == n2:
				f -= 1
		s = set(line[i:f + 1])
		if len(s) > 1:
			print(2)
		else:
			x += (int(s.pop()) + 1) % 2
			print(x)


import sys
import math
from bisect import bisect_right as br

def int_arr():
	return list(map(int, sys.stdin.readline().split()))

def str_arr():
	return list(map(str, sys.stdin.readline().split()))

def input():
	return sys.stdin.readline().strip()
for _ in range(int(input())):
	st = input()
	if '0' not in st:
		print(0)
		continue
	elif '1' not in st:
		print(1)
	else:
		val = st.count('0')
		ind = st.index('0')
		if '1' not in st[ind:ind + val]:
			print(1)
		else:
			print(2)


import sys
import io
import math
import collections
import decimal
import itertools
import bisect
import heapq

def input():
	return sys.stdin.readline()[:-1]
INF = 10 ** 10

def solve(S):
	f0 = False
	n = 0
	for i in range(len(S)):
		if S[i] == '0' and (not f0):
			f0 = True
			n += 1
		elif S[i] == '1':
			f0 = False
	if n == 0:
		return 0
	elif n == 1:
		return 1
	else:
		return 2
T0 = int(input())
for _ in range(T0):
	S = input()
	print(solve(S))


import sys
import math
import heapq
from collections import defaultdict as dd
from collections import OrderedDict as od
from collections import deque
from itertools import permutations as pp
from itertools import combinations as cc
from sys import stdin
from functools import cmp_to_key as ctk
from functools import lru_cache
from bisect import bisect_left as bs_l
from bisect import bisect_right as bs_r
input = sys.stdin.readline
mod = 10 ** 9 + 7
sys.setrecursionlimit(10 ** 5)

def comparator(x, y):
	if x[0] == y[0]:
		return y[1] - x[1]
	return True

def ncr(n, r):
	if n < r:
		return 0
	return fc[n] * pow(fc[r] * fc[n - r] % mod, mod - 2, mod) % mod
T = int(input())
for _ in range(T):
	s = input().strip()
	(ze, on) = (s.count('0'), s.count('1'))
	if ze == 0:
		print(0)
	elif on == 0:
		print(1)
	else:
		gr = 0
		c = 0
		for i in s:
			if i == '1':
				if c:
					gr += 1
				c = 0
			else:
				c += 1
		if c:
			gr += 1
		print(min(2, gr))


import os, sys
from io import BytesIO, IOBase
from types import GeneratorType
from bisect import *
from collections import defaultdict, deque, Counter
import math, string
from heapq import *
from operator import add
from itertools import accumulate, groupby
BUFSIZE = 8192
sys.setrecursionlimit(10 ** 5)

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		import os
		self.os = os
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			self.os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
inf = float('inf')
en = lambda x: list(enumerate(x))
ceil_ = lambda a, b: (a + b - 1) // b
ii = lambda : int(input())
r = lambda : map(int, input().split())
rr = lambda : list(r())

def solve():
	s = input()
	arr = groupby(s)
	a = 0
	if '1' not in s:
		print(1)
		return
	if '0' not in s:
		print(0)
		return
	x = 0
	for (i, j) in arr:
		if i == '0':
			x += 1
	print(min(2, x))
for _ in ' ' * ii():
	solve()


from __future__ import division, print_function
import os
import sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import collections, string, bisect, re, random, queue, itertools, statistics, math
from collections import *
from bisect import *
from string import *
from itertools import *
from statistics import *
from math import *
from re import *
from queue import *

class Stack:

	def __init__(self):
		self.items = []

	def push(self, item):
		self.items.append(item)

	def pop(self):
		return self.items.pop()

	def empty(self):
		return self.items == []

	def size(self):
		return len(self.items)

	def at(self, idx):
		try:
			return self.items[idx]
		except:
			return -1

class priority_queue(object):

	def __init__(self):
		self.queue = []

	def __str__(self):
		return ' '.join([str(i) for i in self.queue])

	def empty(self):
		return len(self.queue) == 0

	def push(self, data):
		self.queue.append(data)

	def pop(self):
		try:
			max = 0
			for i in range(len(self.queue)):
				if self.queue[i] > self.queue[max]:
					max = i
			item = self.queue[max]
			del self.queue[max]
			return item
		except IndexError:
			print()
			exit()
mod = int(1000000000.0 + 7)
imax = float('inf')
imin = float('-inf')
true = True
false = False
N = int(100000.0 + 10)
none = None
inp = lambda : input()
I = lambda : int(inp())
M = lambda : map(int, inp().split())
MS = lambda : map(str, inp().split())
S = lambda : list(MS())
L = lambda : list(M())

def IO():
	try:
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('uttar.txt', 'w')
	except:
		pass
IO()

def kabraji_ka_totka():
	s = inp()
	if not s.count('0'):
		print(0)
		return
	if s.rfind('0') - s.find('0') + 1 == s.count('0'):
		print(1)
		return
	print(2)
for i in range(I()):
	kabraji_ka_totka()


for _ in range(int(input())):
	s = input()
	if all((i == '1' for i in s)):
		print(0)
	elif all((i == '0' for i in s)):
		print(1)
	elif len(list(filter(None, s.split('1')))) == 1:
		print(1)
	else:
		print(2)


import math

def median(s):
	cnt = 0
	i = 0
	while i < len(s):
		if s[i] == '0':
			cnt += 1
			j = i
			while j < len(s) and s[j] == '0':
				j += 1
			i = j
		i += 1
	return min(2, cnt)
t = int(input())
for i in range(t):
	s = input()
	print(median(s))


t = int(input())
for _ in range(t):
	s = str(input())
	n = len(s)
	if '0' not in s:
		print(0)
	elif '1' not in s:
		print(1)
	else:
		key = s[0]
		val = 0
		for i in range(1, n):
			if s[i] != key:
				if s[i] == '1':
					val += 1
				key = s[i]
		if key == '0':
			val += 1
		print(min(val, 2))


def solve(nums):
	noz = i = 0
	n = len(nums)
	while i < n:
		if nums[i] == '0':
			noz += 1
			while i < n and nums[i] == '0':
				i += 1
		else:
			i += 1
	return min(noz, 2)
t = int(input())
while t:
	nums = list(input())
	print(solve(nums))
	t -= 1


for _ in range(int(input())):
	s = input()
	ans = 0
	ok = False
	for i in range(0, len(s)):
		if s[i] == '0':
			if ok == False:
				ok = True
				ans += 1
		else:
			ok = False
	ans = min(ans, 2)
	print(ans)


t = int(input())
for _ in range(t):
	s = input()
	cnt = 0
	if s[0] == '0':
		cnt = 1
	for i in range(1, len(s)):
		if s[i] == '0' and s[i - 1] == '1':
			cnt += 1
			if cnt == 2:
				break
	print(cnt)


for _ in range(int(input())):
	l = input()
	try:
		if '01' and '10' in l[l.index('0'):]:
			print(2)
		else:
			print(1)
	except:
		print(0)


for _ in range(int(input())):
	a = input()
	k0 = a.count('0')
	k1 = a.count('1')
	c = 0
	if k1 == 0:
		print(1)
		continue
	if k0 == 0:
		print(0)
		continue
	if a.rfind('0') - a.find('0') + 1 == k0:
		print(1)
	else:
		print(2)


for _ in range(int(input())):
	st = input()
	zcount = st.count('0')
	onecount = st.count('1')
	if zcount == 0:
		print(0)
		continue
	fst = st.find('0')
	lst = st.rfind('0')
	if lst - fst + 1 == zcount:
		print(1)
	else:
		print(2)


from collections import defaultdict, Counter
from math import ceil
test = int(input())
for t in range(test):
	s = input()
	a = []
	for i in range(1, len(s)):
		if s[i] != s[i - 1]:
			if s[i - 1] == '1':
				a.append(0)
			else:
				a.append(1)
	if s[-1] == '1':
		a.append(0)
	else:
		a.append(1)
	currSum = 0
	for i in range(len(a)):
		currSum += a[i]
		if currSum > 2:
			currSum = 2
	print(currSum)


for i in range(int(input())):
	s = input()
	n = len(s)
	found = False
	counts = [0] * n
	counts[0] = 0 if s[0] == '1' else 1
	for i in range(1, n):
		if s[i] == '0' and s[i - 1] == '1':
			counts[i] = counts[i - 1] + 1
		elif s[i] == '0':
			counts[i] = counts[i - 1]
		else:
			counts[i] = counts[i - 1]
		if counts[i] == 2:
			found = True
	print(counts[-1]) if found == False else print(2)


for _ in range(int(input())):
	a = input()
	count = 0
	i = 0
	n = len(a)
	while i < n:
		if a[i] == '0':
			count += 1
			while i < n and a[i] == '0':
				i += 1
		i += 1
	if '1' in a and '0' in a:
		print(min(2, count))
	else:
		print(count)


for _ in range(int(input())):
	s = input()
	s_without_repeats = ['-1']
	for s_i in s:
		if s_i != s_without_repeats[-1]:
			s_without_repeats.append(s_i)
	del s_without_repeats[0]
	if len(s_without_repeats) > 2 and s_without_repeats[0] == '0':
		print(2)
	elif len(s_without_repeats) > 3 and s_without_repeats[0] == '1':
		print(2)
	elif len(s_without_repeats) == 3 and s_without_repeats[0] == '1':
		print(1)
	elif len(s_without_repeats) == 2:
		print(1)
	elif s_without_repeats[0] == '0':
		print(1)
	else:
		print(0)


result = ''
for r in range(0, int(input())):
	a = input()
	a = list(a)
	temp = [a[0]]
	if a[0] == '0':
		n = 1
	else:
		n = 0
	for i in range(1, len(a)):
		if a[i - 1] == a[i]:
			continue
		temp.append(a[i])
		if a[i] == '0':
			n += 1
		if n == 2:
			break
	if temp == ['0']:
		result += '1\n'
	elif temp == ['1']:
		result += '0\n'
	elif n == 2:
		result += '2\n'
	else:
		result += '1\n'
print(result.rstrip('\n'))


for _ in range(int(input())):
	s = input()
	n = len(s)
	mexs = 0
	i = 1
	test = 0
	pre = s[0]
	while i < n:
		if s[i] != pre:
			mexs += 1 - int(pre)
			pre = s[i]
		if mexs >= 2:
			test = 1
			break
		i += 1
	if test:
		print(2)
	else:
		mexs += 1 - int(pre)
		print(mexs)


for cas in range(int(input())):
	binaryS = input()
	(one, zero, consecutiveZero) = (0, 0, 0)
	maxConsZero = 0
	for i in range(0, len(binaryS), 1):
		if binaryS[i] == '0':
			zero += 1
			if i - 1 >= 0 and (binaryS[i - 1] == '0' and binaryS[i] == '0'):
				consecutiveZero += 1
				maxConsZero = max(consecutiveZero, maxConsZero)
		else:
			one += 1
			consecutiveZero = 0
	if one == 0:
		print(1)
	elif zero == 0:
		print(0)
	elif zero == 1:
		print(1)
	elif maxConsZero > 0 and maxConsZero + 1 == zero:
		print(1)
	else:
		print(2)


for i in range(int(input())):
	s = input()
	n = len(s)
	c = s.count('0')
	s1 = c * '0'
	if s1 in s and c > 0:
		print(1)
	elif c > 1:
		print(2)
	else:
		print(c)


for _ in range(int(input())):
	s = input().strip('\n')
	li = s.split('1')
	print(min(len(li) - li.count(''), 2))


t = int(input())
ans = []
for _ in range(t):
	s = input()
	flag1 = False
	flag2 = False
	mx = 0
	for i in s:
		if i == '0' and (not flag2):
			if not flag1:
				flag1 = True
				mx += 1
		if i == '0' and flag2:
			mx = 2
			break
		if i == '1' and flag1:
			flag2 = True
	ans.append(mx)
print(*ans, sep='\n')


from collections import deque, Counter
from collections import defaultdict as dfd
from bisect import bisect, bisect_left
from math import sqrt, gcd, ceil, factorial, log2
from heapq import heapify, heappush, heappop
MOD = 10 ** 9 + 7
inf = float('inf')
ans_ = []

def nin():
	return int(input())

def ninf():
	return int(file.readline())

def st():
	return input().strip()

def stf():
	return file.readline().strip()

def read():
	return list(map(int, input().strip().split()))

def readf():
	return list(map(int, file.readline().strip().split()))

def readfl():
	return list(map(float, input().strip().split()))

def readffl():
	return list(map(float, file.readline().strip().split()))

def f(s):
	ans = 0
	s += '#'
	x = 1
	for i in range(1, len(s)):
		if s[i] != s[i - 1]:
			if s[i - 1] == '0':
				ans += 1
			x = 1
		else:
			x += 1
	return ans

def solve():
	for _ in range(nin()):
		s = st()
		ans_.append(min(2, f(s)))
solve()
for i in ans_:
	print(i)


def MEX(e):
	if e == 0:
		return 1
	if e == 1:
		return 0
for T in range(int(input())):
	binary = input()
	change = 0
	flag = ''
	for i in range(1, len(binary)):
		if binary[i] != binary[i - 1]:
			change += 1
			if not flag:
				flag = str(binary[i])
	if change == 0:
		print(MEX(int(binary[0])))
	elif change == 1:
		print(1)
	elif change == 2 and flag == '0':
		print(1)
	else:
		print(2)


t = int(input())
for i in range(t):
	s = input()
	z = s.count('0')
	if z == 0:
		print(0)
		continue
	first = s.find('0')
	last = s.rfind('0')
	if last - first + 1 == z:
		print(1)
	else:
		print(2)


from collections import Counter

def answer():
	s = list(input())
	s = [int(i) for i in s]
	c = Counter(s)
	if c[0] == 0:
		print(0)
		return
	n1 = c[1]
	tS = sum(s[:n1])
	if tS == n1:
		print(1)
		return
	for i in range(n1):
		tS -= s[n1 - 1 - i]
		tS += s[-1 - i]
		if tS == n1:
			print(1)
			return
	print(2)
t = int(input())
for _ in range(t):
	answer()


import math, sys
from collections import defaultdict, deque
import bisect as bi

def yes():
	print('YES')

def no():
	print('NO')

def I():
	return int(sys.stdin.readline())

def In():
	return map(int, sys.stdin.readline().split())

def Sn():
	return sys.stdin.readline().strip()

def ceil(a, b):
	return (a + b - 1) // b

def dict(a):
	d = {}
	for x in a:
		if d.get(x, -1) != -1:
			d[x] += 1
		else:
			d[x] = 1
	return d

def find_gt(a, x):
	i = bi.bisect_left(a, x)
	if i != len(a):
		return i
	else:
		return -1

def main():
	try:
		s = Sn()
		ap = []
		cnt = 0
		last = s[0]
		for x in s[1:]:
			if x != last:
				ap.append(last)
				last = x
		ap.append(last)
		cnt = 0
		for x in ap:
			if x == '0':
				cnt += 1
		print(min(cnt, 2))
	except:
		pass
M = 998244353
P = 1000000007
for _ in range(I()):
	main()


import sys
input = lambda : sys.stdin.readline().rstrip('\r\n')
inp_vars_int = lambda : map(int, input().split())
inp_list_int = lambda : list(map(int, input().split()))
t = int(input())
for _ in range(t):
	s = input()
	zeros_subs = False
	res = 0
	for c in s:
		if c == '0':
			zeros_subs = True
		elif zeros_subs:
			res += 1
			zeros_subs = False
	print(min(res + zeros_subs, 2))


m = int(input())
l = []
for i in range(m):
	a = input()
	b = a.count('0')
	c = len(a.replace('1', ' ').replace('2', ' ').split())
	if '0' not in a:
		d = 0
	elif '1' not in a:
		d = 1
	elif '2' not in a:
		d = 2
	l.append(min(b, c, d))
for i in l:
	print(i)


n = int(input())
for i in range(n):
	string = input()
	summ = 0
	flag = False
	for i in range(len(string)):
		if summ < 2:
			if string[i] == '0':
				if not flag:
					summ += 1
				flag = True
			else:
				flag = False
	print(summ)


def minMaxCut(s):
	ans = 0
	t = 1
	for i in range(1, len(s)):
		if s[i] == s[i - 1] and s[i] == '0':
			t += 1
			continue
		elif s[i] == '1' and s[i - 1] == '0':
			t = 1
			ans += 1
		elif s[i] != '0' and t != 1:
			t = 1
			ans += 1
	if t != 1 or s[len(s) - 1] == '0':
		ans += 1
	return min(ans, 2)
for _ in range(int(input())):
	s = input()
	print(minMaxCut(s))


n = int(input())
for i in range(1, n + 1):
	a = input()
	a = a.replace('1', ' ')
	a = a.split()
	b = len(a)
	if b > 2:
		b = 2
	print(b)


def removed(s):
	ans = ''
	ans = ans + s[0]
	seen = s[0]
	for i in range(1, len(s)):
		if s[i] != seen:
			ans = ans + s[i]
			seen = s[i]
	return ans
for _ in range(int(input())):
	s = input()
	print(min(removed(s).count('0'), 2))


test = int(input())
for _ in range(test):
	string = input()
	(result, i, length) = (0, 0, len(string))
	(first0, first1, second0, second1) = (-1, -1, -1, -1)
	changes = 0
	start = string[0]
	for i in range(1, length):
		if string[i] != start:
			changes += 1
			start = string[i]
	if changes == 0:
		print(1 - int(start))
	elif changes == 1 or (changes == 2 and string[0] == '1'):
		print(1)
	else:
		print(2)


def solve():
	s = input()
	n = len(s)
	m = s.count('1')
	if m == 0:
		return 1
	if m == n:
		return 0
	k = 0
	for i in range(n - 1):
		if s[i] == '0' and s[i + 1] == '1':
			k += 1
	if s[n - 1] == '0':
		k += 1
	return min(k, 2)
import sys
input = lambda : sys.stdin.readline().rstrip()
t = int(input())
for i in range(t):
	print(solve())


cases = int(input())
for i in range(cases):
	s = input()
	fir = s.count('0')
	if fir == 0:
		print(0)
		continue
	first = s.find('0')
	last = s.rfind('0')
	if last - first + 1 == fir:
		print(1)
	else:
		print(2)


import math
x = int(input())
for i in range(x):
	n = input()
	x = n.split('1')
	y = n.split('0')
	a = []
	b = []
	count0 = 0
	count1 = 0
	for j in x:
		if j != '':
			a.append(j)
			count0 += 1
	for k in y:
		if k != '':
			b.append(k)
			count1 += 1
	if b == []:
		print(1)
	elif count1 == count0 == 1:
		print(1)
	elif count1 == count0:
		print(2)
	elif count0 >= count1:
		print(2)
	elif count0 < count1 and count0 < 2:
		print(count0)
	elif count0 < count1 and count0 >= 2:
		print(2)


from math import ceil
t = int(input())
inputs = [None] * t
for i in range(t):
	inputs[i] = input()
for row in inputs:
	mex = 0
	is_zero_matched = 0
	for (i, num) in enumerate(row):
		if is_zero_matched == 0 and int(num) == 0:
			is_zero_matched = 1
			mex = mex + 1
		elif int(num) == 1:
			is_zero_matched = 0
		if mex == 2:
			break
	print(mex)


for _ in range(int(input())):
	s = input()
	c = 0
	for i in range(len(s) - 1):
		if c >= 3:
			break
		if s[i] != s[i + 1]:
			c += 1
	if c == 0:
		print(1 - int(s[0]))
	elif c == 1:
		print(1)
	elif c == 2:
		if s[0] == '1':
			print(1)
		else:
			print(2)
	else:
		print(2)


t = int(input())
while t > 0:
	s = input()
	cou = 0
	ret = 0
	for i in s:
		if i == '0':
			cou = 1
		if i == '1' and cou == 1:
			cou = 0
			ret += 1
	if s[len(s) - 1] == '0':
		ret += 1
	pri = min(ret, 2)
	print(pri)
	t -= 1