D = [[-1, 0], [1, 0], [0, -1], [0, 1]]

def puyo_rensa(cnt, fld)
  check = 8.times.map{ [false] * 14 }
  res = 0
  (1..6).each do |i|
    (1..12).each do |j|
      if !check[i][j] && fld[i][j] =~ /[^O.]/
        puyo_connector(i, j,fld){ |arr|
          if arr.size >= 4
            arr.each do |s, t|
              D.each do |x, y|
                p, q = s + x, t + y
                check[p][q] = true if fld[p][q] == 'O'
              end
              check[s][t] = true
            end
            res = 1
          end
        }
      end
    end
  end
  (1..6).each do |i|
    fld[i].map!.with_index{ |c, j| c unless check[i][j] }.compact!
    (14 - fld[i].size).times{ fld[i].unshift('.') }
  end
  if res == 1 then puyo_rensa(cnt + 1, fld)
  else cnt
  end
end

def puyo_connector(i, j, fld)
  check = 8.times.map{ [false] * 14 }
  arr = [[i, j]]
  ret = [[i, j]]
  check[i][j] = true
  until arr.empty?
    i, j = arr.shift
    D.each do |x, y|
      p, q = i + x,  j + y
      if !check[p][q] && fld[i][j] == fld[p][q]
        check[p][q] = true
        arr << [p, q]
        ret << [p, q]
      end
    end
  end
  yield(ret)
end

gets.to_i.times do
  fld = 8.times.map{ ['.'] * 14 }
  12.times do |i|
    gets.chomp.split('').each_with_index do |c, j|
      fld[j+1][i+1] = c
    end
  end
  p puyo_rensa(0, fld)
end