#! ruby # Try yukicoder # author: Leonardone @ NEETSDKASU ############################################################ def gs() gets.chomp end def gi() gets.to_i end def gf() gets.to_f end def gss() gs.split end def gis() gss.map(&:to_i) end def gfs() gss.map(&:to_f) end def nmapf(n,f) n.times.map{ __send__ f } end def ngs(n) nmapf n,:gs end def ngi(n) nmapf n,:gi end def ngss(n) nmapf n,:gss end def ngis(n) nmapf n,:gis end def arr2d(h,w,v=0) h.times.map{[v] * w} end def for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end def nsum(n) n * (n + 1) / 2 end def vcount(d,r=Hash.new(0)) d.inject(r){|r,e| r[e]+=1;r} end ############################################################ def isKadomatuRetsu(a1, a2, a3) a1 != a3 && ((a2 > a1 && a2 > a3) || (a2 < a1 && a2 < a3)) end def checkNotKadomatsuRetsu(a1, a2, a3) return true if a1 > 0 && a2 > 0 && a1 == a2 return true if a2 > 0 && a3 > 0 && a2 == a3 return true if a1 > 0 && a3 > 0 && a1 == a3 return false if [a1, a2, a3].include? 0 return !isKadomatuRetsu(a1, a2, a3) end def isKadomatuGyoretsu(a) return false if checkNotKadomatsuRetsu(a[0][0], a[0][1], a[0][2]) return false if checkNotKadomatsuRetsu(a[1][0], a[1][1], a[1][2]) return false if checkNotKadomatsuRetsu(a[2][0], a[2][1], a[2][2]) return false if checkNotKadomatsuRetsu(a[0][0], a[1][0], a[2][0]) return false if checkNotKadomatsuRetsu(a[0][1], a[1][1], a[2][1]) return false if checkNotKadomatsuRetsu(a[0][2], a[1][2], a[2][2]) return false if checkNotKadomatsuRetsu(a[0][0], a[1][1], a[2][2]) return false if checkNotKadomatsuRetsu(a[0][2], a[1][1], a[2][0]) return true end t = gi t.times do l = gi a = ngis 3 if !isKadomatuGyoretsu(a) puts 0 next end k = 0 xy = [] 3.times do |i| 3.times do |j| if a[i][j] == 0 xy << [i, j] end end end ans = 0 i1, j1 = xy[0] i2, j2 = xy[1] 1.upto(l - 1) do |x| y = l - x a[i1][j1] = x a[i2][j2] = y ans += 1 if isKadomatuGyoretsu(a) end puts ans end