import scala.io.StdIn.readLine

object Solution {
  val mod: Int = Math.pow(10, 9).toInt + 7

  def main(args: Array[String]): Unit = {
    //    val src = io.Source.fromFile("/usr/share/dict/words")
    //    val lines = src.getLines()
    //    src.close()
    val t = readLine().toInt
    for (i <- 1 to t) {
      val nm = readIntLine()
      val grid = (1 to nm.head).map(_ => readLine().toCharArray).toArray
      val couldBlockBadGuys = blockBadGuys(grid, nm.head, nm.last)
      val canGoodGuysReach = reachGood(grid, nm.head, nm.last)
      println(if (couldBlockBadGuys && canGoodGuysReach) "Yes" else "No")
    }
  }

  def reachGood(grid: Array[Array[Char]], n: Int, m: Int): Boolean = {
    val marked = Array.ofDim[Boolean](n, m)

    def dfs(i: Int, j: Int): Unit = {
      if (i > 0 && !marked(i - 1)(j) && grid(i - 1)(j) != '#' &&  grid(i - 1)(j) != 'B') {
        marked(i - 1)(j) = true
        dfs(i - 1, j)
      }
      if (j > 0 && !marked(i)(j - 1) && grid(i)(j - 1) != '#' && grid(i)(j - 1) != 'B') {
        marked(i)(j - 1) = true
        dfs(i, j - 1)
      }
      if (i < n - 1 && !marked(i + 1)(j) && grid(i + 1)(j) != '#'&& grid(i + 1)(j) != 'B') {
        marked(i + 1)(j) = true
        dfs(i + 1, j)
      }
      if (j < m - 1 && !marked(i)(j + 1) && grid(i)(j + 1) != '#' && grid(i)(j + 1) != 'B') {
        marked(i)(j + 1) = true
        dfs(i, j + 1)
      }
    }

    if (grid(n - 1)(m - 1) != '#' && grid(n - 1)(m - 1) != 'B') {
      marked(n - 1)(m - 1) = true
      dfs(n - 1, m - 1)
    }
    for (i <- marked.indices) {
      for (j <- marked(i).indices) {
        if (grid(i)(j) == 'G' && !marked(i)(j)) return false
      }
    }
    grid(n - 1)(m - 1) != 'B'
  }

  def blockBadGuys(grid: Array[Array[Char]], n: Int, m: Int): Boolean = {
    for (i <- grid.indices) {
      for (j <- grid(i).indices) {
        if (grid(i)(j) == 'B') {
          if (i > 0) {
            if (grid(i - 1)(j) == 'G') return false
            if (grid(i - 1)(j) != 'B') grid(i - 1)(j) = '#'
          }
          if (j > 0) {
            if (grid(i)(j - 1) == 'G') return false
            if (grid(i)(j - 1) != 'B') grid(i)(j - 1) = '#'
          }
          if (i < n - 1) {
            if (grid(i + 1)(j) == 'G') return false
            if (grid(i + 1)(j) != 'B') grid(i + 1)(j) = '#'
          }
          if (j < m - 1) {
            if (grid(i)(j + 1) == 'G') return false
            if (grid(i)(j + 1) != 'B') grid(i)(j + 1) = '#'
          }
        }
      }
    }

    true
  }

  def matching(first: Array[Int], second: Array[Int]): Int = {
    val n = first.length
    val mapInt = Array.ofDim[Int](first.length)
    for (i <- first.indices) {
      mapInt(first(i) - 1) = i + 1
    }

    val differences = Array.ofDim[Int](second.length)
    for (i <- first.indices) {
      differences( (mapInt(second(i) - 1) - i - 1 + n) % n) += 1
    }

    var max = 0
    for (i <- differences.indices) {
      if (differences(i) > max)
        max = differences(i)
    }

    max
  }

  def readIntLine(): List[Int] = {
    readLine().split(" ").map(_.toInt).toList
  }

  def readLongLine(): List[Long] = {
    readLine().split(" ").map(_.toLong).toList
  }

  def printArray[T](arr: Array[T]): Unit = {
    print(arr(0))
    for (i <- 1 until arr.length) {
      print(" " + arr(i))
    }
    println()
  }

}

object D {
  import InOut._
  def main(args: Array[String]): Unit = {}

  for (_ <- 1 to nextInt) {
    val n, m = nextInt
    val maze = Array.fill(n){ nextLine.toCharArray }

    @inline def get(r: Int, c: Int): Char = {
      if (r >= 0 && r < n && c >= 0 && c < m) maze(r)(c) else '#'
    }

    var can = true

    var r = 0
    while (r < n) {
      var c = 0
      while (c < m) {
        if (maze(r)(c) == '.' &&
          ((r > 0 && maze(r - 1)(c) == 'B') ||
            (r < n - 1 && maze(r + 1)(c) == 'B') ||
            (c > 0 && maze(r)(c - 1) == 'B') ||
            (c < m - 1 && maze(r)(c + 1) == 'B'))) {
          maze(r)(c) = '#'
        } else if (maze(r)(c) == 'G' &&
          ((r > 0 && maze(r - 1)(c) == 'B') ||
            (r < n - 1 && maze(r + 1)(c) == 'B') ||
            (c > 0 && maze(r)(c - 1) == 'B') ||
            (c < m - 1 && maze(r)(c + 1) == 'B'))) {
          can = false
        }
        c += 1
      }
      r += 1
    }

    def dfs(r: Int, c: Int): Unit = {
      maze(r)(c) = '#'
      if (r > 0 && maze(r - 1)(c) != '#') dfs(r - 1, c)
      if (r < n - 1 && maze(r + 1)(c) != '#') dfs(r + 1, c)
      if (c > 0 && maze(r)(c - 1) != '#') dfs(r, c - 1)
      if (c < m - 1 && maze(r)(c + 1) != '#') dfs(r, c + 1)
    }

    val s0 = maze(n - 1)(m - 1)
    if (s0 == 'B') can = false
    if ((s0 == 'G' || s0 == '.') && can) {
      dfs(n - 1, m - 1)
    }

    r = 0
    while (r < n) {
      var c = 0
      while (c < m) {
        if (maze(r)(c) == 'G') can = false
        c += 1
      }
      r += 1
    }

    val res = can

    out.println(if (res) "Yes" else "No")

    out.flush()
  }

  final object InOut {
    val in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in))
    val out = new java.io.PrintWriter(System.out, false)
    private[this] var tokenizer: java.util.StringTokenizer = _
    def nextToken: String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new java.util.StringTokenizer(in.readLine)
      tokenizer.nextToken
    }
    def nextInt = Integer.parseInt(nextToken)
    def nextLong = java.lang.Long.parseLong(nextToken)
    def nextBig = BigInt(nextToken)
    def nextInts(n: Int) = Array.fill(n) { nextInt }
    def nextLongs(n: Int) = Array.fill(n) { nextLong }
    def nextBigs(n: Int) = Array.fill(n) { nextBig }
    def nextLine = in.readLine
  }
}

object D {

  import InOut._
  def main(args: Array[String]): Unit = {

  for (_ <- 1 to nextInt) {
    val n, m = nextInt
    val maze = Array.fill(n){ nextLine.toCharArray }

    @inline def get(r: Int, c: Int): Char = {
      if (r >= 0 && r < n && c >= 0 && c < m) maze(r)(c) else '#'
    }

    var can = true

    var r = 0
    while (r < n) {
      var c = 0
      while (c < m) {
        if (maze(r)(c) == '.' &&
          (get(r - 1, c) == 'B' || get(r + 1, c) == 'B' || get(r, c - 1) == 'B' || get(r, c + 1) == 'B')) {
          maze(r)(c) = '#'
        } else if (maze(r)(c) == 'G' &&
          (get(r - 1, c) == 'B' || get(r + 1, c) == 'B' || get(r, c - 1) == 'B' || get(r, c + 1) == 'B')) {
          can = false
        }
        c += 1
      }
      r += 1
    }

    def dfs(r: Int, c: Int): Unit = {
      maze(r)(c) = '#'
      if (r > 0 && maze(r - 1)(c) != '#') dfs(r - 1, c)
      if (r < n - 1 && maze(r + 1)(c) != '#') dfs(r + 1, c)
      if (c > 0 && maze(r)(c - 1) != '#') dfs(r, c - 1)
      if (c < m - 1 && maze(r)(c + 1) != '#') dfs(r, c + 1)
    }

    val s0 = maze(n - 1)(m - 1)
    if (s0 == 'B') can = false
    if ((s0 == 'G' || s0 == '.') && can) {
      dfs(n - 1, m - 1)
    }

    r = 0
    while (r < n) {
      var c = 0
      while (c < m) {
        if (maze(r)(c) == 'G') can = false
        c += 1
      }
      r += 1
    }

    val res = can

    out.println(if (res) "Yes" else "No")

    out.flush()
  }
}
  final object InOut {
    val in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in))
    val out = new java.io.PrintWriter(System.out, false)
    private[this] var tokenizer: java.util.StringTokenizer = _
    def nextToken: String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new java.util.StringTokenizer(in.readLine)
      tokenizer.nextToken
    }
    def nextInt = Integer.parseInt(nextToken)
    def nextLong = java.lang.Long.parseLong(nextToken)
    def nextBig = BigInt(nextToken)
    def nextInts(n: Int) = Array.fill(n) { nextInt }
    def nextLongs(n: Int) = Array.fill(n) { nextLong }
    def nextBigs(n: Int) = Array.fill(n) { nextBig }
    def nextLine = in.readLine
  }
}

import scala.io.StdIn.readLine

object Solution {
  val mod: Int = Math.pow(10, 9).toInt + 7

  def main(args: Array[String]): Unit = {
    //    val src = io.Source.fromFile("/usr/share/dict/words")
    //    val lines = src.getLines()
    //    src.close()
    val t = readLine().toInt
    for (i <- 1 to t) {
      val nm = readIntLine()
      val grid = (1 to nm.head).map(_ => readLine().toCharArray).toArray
      val couldBlockBadGuys = blockBadGuys(grid, nm.head, nm.last)
      val canGoodGuysReach = reachGood(grid, nm.head, nm.last)
      println(if (couldBlockBadGuys && canGoodGuysReach) "Yes" else "No")
    }
  }

  def reachGood(grid: Array[Array[Char]], n: Int, m: Int): Boolean = {
    val marked = Array.ofDim[Boolean](n, m)

    def dfs(i: Int, j: Int): Unit = {
      if (i > 0 && !marked(i - 1)(j) && grid(i - 1)(j) != 'W') {
        marked(i - 1)(j) = true
        dfs(i - 1, j)
      }
      if (j > 0 && !marked(i)(j - 1) && grid(i)(j - 1) != 'W') {
        marked(i)(j - 1) = true
        dfs(i, j - 1)
      }
      if (i < n - 1 && !marked(i + 1)(j) && grid(i + 1)(j) != 'W') {
        marked(i + 1)(j) = true
        dfs(i + 1, j)
      }
      if (j < m - 1 && !marked(i)(j + 1) && grid(i)(j + 1) != 'W') {
        marked(i)(j + 1) = true
        dfs(i, j + 1)
      }
    }

    if (grid(n - 1)(m - 1) != 'W' && grid(n - 1)(m - 1) != 'B') dfs(n - 1, m - 1)
    for (i <- marked.indices) {
      for (j <- marked(i).indices) {
        if (grid(i)(j) == 'G' && ! marked(i)(j)) return false
      }
    }
    true
  }

  def blockBadGuys(grid: Array[Array[Char]], n: Int, m: Int): Boolean = {
    for (i <- grid.indices) {
      for (j <- grid(i).indices) {
        if (grid(i)(j) == 'B') {
          if (i > 0) {
            if (grid(i - 1)(j) == 'G') return false
            if (grid(i - 1)(j) != 'B') grid(i - 1)(j) = 'W'
          }
          if (j > 0) {
            if (grid(i)(j - 1) == 'G') return false
            if (grid(i)(j - 1) != 'B') grid(i)(j - 1) = 'W'
          }
          if (i < n - 1) {
            if (grid(i + 1)(j) == 'G') return false
            if (grid(i + 1)(j) != 'B') grid(i + 1)(j) = 'W'
          }
          if (j < m - 1) {
            if (grid(i)(j + 1) == 'G') return false
            if (grid(i)(j + 1) != 'B') grid(i)(j + 1) = 'W'
          }
        }
      }
    }

    true
  }

  def matching(first: Array[Int], second: Array[Int]): Int = {
    val n = first.length
    val mapInt = Array.ofDim[Int](first.length)
    for (i <- first.indices) {
      mapInt(first(i) - 1) = i + 1
    }

    val differences = Array.ofDim[Int](second.length)
    for (i <- first.indices) {
      differences( (mapInt(second(i) - 1) - i - 1 + n) % n) += 1
    }

    var max = 0
    for (i <- differences.indices) {
      if (differences(i) > max)
        max = differences(i)
    }

    max
  }

  def readIntLine(): List[Int] = {
    readLine().split(" ").map(_.toInt).toList
  }

  def readLongLine(): List[Long] = {
    readLine().split(" ").map(_.toLong).toList
  }

  def printArray[T](arr: Array[T]): Unit = {
    print(arr(0))
    for (i <- 1 until arr.length) {
      print(" " + arr(i))
    }
    println()
  }

}

import scala.io.StdIn.readLine

object Solution {
  val mod: Int = Math.pow(10, 9).toInt + 7

  def main(args: Array[String]): Unit = {
    //    val src = io.Source.fromFile("/usr/share/dict/words")
    //    val lines = src.getLines()
    //    src.close()
    val t = readLine().toInt
    for (i <- 1 to t) {
      val nm = readIntLine()
      val grid = (1 to nm.head).map(_ => readLine().toCharArray).toArray
      val couldBlockBadGuys = blockBadGuys(grid, nm.head, nm.last)
      val canGoodGuysReach = reachGood(grid, nm.head, nm.last)
      println(if (couldBlockBadGuys && canGoodGuysReach) "Yes" else "No")
    }
  }

  def reachGood(grid: Array[Array[Char]], n: Int, m: Int): Boolean = {
    val marked = Array.ofDim[Boolean](n, m)

    def dfs(i: Int, j: Int): Unit = {
      if (i > 0 && !marked(i - 1)(j) && grid(i - 1)(j) != 'W') {
        marked(i - 1)(j) = true
        dfs(i - 1, j)
      }
      if (j > 0 && !marked(i)(j - 1) && grid(i)(j - 1) != 'W') {
        marked(i)(j - 1) = true
        dfs(i, j - 1)
      }
      if (i < n - 1 && !marked(i + 1)(j) && grid(i + 1)(j) != 'W') {
        marked(i + 1)(j) = true
        dfs(i + 1, j)
      }
      if (j < m - 1 && !marked(i)(j + 1) && grid(i)(j + 1) != 'W') {
        marked(i)(j + 1) = true
        dfs(i, j + 1)
      }
    }

    if (grid(n - 1)(m - 1) != 'W' && grid(n - 1)(m - 1) != 'B') {
      dfs(n - 1, m - 1)
      marked(n - 1)(m - 1) = true
    }
    for (i <- marked.indices) {
      for (j <- marked(i).indices) {
        if (grid(i)(j) == 'G' && ! marked(i)(j)) return false
      }
    }
    true
  }

  def blockBadGuys(grid: Array[Array[Char]], n: Int, m: Int): Boolean = {
    for (i <- grid.indices) {
      for (j <- grid(i).indices) {
        if (grid(i)(j) == 'B') {
          if (i > 0) {
            if (grid(i - 1)(j) == 'G') return false
            if (grid(i - 1)(j) != 'B') grid(i - 1)(j) = 'W'
          }
          if (j > 0) {
            if (grid(i)(j - 1) == 'G') return false
            if (grid(i)(j - 1) != 'B') grid(i)(j - 1) = 'W'
          }
          if (i < n - 1) {
            if (grid(i + 1)(j) == 'G') return false
            if (grid(i + 1)(j) != 'B') grid(i + 1)(j) = 'W'
          }
          if (j < m - 1) {
            if (grid(i)(j + 1) == 'G') return false
            if (grid(i)(j + 1) != 'B') grid(i)(j + 1) = 'W'
          }
        }
      }
    }

    true
  }

  def matching(first: Array[Int], second: Array[Int]): Int = {
    val n = first.length
    val mapInt = Array.ofDim[Int](first.length)
    for (i <- first.indices) {
      mapInt(first(i) - 1) = i + 1
    }

    val differences = Array.ofDim[Int](second.length)
    for (i <- first.indices) {
      differences( (mapInt(second(i) - 1) - i - 1 + n) % n) += 1
    }

    var max = 0
    for (i <- differences.indices) {
      if (differences(i) > max)
        max = differences(i)
    }

    max
  }

  def readIntLine(): List[Int] = {
    readLine().split(" ").map(_.toInt).toList
  }

  def readLongLine(): List[Long] = {
    readLine().split(" ").map(_.toLong).toList
  }

  def printArray[T](arr: Array[T]): Unit = {
    print(arr(0))
    for (i <- 1 until arr.length) {
      print(" " + arr(i))
    }
    println()
  }

}

import scala.io.StdIn.readLine

object Solution {
  val mod: Int = Math.pow(10, 9).toInt + 7

  def main(args: Array[String]): Unit = {
    //    val src = io.Source.fromFile("/usr/share/dict/words")
    //    val lines = src.getLines()
    //    src.close()
    val t = readLine().toInt
    for (i <- 1 to t) {
      val nm = readIntLine()
      val grid = (1 to nm.head).map(_ => readLine().toCharArray).toArray
      val couldBlockBadGuys = blockBadGuys(grid, nm.head, nm.last)
      val canGoodGuysReach = reachGood(grid, nm.head, nm.last)
      println(if (couldBlockBadGuys && canGoodGuysReach) "Yes" else "No")
    }
  }

  def reachGood(grid: Array[Array[Char]], n: Int, m: Int): Boolean = {
    val marked = Array.ofDim[Boolean](n, m)

    def dfs(i: Int, j: Int): Unit = {
      if (i > 0 && !marked(i - 1)(j) && grid(i - 1)(j) != 'W') {
        marked(i - 1)(j) = true
        dfs(i - 1, j)
      }
      if (j > 0 && !marked(i)(j - 1) && grid(i)(j - 1) != 'W') {
        marked(i)(j - 1) = true
        dfs(i, j - 1)
      }
      if (i < n - 1 && !marked(i + 1)(j) && grid(i + 1)(j) != 'W') {
        marked(i + 1)(j) = true
        dfs(i + 1, j)
      }
      if (j < m - 1 && !marked(i)(j + 1) && grid(i)(j + 1) != 'W') {
        marked(i)(j + 1) = true
        dfs(i, j + 1)
      }
    }

    if (grid(n - 1)(m - 1) != 'W' && grid(n - 1)(m - 1) != 'B') {
      marked(n - 1)(m - 1) = true
      dfs(n - 1, m - 1)
    }
    for (i <- marked.indices) {
      for (j <- marked(i).indices) {
        if (grid(i)(j) == 'G' && !marked(i)(j)) return false
      }
    }
    grid(n - 1)(m - 1) != 'B'
  }

  def blockBadGuys(grid: Array[Array[Char]], n: Int, m: Int): Boolean = {
    for (i <- grid.indices) {
      for (j <- grid(i).indices) {
        if (grid(i)(j) == 'B') {
          if (i > 0) {
            if (grid(i - 1)(j) == 'G') return false
            if (grid(i - 1)(j) != 'B') grid(i - 1)(j) = 'W'
          }
          if (j > 0) {
            if (grid(i)(j - 1) == 'G') return false
            if (grid(i)(j - 1) != 'B') grid(i)(j - 1) = 'W'
          }
          if (i < n - 1) {
            if (grid(i + 1)(j) == 'G') return false
            if (grid(i + 1)(j) != 'B') grid(i + 1)(j) = 'W'
          }
          if (j < m - 1) {
            if (grid(i)(j + 1) == 'G') return false
            if (grid(i)(j + 1) != 'B') grid(i)(j + 1) = 'W'
          }
        }
      }
    }

    true
  }

  def matching(first: Array[Int], second: Array[Int]): Int = {
    val n = first.length
    val mapInt = Array.ofDim[Int](first.length)
    for (i <- first.indices) {
      mapInt(first(i) - 1) = i + 1
    }

    val differences = Array.ofDim[Int](second.length)
    for (i <- first.indices) {
      differences( (mapInt(second(i) - 1) - i - 1 + n) % n) += 1
    }

    var max = 0
    for (i <- differences.indices) {
      if (differences(i) > max)
        max = differences(i)
    }

    max
  }

  def readIntLine(): List[Int] = {
    readLine().split(" ").map(_.toInt).toList
  }

  def readLongLine(): List[Long] = {
    readLine().split(" ").map(_.toLong).toList
  }

  def printArray[T](arr: Array[T]): Unit = {
    print(arr(0))
    for (i <- 1 until arr.length) {
      print(" " + arr(i))
    }
    println()
  }

}

import scala.io.StdIn.readLine

object Solution {
  val mod: Int = Math.pow(10, 9).toInt + 7

  def main(args: Array[String]): Unit = {
    //    val src = io.Source.fromFile("/usr/share/dict/words")
    //    val lines = src.getLines()
    //    src.close()
    val t = readLine().toInt
    for (i <- 1 to t) {
      val nm = readIntLine()
      val grid = (1 to nm.head).map(_ => readLine().toCharArray).toArray
      val couldBlockBadGuys = blockBadGuys(grid, nm.head, nm.last)
      val canGoodGuysReach = reachGood(grid, nm.head, nm.last)
      println(if (couldBlockBadGuys && canGoodGuysReach) "Yes" else "No")
    }
  }

  def reachGood(grid: Array[Array[Char]], n: Int, m: Int): Boolean = {
    val marked = Array.ofDim[Boolean](n, m)

    def dfs(i: Int, j: Int): Unit = {
      if (i > 0 && !marked(i - 1)(j) && grid(i - 1)(j) != '#' &&  grid(i - 1)(j) != 'B') {
        marked(i - 1)(j) = true
        dfs(i - 1, j)
      }
      if (j > 0 && !marked(i)(j - 1) && grid(i)(j - 1) != '#' && grid(i)(j - 1) != 'B') {
        marked(i)(j - 1) = true
        dfs(i, j - 1)
      }
      if (i < n - 1 && !marked(i + 1)(j) && grid(i + 1)(j) != '#'&& grid(i + 1)(j) != 'B') {
        marked(i + 1)(j) = true
        dfs(i + 1, j)
      }
      if (j < m - 1 && !marked(i)(j + 1) && grid(i)(j + 1) != '#' && grid(i)(j + 1) != 'B') {
        marked(i)(j + 1) = true
        dfs(i, j + 1)
      }
    }

    if (grid(n - 1)(m - 1) != '#' && grid(n - 1)(m - 1) != 'B') {
      marked(n - 1)(m - 1) = true
      dfs(n - 1, m - 1)
    }
    for (i <- marked.indices) {
      for (j <- marked(i).indices) {
        if (grid(i)(j) == 'G' && !marked(i)(j)) return false
      }
    }
    grid(n - 1)(m - 1) != 'B'
  }

  def blockBadGuys(grid: Array[Array[Char]], n: Int, m: Int): Boolean = {
    for (i <- grid.indices) {
      for (j <- grid(i).indices) {
        if (grid(i)(j) == 'B') {
          if (i > 0) {
            if (grid(i - 1)(j) == 'G') return false
            if (grid(i - 1)(j) != 'B') grid(i - 1)(j) = 'W'
          }
          if (j > 0) {
            if (grid(i)(j - 1) == 'G') return false
            if (grid(i)(j - 1) != 'B') grid(i)(j - 1) = 'W'
          }
          if (i < n - 1) {
            if (grid(i + 1)(j) == 'G') return false
            if (grid(i + 1)(j) != 'B') grid(i + 1)(j) = 'W'
          }
          if (j < m - 1) {
            if (grid(i)(j + 1) == 'G') return false
            if (grid(i)(j + 1) != 'B') grid(i)(j + 1) = 'W'
          }
        }
      }
    }

    true
  }

  def matching(first: Array[Int], second: Array[Int]): Int = {
    val n = first.length
    val mapInt = Array.ofDim[Int](first.length)
    for (i <- first.indices) {
      mapInt(first(i) - 1) = i + 1
    }

    val differences = Array.ofDim[Int](second.length)
    for (i <- first.indices) {
      differences( (mapInt(second(i) - 1) - i - 1 + n) % n) += 1
    }

    var max = 0
    for (i <- differences.indices) {
      if (differences(i) > max)
        max = differences(i)
    }

    max
  }

  def readIntLine(): List[Int] = {
    readLine().split(" ").map(_.toInt).toList
  }

  def readLongLine(): List[Long] = {
    readLine().split(" ").map(_.toLong).toList
  }

  def printArray[T](arr: Array[T]): Unit = {
    print(arr(0))
    for (i <- 1 until arr.length) {
      print(" " + arr(i))
    }
    println()
  }

}

import scala.io.StdIn.readLine

object Solution {
  val mod: Int = Math.pow(10, 9).toInt + 7

  def main(args: Array[String]): Unit = {
    //    val src = io.Source.fromFile("/usr/share/dict/words")
    //    val lines = src.getLines()
    //    src.close()
    val t = readLine().toInt
    for (i <- 1 to t) {
      val nm = readIntLine()
      val grid = (1 to nm.head).map(_ => readLine().toCharArray).toArray
      val couldBlockBadGuys = blockBadGuys(grid, nm.head, nm.last)
      val canGoodGuysReach = reachGood(grid, nm.head, nm.last)
      println(if (couldBlockBadGuys && canGoodGuysReach) "Yes" else "No")
    }
  }

  def reachGood(grid: Array[Array[Char]], n: Int, m: Int): Boolean = {
    val marked = Array.ofDim[Boolean](n, m)

    def dfs(i: Int, j: Int): Unit = {
      if (i > 0 && !marked(i - 1)(j) && grid(i - 1)(j) != 'W' &&  grid(i - 1)(j) != 'B') {
        marked(i - 1)(j) = true
        dfs(i - 1, j)
      }
      if (j > 0 && !marked(i)(j - 1) && grid(i)(j - 1) != 'W' && grid(i)(j - 1) != 'B') {
        marked(i)(j - 1) = true
        dfs(i, j - 1)
      }
      if (i < n - 1 && !marked(i + 1)(j) && grid(i + 1)(j) != 'W'&& grid(i + 1)(j) != 'B') {
        marked(i + 1)(j) = true
        dfs(i + 1, j)
      }
      if (j < m - 1 && !marked(i)(j + 1) && grid(i)(j + 1) != 'W' && grid(i)(j + 1) != 'B') {
        marked(i)(j + 1) = true
        dfs(i, j + 1)
      }
    }

    if (grid(n - 1)(m - 1) != 'W' && grid(n - 1)(m - 1) != 'B') {
      marked(n - 1)(m - 1) = true
      dfs(n - 1, m - 1)
    }
    for (i <- marked.indices) {
      for (j <- marked(i).indices) {
        if (grid(i)(j) == 'G' && !marked(i)(j)) return false
      }
    }
    grid(n - 1)(m - 1) != 'B'
  }

  def blockBadGuys(grid: Array[Array[Char]], n: Int, m: Int): Boolean = {
    for (i <- grid.indices) {
      for (j <- grid(i).indices) {
        if (grid(i)(j) == 'B') {
          if (i > 0) {
            if (grid(i - 1)(j) == 'G') return false
            if (grid(i - 1)(j) != 'B') grid(i - 1)(j) = 'W'
          }
          if (j > 0) {
            if (grid(i)(j - 1) == 'G') return false
            if (grid(i)(j - 1) != 'B') grid(i)(j - 1) = 'W'
          }
          if (i < n - 1) {
            if (grid(i + 1)(j) == 'G') return false
            if (grid(i + 1)(j) != 'B') grid(i + 1)(j) = 'W'
          }
          if (j < m - 1) {
            if (grid(i)(j + 1) == 'G') return false
            if (grid(i)(j + 1) != 'B') grid(i)(j + 1) = 'W'
          }
        }
      }
    }

    true
  }

  def matching(first: Array[Int], second: Array[Int]): Int = {
    val n = first.length
    val mapInt = Array.ofDim[Int](first.length)
    for (i <- first.indices) {
      mapInt(first(i) - 1) = i + 1
    }

    val differences = Array.ofDim[Int](second.length)
    for (i <- first.indices) {
      differences( (mapInt(second(i) - 1) - i - 1 + n) % n) += 1
    }

    var max = 0
    for (i <- differences.indices) {
      if (differences(i) > max)
        max = differences(i)
    }

    max
  }

  def readIntLine(): List[Int] = {
    readLine().split(" ").map(_.toInt).toList
  }

  def readLongLine(): List[Long] = {
    readLine().split(" ").map(_.toLong).toList
  }

  def printArray[T](arr: Array[T]): Unit = {
    print(arr(0))
    for (i <- 1 until arr.length) {
      print(" " + arr(i))
    }
    println()
  }

}

object D {
  import InOut._
  def main(args: Array[String]): Unit = {}

  for (_ <- 1 to nextInt) {
    val n, m = nextInt
    val maze = Array.fill(n){ nextLine.toCharArray }

    def get(r: Int, c: Int): Char = {
      if (r >= 0 && r < n && c >= 0 && c < m) maze(r)(c) else '#'
    }

    for (r <- 0 until n) {
      for (c <- 0 until m) {
        if (maze(r)(c) == '.' &&
          (get(r - 1, c) == 'B' || get(r + 1, c) == 'B' || get(r, c - 1) == 'B' || get(r, c + 1) == 'B')) {
          maze(r)(c) = '#'
        }
      }
    }

    def dfs(r: Int, c: Int): Unit = {
      maze(r)(c) = '#'
      if (get(r - 1, c) != '#') dfs(r - 1, c)
      if (get(r + 1, c) != '#') dfs(r + 1, c)
      if (get(r, c - 1) != '#') dfs(r, c - 1)
      if (get(r, c + 1) != '#') dfs(r, c + 1)
    }

    val s0 = maze(n - 1)(m - 1)
    if (s0 == 'G' || s0 == '.') dfs(n - 1, m - 1)

    val res = !maze.exists(_.contains('G'))

    out.println(if (res) "Yes" else "No")

    out.flush()
  }

  final object InOut {
    val in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in))
    val out = new java.io.PrintWriter(System.out, false)
    private[this] var tokenizer: java.util.StringTokenizer = _
    def nextToken: String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new java.util.StringTokenizer(in.readLine)
      tokenizer.nextToken
    }
    def nextInt = Integer.parseInt(nextToken)
    def nextLong = java.lang.Long.parseLong(nextToken)
    def nextBig = BigInt(nextToken)
    def nextInts(n: Int) = Array.fill(n) { nextInt }
    def nextLongs(n: Int) = Array.fill(n) { nextLong }
    def nextBigs(n: Int) = Array.fill(n) { nextBig }
    def nextLine = in.readLine
  }
}

object D {
  import InOut._
  def main(args: Array[String]): Unit = {}

  for (_ <- 1 to nextInt) {
    val n, m = nextInt
    val maze = Array.fill(n){ nextLine.toCharArray }

    @inline def get(r: Int, c: Int): Char = {
      if (r >= 0 && r < n && c >= 0 && c < m) maze(r)(c) else '#'
    }

    var can = true

    var r = 0
    while (r < n) {
      var c = 0
      while (c < m) {
        if (maze(r)(c) == '.' &&
          (get(r - 1, c) == 'B' || get(r + 1, c) == 'B' || get(r, c - 1) == 'B' || get(r, c + 1) == 'B')) {
          maze(r)(c) = '#'
        } else if (maze(r)(c) == 'G' &&
          (get(r - 1, c) == 'B' || get(r + 1, c) == 'B' || get(r, c - 1) == 'B' || get(r, c + 1) == 'B')) {
          can = false
        }
        c += 1
      }
      r += 1
    }

    def dfs(r: Int, c: Int): Unit = {
      maze(r)(c) = '#'
      if (r > 0 && maze(r)(c) != '#') dfs(r - 1, c)
      if (r < n - 1 && maze(r + 1)(c) != '#') dfs(r + 1, c)
      if (c > 0 && maze(r)(c - 1) != '#') dfs(r, c - 1)
      if (c < m - 1 && maze(r)(c + 1) != '#') dfs(r, c + 1)
    }

    val s0 = maze(n - 1)(m - 1)
    if (s0 == 'B') can = false
    if ((s0 == 'G' || s0 == '.') && can) {
      dfs(n - 1, m - 1)
    }

    r = 0
    while (r < n) {
      var c = 0
      while (c < m) {
        if (maze(r)(c) == 'G') can = false
        c += 1
      }
      r += 1
    }

    val res = can

    out.println(if (res) "Yes" else "No")

    out.flush()
  }

  final object InOut {
    val in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in))
    val out = new java.io.PrintWriter(System.out, false)
    private[this] var tokenizer: java.util.StringTokenizer = _
    def nextToken: String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new java.util.StringTokenizer(in.readLine)
      tokenizer.nextToken
    }
    def nextInt = Integer.parseInt(nextToken)
    def nextLong = java.lang.Long.parseLong(nextToken)
    def nextBig = BigInt(nextToken)
    def nextInts(n: Int) = Array.fill(n) { nextInt }
    def nextLongs(n: Int) = Array.fill(n) { nextLong }
    def nextBigs(n: Int) = Array.fill(n) { nextBig }
    def nextLine = in.readLine
  }
}

object D {
  import InOut._
  def main(args: Array[String]): Unit = {}

  for (_ <- 1 to nextInt) {
    val n, m = nextInt
    val maze = Array.fill(n){ nextLine.toCharArray }

    @inline def get(r: Int, c: Int): Char = {
      if (r >= 0 && r < n && c >= 0 && c < m) maze(r)(c) else '#'
    }

    var can = true

    var r = 0
    while (r < n) {
      var c = 0
      while (c < m) {
        if (maze(r)(c) == '.' &&
          (get(r - 1, c) == 'B' || get(r + 1, c) == 'B' || get(r, c - 1) == 'B' || get(r, c + 1) == 'B')) {
          maze(r)(c) = '#'
        } else if (maze(r)(c) == 'G' &&
          (get(r - 1, c) == 'B' || get(r + 1, c) == 'B' || get(r, c - 1) == 'B' || get(r, c + 1) == 'B')) {
          can = false
        }
        c += 1
      }
      r += 1
    }

    def dfs(r: Int, c: Int): Unit = {
      maze(r)(c) = '#'
      if (r > 0 && maze(r)(c) != '#') dfs(r - 1, c)
      if (r < n - 1 && maze(r + 1)(c) != '#') dfs(r + 1, c)
      if (c > 0 && maze(r)(c - 1) != '#') dfs(r, c - 1)
      if (c < m - 1 && maze(r)(c + 1) != '#') dfs(r, c + 1)
    }

    val s0 = maze(n - 1)(m - 1)
    if ((s0 == 'G' || s0 == '.') && can) {
      dfs(n - 1, m - 1)
    }

    r = 0
    while (r < n) {
      var c = 0
      while (c < m) {
        if (maze(r)(c) == 'G') can = false
        c += 1
      }
      r += 1
    }

    val res = can

    out.println(if (res) "Yes" else "No")

    out.flush()
  }

  final object InOut {
    val in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in))
    val out = new java.io.PrintWriter(System.out, false)
    private[this] var tokenizer: java.util.StringTokenizer = _
    def nextToken: String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new java.util.StringTokenizer(in.readLine)
      tokenizer.nextToken
    }
    def nextInt = Integer.parseInt(nextToken)
    def nextLong = java.lang.Long.parseLong(nextToken)
    def nextBig = BigInt(nextToken)
    def nextInts(n: Int) = Array.fill(n) { nextInt }
    def nextLongs(n: Int) = Array.fill(n) { nextLong }
    def nextBigs(n: Int) = Array.fill(n) { nextBig }
    def nextLine = in.readLine
  }
}


object _948A extends CodeForcesApp {
  import Utils._, annotation._, collection.{mutable, Searching}, util._, control._, math.Ordering.Implicits._, Searching._, Math._

  override def apply(io: IO): io.type = {
    val r, c = io.read[Int]
    val pasture = io.read[Vector, String](r).map(_.replace('.', 'D'))

    def isWolf(i: Int)(j: Int) = pasture.lift(i).flatMap(_.lift(j)).contains('W')

    val ans = (0 until r) X (0 until c) find { case (i, j) =>
      pasture(i)(j) == 'S' && (isWolf(i-1)(j) || isWolf(i+1)(j) || isWolf(i)(j-1) || isWolf(i)(j+1))
    }

    io.write(if(ans.isDefined) "No" else pasture.mkString("Yes\n", "\n", ""))
  }
}
/****************************[Ignore Template Below]****************************************/
trait CodeForcesApp {
  lazy val isOnlineJudge: Boolean = sys.props.get("ONLINE_JUDGE").exists(_.toBoolean)
  def debug(x: => Any): Unit = if (!isOnlineJudge) println(x)
  def apply(io: IO): io.type
  def main(args: Array[String]): Unit = this(new IO(System.in, System.out)).close()
}
object Utils {
  import scala.collection.mutable, mutable.{Map => Dict}
  type Point = java.awt.geom.Point2D.Double
  implicit class GenericExtensions[A](val a: A) extends AnyVal {
    def in(set: collection.Set[A]): Boolean = set(a)
    def some: Option[A] = Some(a)
    def partialMatch[B](f: PartialFunction[A, B]): Option[B] = f lift a
  }
  implicit class TraversableExtensions[A, C[X] <: Traversable[X]](val t: C[A]) extends AnyVal {
    def onlyElement: A = assuming(t.size == 1)(t.head)
    def groupByIdentifier[K](f: A => K): Map[K, A] = t.groupBy(f).mapValues(_.onlyElement)
    def duplicates(implicit b: C of A): C[A] = {
      val elems = mutable.Set.empty[A]
      (t collect {case i if !elems.add(i) => i}).to[C]
    }
    def sumWith[B](f: A => B)(implicit n: Numeric[B]): B = t.foldLeft(n.zero) {case (p, i) => n.plus(p, f(i))}
    def maxWith[B](f: A => B)(implicit ord: Ordering[B]): Option[B] = t.view.map(f).reduceOption(ord.max)
    def minWith[B](f: A => B)(implicit ord: Ordering[B]): Option[B] = t.view.map(f).reduceOption(ord.min)
    def zipWith[B](f: A => B)(implicit b: C of (A, B)): C[(A, B)] = (t map {i => i -> f(i)}).to[C]
    def mapTo[B](f: A => B)(implicit b: C of (A, B)): Map[A, B] = zipWith(f).toUniqueMap
    def whenNonEmpty[B](f: t.type => B): Option[B] = when(t.nonEmpty)(f(t))  // e.g. grades.whenNonEmpty(_.max)
    def collectFirst[B](f: PartialFunction[A, Option[B]]): Option[B] = t.view collect f collectFirst {case Some(x) => x}
    def toMutableMultiSet: Dict[A, Int] = {
      val c = map[A] to 0
      t.foreach(i => c(i) += 1)
      c
    }
    def toMultiSet: Map[A, Int] = toMutableMultiSet.toMap withDefaultValue 0
    def X[B](s: Traversable[B]): Iterator[(A, B)] = for {a <- t.toIterator; b <- s} yield (a, b)
  }
  implicit class ArrayExtensions[A <: AnyRef](val a: Array[A]) extends AnyVal {
    def sort(from: Int = 0, until: Int = a.length)(implicit cmp: Ordering[A]): Unit = java.util.Arrays.sort(a, from, until, cmp) //sort [from, until)
  }
  implicit class ArrayExtensions2[A](val a: Array[A]) extends AnyVal {
    def sort(from: Int = 0, until: Int = a.length)(implicit cmp: Ordering[A]): Unit = a.view(from, until).sorted.copyToArray(a, from)
  }
  implicit class SeqExtensions[A, C[X] <: Seq[X]](val s: C[A]) extends AnyVal {
    def isRotationOf(t: Seq[A]): Boolean = s ++ s containsSlice t
  }
  implicit class PairExtensions[A, B](val p: (A, B)) extends AnyVal {
    def key = p._1
    def value = p._2
    def toSeq(implicit ev: B =:= A): Seq[A] = Seq(p._1, ev(p._2))
  }
  implicit class PairsExtensions[A, B, C[X] <: Traversable[X]](t: C[(A, B)]) {
    def toUniqueMap: Map[A, B] = t.groupByIdentifier(_.key).mapValues(_.value)
    def toMultiMap(implicit b: C of B): Map[A, C[B]] = t.groupBy(_.key).mapValues(_.map(_.value).to[C]).withDefaultValue(b().result())
    def swap(implicit b: C of (B, A)): C[(B, A)] = t.map(_.swap).to[C]
  }
  implicit class MapExtensions[K, V, C[X, Y] <: Map[X, Y]](val m: C[K, V]) extends AnyVal {
    def lastKeyOption: Option[K] = m.lastOption.map(_.key)
    def firstKeyOption: Option[K] = m.headOption.map(_.key)
    def invert: Map[V, Set[K]] = m.toSet[(K, V)].swap.toMultiMap
    def mapKeys[K2](f: K => K2): Map[K2, V] = m map {case (k, v) => f(k) -> v}
    def toMutable: Dict[K, V] = Dict.empty[K, V] ++ m
    def mapValuesSafely[V2](f: V => V2): Map[K, V2] = m.mapValues(f).view.force //Hack to get around https://issues.scala-lang.org/browse/SI-4776
  }
  implicit class MutableMapExtensions[K, V](val m: mutable.Map[K, V]) extends AnyVal {
    def removeLast(): Option[(K, V)] = m.lastOption.map(removeNode)
    def removeFirst(): Option[(K, V)] = m.headOption.map(removeNode)
    private def removeNode(kv: (K, V)): (K, V) = {
      m -= kv.key
      kv
    }
  }
  implicit class SetExtensions[A, C[A] <: Set[A]](val s: C[A]) extends AnyVal {
    def notContains(x: A): Boolean = !(s contains x)
    def toMutable = mutable.Set.empty[A] ++ s
  }
  implicit class IterableExtensions[A, C[A] <: Seq[A]](val s: C[A]) extends AnyVal {
    def indicesWhere(f: A => Boolean): Seq[Int] = s.zipWithIndex.collect {case (a, i) if f(a) => i}
    def deleteAtIndex(i: Int, howMany: Int = 1) = s.patch(i, Nil, howMany)
    def insertAtIndex(i: Int, items: Seq[A]) = s.patch(i, items, 0)
    def findWithIndex(f: A => Boolean): Option[(A, Int)] = s.zipWithIndex find {case (a, _) => f(a)}
    //def foldUntil[B](z: B)(f: (B, A) => B)(c: B => Boolean): Option[(B, Int)] = s.view.scanLeft(z)(f).findWithIndex(c)
  }
  implicit class BooleanExtensions(val x: Boolean) extends AnyVal {
    def to[A](implicit n: Numeric[A]) = if(x) n.one else n.zero
    def toEnglish = if(x) "Yes" else "No"
  }
  implicit class IntExtensions(val x: Int) extends AnyVal {
    import java.lang.{Integer => JInt}
    def withHighestOneBit: Int = JInt.highestOneBit(x)
    def withLowestOneBit: Int = JInt.lowestOneBit(x)
    def numberOfLeadingZeroes: Int = JInt.numberOfLeadingZeros(x)
    def numberOfTrailingZeroes: Int = JInt.numberOfTrailingZeros(x)
    def bitCount: Int = JInt.bitCount(x)
    def setLowestBits(n: Int): Int = x | (left(n) - 1)
    def clearLowestBits(n: Int): Int = x & left(n, ~0)
    def setBit(i: Int, set: Boolean): Int = if (set) setBit(i) else clearBit(i)
    def setBit(i: Int): Int = x | left(i)
    def clearBit(i: Int): Int = x & ~left(i)
    def toggleBit(i: Int): Int = x ^ left(i)
    def getBit(i: Int): Int = (x >> i) & 1
    private def left(bits: Int, num: Int = 1) = assuming(bits >= 0 && bits < JInt.SIZE)(num << bits)
    def X(y: Int): Iterator[(Int, Int)] = until(x) X until(y)
  }
  implicit class LongExtensions(val x: Long) extends AnyVal {
    import java.lang.{Long => JLong}
    def withHighestOneBitSet: Long = JLong.highestOneBit(x)
    def withLowestOneBitSet: Long = JLong.lowestOneBit(x)
    def numberOfLeadingZeroes: Int = JLong.numberOfLeadingZeros(x)
    def numberOfTrailingZeroes: Int = JLong.numberOfTrailingZeros(x)
    def bitCount: Int = JLong.bitCount(x)
    def setLowestBits(n: Int): Long = x | (left(n) - 1)
    def clearLowestBits(n: Int): Long = x & left(n, ~0L)
    def setBit(i: Int, set: Boolean): Long = if (set) setBit(i) else clearBit(i)
    def setBit(i: Int): Long = x | left(i)
    def clearBit(i: Int): Long = x & ~left(i)
    def toggleBit(i: Int): Long = x ^ left(i)
    def getBit(i: Int): Long = (x >> i) & 1
    private def left(bits: Int, num: Long = 1L) = assuming(bits >= 0 && bits < JLong.SIZE)(num << bits)
  }
  implicit class NumericExtensions[A](val x: A)(implicit n: Numeric[A]) {
    import Numeric.Implicits._
    def **(i: Int): A = if (i == 0) n.one else {
      val h = x ** (i/2)
      if (i%2 == 0) h * h else h * h * x
    }
    def distance(y: A) = (x - y).abs()
    def nonNegative: Option[A] = when(n.gteq(x, n.zero))(x)
    def whenNegative(f: => A): A = nonNegative.getOrElse(f)   //e.g. students.indexOf("Greg").whenNegative(Int.MaxValue)
  }
  implicit class IntegralExtensions[A](val x: A)(implicit n: Integral[A]) {
    import scala.collection.immutable.NumericRange, Integral.Implicits._
    def mod(y: A): A = x - ((x / y) * y) //(x/y)*y + (x mod y) == x
    def -->(y: A): NumericRange.Inclusive[A] = NumericRange.inclusive(x, y, if (n.lteq(x, y)) n.one else -n.one)
    def ceilDiv(y: A): A = if (mod(y) == n.zero) x/y else n.one + (x/y)
  }
  implicit def toPoint[A](p: (A, A))(implicit n: Numeric[A]) = new Point(n.toDouble(p._1), n.toDouble(p._2))
  def desc[A: Ordering]: Ordering[A] = asc[A].reverse  //e.g. students.sortBy(_.height)(desc)
  def asc[A](implicit order: Ordering[A]): Ordering[A] = order
  def map[K] = new {
    def to[V](default: => V): Dict[K, V] = using(_ => default)
    def using[V](f: K => V): Dict[K, V] = new mutable.HashMap[K, V]() {
      override def apply(key: K) = getOrElseUpdate(key, f(key))
    }
  }
  def newMultiMap[K, V] = map[K] to mutable.Set.empty[V]
  def memoize[A, B](f: A => B): A ==> B = map[A] using f
  implicit class FuzzyDouble(val x: Double) extends AnyVal {
    def  >~(y: Double) = x > y + eps
    def >=~(y: Double) = x >= y + eps
    def  ~<(y: Double) = y >=~ x
    def ~=<(y: Double) = y >~ x
    def  ~=(y: Double) = (x distance y) <= eps
  }
  def toPolygon(points: Traversable[(Int, Int)]): java.awt.Polygon = {
    val (xs, ys) = points.unzip
    new java.awt.Polygon(xs.toArray, ys.toArray, points.size)
  }
  def toShape(points: Traversable[(Double, Double)]): java.awt.geom.GeneralPath = {
    val shape = new java.awt.geom.GeneralPath()
    points.headOption foreach {case (x, y) => shape.moveTo(x, y)}
    points.tail foreach {case (x, y) => shape.lineTo(x, y)}
    shape.closePath()
    shape
  }
  def isPrime(n: Int): Boolean = BigInt(n).isProbablePrime(31)
  def until(until: Int): Range = 0 until until
  def not[A](f: A => Boolean): A => Boolean = {x: A => !f(x)}
  def assuming[A](cond: => Boolean)(f: => A): A = {require(cond); f}
  @inline def when[A](check: Boolean)(f: => A): Option[A] = if (check) f.some else None
  def repeat[U](n: Int)(f: => U): Unit = (1 to n).foreach(_ => f)
  type of[C[_], A] = scala.collection.generic.CanBuild[A, C[A]]
  type ==>[A, B] = collection.Map[A, B]
  val mod: Int = (1e9 + 7).toInt
  val eps: Double = 1e-9
}
/***********************************[Scala I/O]*********************************************/
import Utils._, java.io._, java.util.StringTokenizer

class IO(in: BufferedReader, out: BufferedWriter) extends Iterator[String] with AutoCloseable with Flushable {
  def this(reader: Reader, writer: Writer) = this(new BufferedReader(reader), new BufferedWriter(writer))
  def this(in: InputStream, out: OutputStream) = this(new InputStreamReader(in), new OutputStreamWriter(out))

  private[this] val tokenizers = Iterator.continually(in.readLine()).takeWhile(_ != null).map(new StringTokenizer(_)).buffered
  val printer = new PrintWriter(out, true)

  @inline private[this] def tokenizer() = {
    while(tokenizers.nonEmpty && !tokenizers.head.hasMoreTokens) tokenizers.next()
    when(tokenizers.nonEmpty)(tokenizers.head)
  }

  def read[A](implicit read: IO.Read[A]): A = read.apply(this)
  def read[C[_], A: IO.Read](n: Int)(implicit b: C of A): C[A] = Iterator.fill(n)(read).to[C]

  def readTill(delim: String): String = tokenizer().get.nextToken(delim)
  def readTillEndOfLine(): String = readTill("\n\r")

  def readAll[A: IO.Read]: (Int, Vector[A]) = {
    val data = read[Vector[A]]
    (data.length, data)
  }

  override def next() = tokenizer().get.nextToken()
  override def hasNext = tokenizer().nonEmpty

  def write(obj: Any): this.type = {
    printer.print(obj)
    this
  }
  def writeLine(): this.type = {
    printer.println()
    this
  }
  def writeLine(obj: Any): this.type = {
    printer.println(obj)
    this
  }
  def writeAll(obj: Traversable[Any], separator: String = " "): this.type = write(obj mkString separator)
  def query[A: IO.Read](question: Any): A = writeLine(question).read

  override def flush() = printer.flush()
  def close() = {
    flush()
    in.close()
    printer.close()
  }
}
object IO {
  class Read[A](val apply: IO => A) {
    def map[B](f: A => B): Read[B] = new Read(apply andThen f)
  }
  object Read {
    implicit def collection[C[_], A: Read](implicit b: C of A): Read[C[A]]         = new Read(r => r.read[C, A](r.read[Int]))
    implicit val string                                       : Read[String]       = new Read(_.next())
    implicit val char                                         : Read[Char]         = string.map(_.toTraversable.onlyElement)
    implicit val int                                          : Read[Int]          = string.map(_.toInt)
    implicit val long                                         : Read[Long]         = string.map(_.toLong)
    implicit val bigInt                                       : Read[BigInt]       = string.map(BigInt(_))
    implicit val double                                       : Read[Double]       = string.map(_.toDouble)
    implicit val bigDecimal                                   : Read[BigDecimal]   = string.map(BigDecimal(_))
    implicit def tuple2[A: Read, B: Read]                     : Read[(A, B)]       = new Read(r => (r.read[A], r.read[B]))
    implicit def tuple3[A: Read, B: Read, C: Read]            : Read[(A, B, C)]    = new Read(r => (r.read[A], r.read[B], r.read[C]))
    implicit def tuple4[A: Read, B: Read, C: Read, D: Read]   : Read[(A, B, C, D)] = new Read(r => (r.read[A], r.read[B], r.read[C], r.read[D]))
    implicit val boolean                                      : Read[Boolean]      = string map {s =>
      s.toLowerCase match {
        case "yes" | "true" | "1" => true
        case "no" | "false" | "0" => false
        case _ => s.toBoolean
      }
    }
  }
}

object A extends App {
  def protect(p: Array[Array[Char]], r: Int, c: Int): Boolean = {
    for (i <- 0 until r)
      for (j <- 0 until c)
        if (p(i)(j) == 'S') {
          if (
            p(i)(0 max (j - 1)) == 'W' ||
            p(i)((c - 1) min (j + 1)) == 'W' ||
            p(0 max (i - 1))(j) == 'W' ||
            p((r - 1) min (i + 1))(j) == 'W'
          ) {
            return false
          }
          else {
            if (p(i)(0 max (j - 1)) == '.') p(i)(0 max (j - 1)) = 'D'
            if (p(i)((c - 1) min (j + 1)) == '.') p(i)((c - 1) min (j + 1)) = 'D'
            if (p(0 max (i - 1))(j) == '.') p(0 max (i - 1))(j) = 'D'
            if (p((r - 1) min (i + 1))(j) == '.') p((r - 1) min (i + 1))(j) = 'D'
          }
        }
    true
  }

  val Array(r, c) = scala.io.StdIn.readLine().split(" ").map(_.toInt)

  val p = Array.fill(r)(new Array[Char](c))
  for (i <- 0 until r) {
    val line = scala.io.StdIn.readLine()
    for (j <- 0 until c)
      p(i)(j) = line(j)
  }

  val s = protect(p, r, c)
  if (s) {
    println("Yes")
    println(p.map(_.mkString("")).mkString("\n"))
  } else println("No")

}


import scala.io.Source

object Solution extends App {
  val in = Source.stdin.getLines()
  val Array(n, m) = in.next().split(' ').map(_.toInt)
  val data = Array.fill[List[Int]](n){Nil}
  (1 to m).foreach { _ =>
    val Array(a, b) = in.next().split(' ').map(_.toInt - 1)
    data(a) ::= b
    data(b) ::= a
  }
  var zero = data.indices.filter(i => data(i).length == 1)
  var i = 0
  while (zero.nonEmpty) {
    i += 1
    zero.foreach {j =>
      data(j).foreach { k => data(k) = data(k).filter(_ != j)}
      data(j) = Nil
    }
    zero = data.indices.filter(i => data(i).length == 1)
  }
  println(i)

}

object StudentsAndShoelaces129B extends App {
  import java.io.{InputStream, PrintStream}
  import java.util.Scanner
  solve(System.in,System.out)
  def solve(in: InputStream, out: PrintStream): Unit = {
    val scanner = new Scanner(in)
    val n = scanner.nextInt()
    val m = scanner.nextInt()
    val nodes = (1 to n).map(Node)
    val edges = (1 to m).map{ _ =>
      val a = nodes(scanner.nextInt()-1)
      val b = nodes(scanner.nextInt()-1)
      UndirectedEdge(a,b)
    }
    val g = new Graph(nodes,edges)

    def findNumGroups(graph: Graph): Int = {
      val badGuys = graph.findBadGuys()
      if(badGuys.isEmpty)
        0
      else 1 + findNumGroups(graph.kickBadGuys(badGuys))
    }

    out.println(findNumGroups(g))

  }
  class Graph(val nodes: Seq[Node],val edges: Seq[UndirectedEdge]){
    val mapNeighbours = nodes.map(n => n -> edges.filter(e => e.a == n || e.b==n).map{case UndirectedEdge(a,b) => if(a==n) b else a}).toMap
    def findBadGuys(): Seq[Node] =  mapNeighbours.filter{case (_,neighbours) => neighbours.length == 1}.keys.toSeq
    def kickBadGuys(badGuys: Seq[Node]): Graph = {
      val goodGuys = nodes.filterNot(n => badGuys.contains(n))
      val newEdges = edges.filterNot{case UndirectedEdge(a,b) => badGuys.contains(a) || badGuys.contains(b)}
      new Graph(goodGuys,newEdges)
    }

  }
  case class Node(node: Int)
  case class UndirectedEdge(a: Node, b: Node)
}


import scala.io.StdIn

object ZahirGame {

  def main(args: Array[String]): Unit = {
    val k = StdIn.readLine().split(" ").tail.head.toInt
    val in = StdIn.readLine()

    def step(acc : (List[Char], Int), c: Char): (List[Char], Int) = acc match {
      case (Nil, _) => (List(c), 1)
      case (l, n) if l.head == c || n == 0 => (c :: l, n + 1)
      case (l, n) => (c :: l.drop(n % k), 1)
    }

    val (resWithLast, residue) = in.foldLeft[(List[Char], Int)]((List(), 0))(step)
    val res = resWithLast.drop(residue % k).sorted.foldLeft[Map[Char, Int]](Map()) { (acc, c) =>
      acc + (c -> (acc.getOrElse(c, 0) + 1))
    }
    println(if (res.isEmpty) 0 else res.values.max / k)
  }
}

import scala.collection.mutable

object Problem_1105B {
  // Salem and Sticks

  def main(args: Array[String]) {
    val scan = scala.io.StdIn

    //Get inputs
    val inputs: Array[Int] = scan.readLine().split(" ").map(_.toInt)
    val targetArr: String = scan.readLine()
    val n = inputs(0)
    val k = inputs(1)
    val occMap = Array.fill(256)(0)

    var x = 0
    while (x < n - k + 1) {
      val tempChar = targetArr(x)
      if (targetArr.substring(x, x + k).count(_ == tempChar) == k) {
        occMap(tempChar) += 1
        x += k
      }
      else{
        while(targetArr(x) == targetArr(x+1)){
          x+=1
        }
        x+=1
      }

    }
    val ans = occMap.max
    println(ans)
  }

}

object Main {
  def main(args: Array[String]): Unit = {
    val s = new Main()
    s.solve()
    s.out.flush()
  }
}

class Main {
  import java.io._
  import java.util.StringTokenizer

  import scala.collection.mutable
  import scala.util.Sorting
  import math.{abs, max, min}
  import mutable.{ArrayBuffer, ListBuffer}
  import scala.reflect.ClassTag

  val MOD = 1000000007
  val out = new PrintWriter(System.out)

  def solve(): Unit = {
    val N, K = ni()
    val S = ns(N)

    var ans = 0
    REP(26) { i =>
      val c = 'a' + i
      var cnt = 0
      var lvl = 0
      REP(N) { j =>
        if (S(j) == c) {
          cnt += 1
          if (cnt == K) {
            cnt = 0
            lvl += 1
          }
        } else {
          cnt = 0
        }
      }
      ans = max(ans, lvl)
    }

    out.println(ans)
  }

  def debug(as: Array[Boolean]): Unit = {
    System.err.println(as.map(x => if(x) "1" else "0").mkString)
  }

  def debug(as: Array[Int]): Unit = {
    System.err.println(as.mkString(" "))
  }

  def debug(as: Array[Long]): Unit = {
    System.err.println(as.mkString(" "))
  }

  def debug(s: String): Unit = {
    System.err.println(s)
  }

  class InputReader(val stream: InputStream) {
    private val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    private var tokenizer: StringTokenizer = _

    def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = next().toInt
    def nextLong(): Long = next().toLong
    def nextChar(): Char = next().charAt(0)
  }
  val sc = new InputReader(System.in)
  def ni(): Int = sc.nextInt()
  def nl(): Long = sc.nextLong()
  def nc(): Char = sc.nextChar()
  def ns(): String = sc.next()
  def ns(n: Int): Array[Char] = ns().toCharArray
  def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)
  def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
    val A1, A2 = Array.ofDim[Int](n)
    REP(n) { i =>
      A1(i) = ni() + offset
      A2(i) = ni() + offset
    }
    (A1, A2)
  }
  def nm(n: Int, m: Int): Array[Array[Int]] = {
    val A = Array.ofDim[Int](n, m)
    REP(n) { i =>
      REP(m) { j =>
        A(i)(j) = ni()
      }
    }
    A
  }
  def nal(n: Int): Array[Long] = map(n)(_ => nl())
  def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n) (_ => ns(m))
  def REP(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while(i < N) { f(i); i += 1 }
  }
  def REP_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while(i >= offset) { f(i); i -= 1 }
  }

  def map[@specialized A: ClassTag](n: Int, offset: Int = 0)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    REP(n)(i => res(i) = f(i + offset))
    res
  }

  def sumL(as: Array[Int]): Long = {
    var s = 0L
    REP(as.length)(i => s += as(i))
    s
  }

  def cumSum(as: Array[Int]) = {
    val cum = Array.ofDim[Long](as.length + 1)
    REP(as.length) { i =>
      cum(i + 1) = cum(i) + as(i)
    }
    cum
  }
}


import scala.io.StdIn

object Team extends App {

  val ques = StdIn.readInt()
  var solve = 0
  for (i <- Range(0, ques)) {
    if (impl(StdIn.readLine().split(" ").map(_.toInt)))
      solve += 1
  }
  print(solve)

  def impl(soln: Array[Int]): Boolean = {
    soln.count(_ == 1) >= 2
  }
}

object pratise {
    def main(args : Array[String]) {
        val n = readInt
        val res = (1 to n).count(_ => readLine().split(" ").map(_.toInt).sum>1)
        println(res)
    }
}

object cf extends App{
  val t = readInt()
  var res = 0
  for (i <- 0 to t-1) {
    val Array(a, b, c) = readLine().split(" ").map(_.toInt)
    val r = a+b+c
    if (r > 1) res += 1
  }
  println(res)
}

import scala.io.StdIn.readLine
object Solution {
    def reduceMembers(s: Iterable[Array[Int]]) = s map (_ reduceLeft (_ + _))
    def reduceProblems(s: Iterable[Int]) = s count (_ >= 2)
    def readLines(n: Int) = Iterator.continually(readLine).take(n).toIterable
    def main(args: Array[String]) = {
        val n = readLine.toInt
        val problemMembers = readLines(n) map (_ split " " map (_.toInt))
        println(reduceProblems(reduceMembers(problemMembers)))
    }
}

object Solution231A extends App {

  def solution() {
    val n = _int
    val res = 1.to(n).map(_ => _int + _int + _int).filter(i => i > 1).size
    println(res)
  }

  //////////////////////////////////////////////////
  import java.util.Scanner
  val SC: Scanner = new Scanner(System.in)
  def _line: String = SC.nextLine
  def _int: Int = SC.nextInt
  def _long: Long = SC.nextLong
  def _string: String = SC.next
  solution()
}

object Competition extends App {

  import scala.io.StdIn._

  val k = readInt()

  def input(n: Int)= (1 to n).map(_ => readLine().split(" ").toSeq).toSeq

  def sumUp(seq: Seq[String]) = seq.map(Integer.parseInt).sum

  println(input(k).map(sumUp).count(_ > 1))
}

object Cf231A extends App {
  val n = readInt()
  var res = 0
  for (i <- 1 to n) {
    res = res + (if (readLine.split(" ").map(_.toInt).sum >= 2) 1 else 0)
  }
  println(res)
}

object A extends App {
    import scala.io.StdIn._
    val n = readInt
    var i = 0
    var t = 0
    for (i <- 0 until n) {
        val x = (readLine().split(' ')).map(_.toInt)
        if (x.sum >= 2)
            t += 1
    }
    println(t)
}

import scala.collection.mutable._
import scala.math._
import scala.util.control.Breaks.break
import scala.util.control.Breaks.breakable
import java.util._
object Main extends App {
  val sc = new Scanner(System.in)
  val n = sc.nextInt
  println(
    (for(i <- 1 to n) yield {
      (for(j <- 1 to 3) yield sc.nextInt).foldLeft(0)(_ + _) >= 2
    }).count(_ == true)
  )
}

object Problems_231A {
    def main(args: Array[String]) = {
        println((1 to readInt).count(_ => readLine.split(" ").map(_.toInt).sum >= 2))
    }
}

import java.util.Scanner

object A extends App {
  val sc = new Scanner(System.in)

  val n = sc.nextInt
  val ones = for {
    i <- 1 to n
    p = sc.nextInt
    q = sc.nextInt
    r = sc.nextInt
    if p + q + r >= 2
  } yield 1
  println(ones.length)
}

// http://codeforces.com/problemset/problem/231/A

import scala.io.StdIn.readInt
import scala.io.StdIn.readLine

object Team {
    def main(args: Array[String]): Unit = {
        val n: Int = readInt()
        var count: Int = 0
        for(i <- 1 to n) {
            if (readLine().split(" ").map(_.toInt).sum >= 2) {
                count += 1
            }
        }
        println(count)
    }
}

import scala.io.StdIn

/**
  * @author traff
  */

object Main extends App {
  val n = StdIn.readLine().toInt

  var cnt = 0

  1 to n foreach { _ =>
    val Array (x, y, z) = StdIn.readLine().split(" ").map(_.toInt)

    def f(x: Int) = if (x>0) 1 else 0

    cnt += (if (f(x) +  f(y) + f(z) > 1) 1 else 0)
  }

  println(cnt)

}

import scala.io.StdIn._

object Main {
  def main(args: Array[String]): Unit = {
    val n = readInt
    val ans = (0 until n).map { k =>
       val s = readLine.split(" ").map(_.toInt).sum
       if (s >= 2) 1 else 0
    }.sum
    println(ans)
  }
}

object Solution extends App {
  val in = scala.io.Source.stdin.getLines()
  val n = in.next().toInt
  println(Range(0, n).map(_ => in.next().split(" ").map(_.toInt).sum).count(_ > 1))
}

object P231a extends App {
  val n = readLine.toInt
  val problems = (1 to n).map(_ => readLine.split(" ").map(_.toInt))
  val implementings = problems.filter(_.sum >= 2)
  println(implementings.size)
}

object Main {
  def main(args: Array[String]) {
    val std = scala.io.StdIn
    val n = std.readInt()
    var result = 0
    for (i <- 0 until n) {
      val r = std.readLine().split(" ").map(_.toInt).sum
      if (r>=2) result += 1
    }
    println(result)
  }
}

import scala.io.StdIn
object solution extends App{

   val rounds = StdIn.readInt
   var count = 0
  for(round <- 1 to rounds){
       val views = StdIn.readLine.split(" ").map(_.toInt).sum
       if( views >= 2) count += 1
  }
   println(count)

}

import scala.io.StdIn
object Team extends App
{
    println((1 to StdIn.readInt()).count(_ => StdIn.readLine().split(' ').map(_.toInt).sum >=2))
}

object HelloWorld {
   def main(args: Array[String]) {
      val n = readInt
      var c = 0
      for (i <- 0 until n) {
          c += (if(readLine.split(" ").map(_.toInt).count(_ == 1) > 1) 1 else 0)
      }
      println(c)
   }
}

import scala.io.StdIn

object p231a extends  App{

  def sumline(): Int ={
    val s = StdIn.readLine()
    var count = 0

    for (c <- s.toCharArray){
      if (c=='1') {
        count+=1
      }
    }
    if (count>=2) {
      return 1;
    }
    return 0
  }

  private val num = StdIn.readLine().toInt

  var sum = 0

  for (i <- 0 until num){
    sum += sumline()
  }

  print(sum)

}

object A231 {
  @inline def tokenizeLine = new java.util.StringTokenizer(scala.io.StdIn.readLine)

  def readInts(n: Int) = {
    val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt)
  }

  def readLongs(n: Int) = {
    val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toLong)
  }

  def readBigs(n: Int) = {
    val tl = tokenizeLine; Array.fill(n)(BigInt(tl.nextToken))
  }

  def main(args: Array[String]): Unit = {
    val Array(n) = readInts(1)
    val input = new Array[Int](n)
    for(i <- 0 until n) {
      val Array(a, b, c) = readInts(3)
      input(i) = a + b + c
    }
    println(input.count(_ > 1))
  }
}

import java.util.Scanner

/**
 * User: Takayuki
 * Date: 12/10/08
 */
object A extends App {
	val in = new Scanner(System.in)
	val n = in.nextInt()
	var res = 0
	for (i <- 0 until n) {
		if (in.nextInt() + in.nextInt() + in.nextInt() >= 2) {
			res += 1
		}
	}
	println(res)
}

object Hello {
    def main(args: Array[String]): Unit = {
      val n = readLine toInt
      var ans = 0
      for (i <- 1 to n) {
        val Array(a, b, c): Array[Int] = readLine split (' ') map (_ toInt)
        if (a + b + c > 1)
          ans = ans + 1
      }
      println(ans)
    }
}

import java.util.Scanner
import java.io.PrintWriter
import scala.annotation.tailrec
import scala.collection.mutable
import scala.util.Random
import scala.util.matching.Regex

object P231A extends App {
  val sc = new Scanner(System.in)
  val out = new PrintWriter(System.out)

  val N = sc.nextInt

  def solve: Int = {
    @tailrec
    def loop(acc: Int, i: Int): Int =
      if (i == N) acc
      else {
        val P, V, T = sc.nextInt
        if (P + V + T >= 2) loop(acc + 1, i + 1)
        else loop(acc, i + 1)
      }
    loop(0, 0)
  }

  out.println(solve)
  out.close
}

object Team {
	def main (args: Array[String]) {
		val scanner = new java.util.Scanner(System.in)
		val n = scanner.nextInt()
		var count = 0
		for (i <- 1 to n) {
		  val first = scanner.nextInt()
		  val second = scanner.nextInt()
		  val third = scanner.nextInt()
		  if (first + second + third >= 2)
		    count = count + 1
		}
		println(count)
	}
}

import scala.io.StdIn

object A231 {
    def main(args: Array[String]) =
        println((for (i <- 1 to StdIn.readLine().toInt if StdIn.readLine().split(" ").map(_.toInt).sum > 1) yield i).length)
}

object Codeforces231A extends App{

  def Checkline(ques:String):Boolean={
    var conv=ques.split(" ")
    var conve=conv.map(_.toInt)
    var res=conve(0)+conve(1)+conve(2)
    if (res/2==1){
      return true
    }
    else{
      return false
    }
  }

  var ans:Int=0
  val s:Int=scala.io.StdIn.readInt
  for (i <- 1 to s){
    val k:String=scala.io.StdIn.readLine
    if (Checkline(k)==true){
      ans+=1
    }
  }
  println(ans)

}

import java.io._

object Easy {
  val reader = new BufferedReader(new InputStreamReader(System.in))
  def readInt = reader.readLine().toInt
  def readInts = reader.readLine().split(" ").map(_.toInt)

  def ans = (for(_ <- 1 to readInt) yield readInts.reduce(_ + _)).count(_ > 1)

  def main(a: Array[String]) {
    println(ans)
  }
}

/**
 * Created by vol on 9/18/14.
 */
object A231 extends App {
  val n = readInt()

  def loop(n:Int, i:Int, acc:Int):Int = {
    if (n == i) acc
    else  {
      val count = readLine().split(" ").map(_.toInt).count((e:Int) => e == 1)
      if (count >= 2) loop(n, i + 1, acc + 1) else loop(n, i + 1, acc)
    }
  }

  println(loop(n, 0, 0))

}

object test5 extends App {
    val n=readLine.toInt

    val list=for(i<-1 to n) yield{
    	if(readLine.split(" ").map(_.toInt).sum>=2) 1 else 0
    }
    println(list.sum)
}

object Main {
  def main(args: Array[String]) {
    var n = readInt()
    val tasks = (1 to n).map { unusded =>
      val arr = readLine().split(" ").map(_.toInt)
      arr.sum
    }
    println(tasks.filter(_ >= 2).size)
  }
}

import scala.io.StdIn._

object Solution extends App {

  def solve() = {
    val n = readInt()
    val results = (1 to n).map { _ =>
      val answers = readLine().split(" ").map(_.toInt)
      answers.count(_ == 1) > 1
    }

    println(results.count(_ == true))
  }

  solve()
}

object Main {
  def main(args: Array[String]): Unit = {
    val n = readLine.toInt
    var sum = 0
    var i = 0
    while(i < n) {
      val Array(m, n, d) = readLine.split(" ").map(_.toInt)
//      println(m +","+ n +","+ d)
      if(m + n + d >= 2) sum = sum + 1
      i = i + 1
    }
    println(sum)
  }
}

import scala.io.StdIn._

object CF_231A_Team {
  def main(args: Array[String]): Unit = {
    println((for (i <- 1 to readInt) yield readLine().split(" ").map(_.toInt).sum >= 2).count(_ == true))
  }
}

import scala.io.StdIn

object  Test {
  def main(args1: Array[String]) {
//    val args = StdIn.readLine().split(" ")
//    var (x: Long, y: Long) = (args(0).toLong, args(1).toLong)

    val count = StdIn.readInt

    var ret = 0L
    for (i <- 0 until count) {
      val a = StdIn.readLine().split(" ")
      val sum = a.filter(s => s.equals("1")).length
      if (sum > 1) {
        ret += 1
      }
    }
    println(ret)
  }

}

object TwoThreeOneA {
	def main(args : Array[String]) : Unit = {
		var sol=0
		var n=readInt
		for(i<-0 until n){
			var ones=readLine.split(' ').count(_=="1")
			if(ones>1){
				sol+=1
			}
		}
		println(sol)
	}
}

import scala.io.StdIn._

object Team {
  def main(args: Array[String]): Unit = {
    val n = readInt()
    val problems =
      for {
        _ <- 0 until n
        x = readLine().split(" ").map(_.toInt).reduce(_ + _)
      } yield x
    val ans = problems.filter(_ > 1).length
    println(ans)
  }
}

object CF231A extends App {
    import scala.io.StdIn.readLine
    val n = readLine.toInt
    var count=0
    for (_ <- 1 to n) {
      if (readLine.split(" ").map(_.toInt).count(x => x > 0)>1)
        count+=1
    }
    print(count)
}

object Team extends App{

    val probNum = scala.io.StdIn.readInt()

    var solutions = 0

    for (x <- 1 to probNum) {
        val confLvl = scala.io.StdIn.readLine().split(" ").map(k => k.toInt)
        if (confLvl.sum >= 2) solutions += 1
        else solutions

        //confLvl.sum

    }

    println(solutions)
}

object Team extends App{

    val probNum = scala.io.StdIn.readInt()

    val confLvl = for (i <- 1 to probNum) yield {
        if (scala.io.StdIn.readLine().split(" ").map(k => k.toInt).sum >= 2) 1
        else 0
    }

    println(confLvl.sum)
}

object TaskA231 {
  def main(args: Array[String]): Unit = {
    val n = scala.io.StdIn.readInt
    var l = List[String]()
    for (i <- 0 until n) l = scala.io.StdIn.readLine :: l
    println(l.count(_.count(_ == '1') >= 2))
  }
}

object CF0231A extends App {

  val n = readInt()

  var count = 0

  (1 to n).foreach(v => {

    val str = readLine().split(" ").map(_.toInt).filter(_>0).size
    if (str >= 2) count += 1
  })

  println(count)

}

object task231A {
  def main(args:Array[String]) {
    var rv:Int=0;
    for (l <- io.Source.stdin.getLines.toList.drop(1)) {
      val Array(a,b,c)=l.split(" ").map(_.toInt)
      if (a+b+c>1) { rv+=1; }
    }
    println(rv)
  }
}

object Team {
  def main(args: Array[String]) = {
        val input = Iterator.continually(scala.io.StdIn.readLine()).takeWhile(_ != null).drop(1)

        val solution = input.map(_.split(" ").map(_.toInt).sum).count(_ > 1)

        println(solution)
    }
}

import scala.io.StdIn

object Problem231A extends App {

  val n = StdIn.readInt()

  val total = (1 to n).map(_ => StdIn.readLine()).map(_.split(" ").map(_.toInt).sum).filter(_ > 1).size
  println(total)

}

import scala.io.StdIn
object A {
  def main(args: Array[String]): Unit = {
    var ans:StringBuilder=new StringBuilder()
//    var t=StdIn.readInt();
    var t=1
    while(t>0)
      {

        var n:Int=StdIn.readInt()
//        var a:Array[String]=StdIn.readLine().split(" ")
//        var b:Array[String]=StdIn.readLine().split(" ")
//        var c:Array[String]=StdIn.readLine().split(" ")

        var res=0;
        for(i <- 0 until n)
          {
            var a:Array[String]=StdIn.readLine().split(" ")
            var count=0
            if(a(0).equals("1"))
              {
                count+=1;
              }
            if(a(1).equals("1"))
            {
              count+=1
            }
            if(a(2).equals("1"))
              {
                count+=1
              }

            if(count>=2)
              {
                res+=1
              }
          }
        ans.append(res).append("\n")

        t-=1;
      }
    println(ans.toString());

  }
}

object Main{
    def main(args:Array[String]){
        var p =0
        for(_<-1 to readLine.toInt){
            var n = 0
            for(v<-readLine.split(" ").map(x=>x.toInt)){
                if(v==1)
                    n+=1
            }
            if(n>=2)
                p+=1
        }
        println(p)
    }
}

object Problems_231A {
    def main(args: Array[String]) = {
        println((1 to readInt).count(_ => readLine.split(" ").map(_.toInt).sum >= 2))
    }
}

object test
{
    def main(args : Array[String])
    {
        val n = readInt
        var ans = 0
        for(i <- 0 to n-1)
            if(readLine().split(" ").map(_.toInt).count(x => x==1) >=2)
                ans +=1;
        print (ans)
    }
}

object test extends App
{
    print ((1 to readInt).count(x => readLine().split(' ').map(_.toInt).sum >=2))
}

object Main {

  def main(args: Array[String]): Unit = {
    var res = 0;
    for(i <- 1 to readInt()){
      if(readLine().split(' ').map(_.toInt).sum>=2)res += 1
    }
    println(res)
  }

}

object Main {

  def main(args: Array[String]): Unit = {
    println((for(i <- 1 to readInt())yield if(readLine().split(' ').map(_.toInt).sum>=2) 1 else 0).sum)
  }

}

import scala.io.StdIn._

object Team_231A extends App {
  val n = readInt
  val p = (for (x <- 1 to n) yield (readLine.split(" ").map(_.toInt)))

  val ans = p.foldLeft(0)((acc, cp) => {
    acc +
    (if (cp.count(_ == 1) > 1) 1 else 0)
  })

  println(ans)
}

object codeforces {
  def main(args: Array[String]): Unit = {
    val n: Int = scala.io.StdIn.readLine().toInt
    var count: Int = 0
    for (_ <- 0 until n){
      val word: String = scala.io.StdIn.readLine()
//      println(word)
      val line = word.split(" ")
      var num: Int = 0
      for (i <- word){
        if (i == '1'){
          num+=1
        }
      }
      if (num >= 2){
        count+=1
      }
    }
    println(count)
  }
}

import scala.io.Source

object Cf231a extends App {
  println(Source.stdin.getLines().drop(1).map(_.split(" ").filter(!_.isEmpty()).map(_.toInt)).map(x => if (x.sum >= 2) 1 else 0).sum)
}

object Team {
  import io.StdIn.{readLine => rl}
  def main(args : Array[String]) {
    val gls = io.Source.stdin.getLines
    println(gls drop(1) count(((x) => 1 < x.split(" ").map(_.toInt).reduce(_ + _))))
  }
}

object main extends App{
    println((1 to readInt).count(_ => readLine.split(' ').map(_.toInt).sum >=2) )
}

object main extends App{
    val n = readLine().toInt
    var cnt = 0
    for(i <- 1 to n){
        val Array(a,b,c) = readLine.split(' ').map(_.toInt)
        if(a + b + c >=2) cnt += 1
    }
    print(cnt)
}

object A00231 extends App {
  def parseInt(str: String, count: Int): Seq[Int] = {
    val scanner = new java.util.Scanner(str)
    val res = (1 to count) map (_ => scanner.nextInt())
    scanner.close()
    res
  }
  def scanInts(count: Int): Seq[Int] = parseInt(Console.readLine, count)

  def oneIntLine = {
    val Seq(count) = scanInts(1);
    count
  }

  val cnt = oneIntLine
  var res = 0
  for (i <- 1 to cnt) {
    if (scanInts(3).sum > 1) res += 1
  }
  println(res)
}

import scala.collection.immutable.Range
import java.lang.String

object Olymp{
  def main(args: Array[String]): Unit = {
    val n = readLine toInt
    var ans = 0
    for (i <- 1 to n) {
      val Array(a, b, c): Array[Int] = readLine split (' ') map (_ toInt)
      if (a+b+c > 1)
        ans = ans + 1;
    }
    println(ans)
  }
}

object main extends App{
    println( (1 to readInt).count(_ => readLine.split(' ').map(_.toInt).sum >=2 ) )

}

import scala.io.StdIn.readLine
import scala.io.StdIn.readInt

object Team {
  def main(args: Array[String]) {
    val n = readInt
    val problems = for {
      i <- 1 to n
      line = readLine
    } yield line
    println(
      problems.map(l => l.split(' ').map(_.toInt).toList.sum).count(_ >= 2)
    )
  }
}

object CF0231A extends App {

  val n = readInt()

  var count = 0

  (1 to n).foreach(v => {

    val str = readLine().split(" ").map(_.toInt).filter(_>0).size
    if (str > 2) count += 1
  })

  println(count)

}

object Main {
  def main(args: Array[String]) {
    var n = readInt()
    val tasks = (1 to n).map { unusded =>
      val arr = readLine().split(" ").map(_.toInt)
      println(arr)
      arr.sum
    }
    println(tasks)
    println(tasks.filter(_ >= 2).size)
  }
}

object Main {
  def main(args: Array[String]) {
    var n = readInt()
    val tasks = (1 to n).map { unusded =>
      val arr = readLine().split(" ").map(_.toInt)
      arr.sum
    }
    println(tasks.filter(2 >=).size)
  }
}

import scala.io.StdIn

object Problem231A extends App {

  val n = StdIn.readInt()

  val total = (1 to n).map(_ => StdIn.readLine()).map(_.split(" ").map(_.toInt).sum).filter(_ > 1).sum
  println(total)

}

object Team {
  import io.StdIn.{readLine => rl}
  def main(args : Array[String]) {
    val gls = io.Source.stdin.getLines
    gls drop(1)
    println(gls count(x => 1 < x.map(_.toInt).reduce(_ + _)))
  }
}


object CF285B extends App {
  val in = new java.util.Scanner(System.in)
  val out = new java.io.PrintWriter(System.out)

  val n = in.nextInt
  val s = in.nextInt
  val t = in.nextInt
  val v = for (i <- 1 to n) yield in.nextInt
  def sol(x: Int, c: Int): Int =
    if (x == t) c
    else {
      val next = v(x-1)
      if (c > n || next == s) -1
      else sol(next, c+1)
    }

  out.println(sol(s, 0))
  out.flush
}

import java.util.Scanner
import java.io.PrintWriter
import java.util.Arrays.binarySearch
import scala.annotation.tailrec
import scala.collection.mutable
import scala.collection.mutable.ListBuffer
import scala.collection.mutable.ArrayBuffer
import scala.util.Random
import scala.util.control.Breaks._

object P285B extends App {
  val sc = new Scanner(System.in)
  val out = new PrintWriter(System.out)

  val N = sc.nextInt
  val S, T = sc.nextInt - 1
  val P = Array.fill(N)(sc.nextInt - 1)

  var n = 0
  var i = S
  breakable {
    while (true) {
      if (i == T) break
      else if (P(i) == -1) {
        n = -1
        break
      }
      else {
        val j = i
        i = P(i)
        P(j) = -1
        n = n + 1
      }
    }
  }

  out.println(n)
  out.close
}

import java.io._
import java.util.Locale
import scala.annotation.tailrec

object Easy {
  val reader = new BufferedReader(new InputStreamReader(System.in))
  def readInt = reader.readLine().toInt
  def readInts = reader.readLine().split(" ").map(_.toInt)
  def readLongs = reader.readLine().split(" ").map(_.toLong)

  val Array(n, s, t) = readInts
  val a = readInts
  @tailrec
  def rec(moves: Int, pos: Int): Int = if(pos == s && moves != 0) -1 else if(pos == t) moves else rec(moves + 1, a(pos - 1))
  def ans = rec(0, s)

  def main(a: Array[String]) {
    println(ans)
  }
}

object Main {
  def main(args: Array[String]) = {
    val nst = readLine().split(" ").map(_.toInt)
    val a = readLine().split(" ").map(_.toInt)
    val n = nst(0)
    val s = nst(1)
    val t = nst(2)

    var cur = s
    var step = 0

    for {
      _ <- 1 to n
      if step != -1
      if cur != t
    } {
      val next = a(cur - 1)
      if (next == s) step = -1
      else {
        step += 1
        cur = next
      }
    }

    println(step)
  }
}

object Solution extends App {
  val in = scala.io.Source.stdin.getLines()
  val Array(n, sn, tn) = in.next().split(' ').map(_.toInt)
  val data = in.next().split(' ').map(_.toInt - 1)
  val s = sn - 1
  var c = s
  val t = tn - 1
  var i = 0
  val marked = Array.ofDim[Boolean](n)
  while (!marked(c) && c != t) {
    marked(c) = true
    c = data(c)
    i += 1
  }
  if (c == t)
    println(i)
  else
    println(-1)

}


import scala.util.control.Breaks

object Polygon {
  def main(args: Array[String]): Unit = {
    val in = scala.io.StdIn
    val t = in.readLine().toInt
    for (t <- 0 until t) {
      val n = in.readLine().toInt
      val arr = Array.ofDim[Int](n, n)
      for (i <- 0 until n) {
        val row = in.readLine()
        for (j <- 0 until n) {
          arr(i)(j) = row(j) - 48
        }
      }
      var flag = false
      Breaks.breakable {
        for (i <- 0 until n) {
          for (j <- 0 until n) {
            if (arr(i)(j) == 1 && i != arr.length-1 && j != arr(0).length-1 && arr(i + 1)(j) != 1 && arr(i)(j + 1) != 1) {
              flag = true
              Breaks.break()
            }
          }
        }
      }
      println(if(flag) "NO" else "YES")
    }
  }
}

import java.io._
import java.util.StringTokenizer

import scala.reflect.ClassTag

object Main {
  def main(args: Array[String]): Unit = {
    try {
      val (in, out) =
        if (isOnlineJudge) {
          (
            new FastScanner(new InputStreamReader(System.in)),
            new PrintWriter(System.out)
          )
        } else {
          (
            new FastScanner(new FileReader(new File("src/resource/problem.in"))),
            new PrintWriter(new File("src/resource/problem.out"))
          )
        }
      new CF644(in, out).solve()
      out.flush()
      out.close()
    } catch {
      case e: IOException => e.printStackTrace()
    }
  }

  private[this] val isOnlineJudge = true

  class FastScanner(val streamReader: InputStreamReader) {
    private[this] val reader = new BufferedReader(streamReader, 32768)
    private[this] var tokenizer: StringTokenizer = _

    @inline private[this] final def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = Integer.parseInt(next())

    def nextLong(): Long = java.lang.Long.parseLong(next())

    def nextChar(): Char = next().charAt(0)

    def ni(): Int = nextInt()

    def nl(): Long = nextLong()

    def nc(): Char = nextChar()

    def ns(): String = next()

    def ns(n: Int): Array[Char] = ns().toCharArray

    def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)

    def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
      val A1, A2 = Array.ofDim[Int](n)
      REP(n) { i =>
        A1(i) = ni() + offset
        A2(i) = ni() + offset
      }
      (A1, A2)
    }

    def nm(n: Int, m: Int): Array[Array[Int]] = {
      val A = Array.ofDim[Int](n, m)
      REP(n) { i =>
        REP(m) { j =>
          A(i)(j) = ni()
        }
      }
      A
    }

    def nal(n: Int): Array[Long] = map(n)(_ => nl())

    def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n)(_ => ns(m))
  }

  def REP(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while (i < N) {
      f(i);
      i += 1
    }
  }

  def REP_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while (i >= offset) {
      f(i);
      i -= 1
    }
  }

  def TO(from: Int, to: Int)(f: Int => Unit): Unit = {
    REP(to - from + 1, from)(f)
  }

  def map[@specialized A: ClassTag](n: Int, offset: Int = 0)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    REP(n)(i => res(i) = f(i + offset))
    res
  }

  def sumL(as: Array[Int]): Long = {
    var s = 0L
    REP(as.length)(i => s += as(i))
    s
  }

  def cumSum(as: Array[Int]): Array[Long] = {
    val cum = Array.ofDim[Long](as.length + 1)
    REP(as.length) { i =>
      cum(i + 1) = cum(i) + as(i)
    }
    cum
  }
}

class CF644(fScanner: Main.FastScanner, out: java.io.PrintWriter) {

  import Main._
  import fScanner._

  def solve(): Unit = {
    val t = ni()
    REP(t) { _ =>
      val n = ni()
      a = Array.ofDim[Int](n,n)
      conn = Array.fill[Boolean](n,n)(false)
      REP(n) { i =>
        val s = ns(n)
        REP(n) { j =>
          a(i)(j) = s(j) - '0'
          if(j == n-1 && a(i)(j) == 1) conn(i)(j) = true
          if(i == n-1 && a(i)(j) == 1) conn(i)(j) = true
        }
      }

      vis = Array.fill[Int](n,n)(0)
      REP(n) { i =>
        if(a(i)(n-1) == 1) {
          var j = 1
          while (n-j-1 >=0 && a(i)(n-j-1) == 1) {
            conn(i)(n-j-1) = true
            j += 1
          }
        }
        if(a(n-1)(i) == 1) {
          var j = 1
          while (n-j-1 >=0 && a(n-j-1)(i) == 1) {
            conn(n-j-1)(i) = true
            j += 1
          }
        }
      }
      var yes = true
      REP_r(n-1) { i =>
        REP_r(n-1) { j =>
          if(a(i)(j) == 1 && !conn(i)(j)) {
            conn(i)(j) = conn(i+1)(j) | conn(i)(j+1)
          }
          if(a(i)(j) == 1 && !conn(i)(j)) yes = false
        }
      }
      if(yes)
        out.println("YES")
      else out.println("NO")
    }
  }
  var a: Array[Array[Int]] = _
  var conn: Array[Array[Boolean]] = _
  var vis: Array[Array[Int]] = _
}

object E extends App {
  val t = scala.io.StdIn.readInt()

  (0 until t).foreach { _ =>
    val n = scala.io.StdIn.readInt()
    val pn = (0 until n).foldLeft(Array.ofDim[Array[Byte]](n)) { (pn, i) =>
      pn(i) = scala.io.StdIn.readLine().split("").map(_.toByte)
      pn
    }

    val ans = pn.init.zipWithIndex.forall {
      case (p, i) =>
        p.zip(p.tail).corresponds(pn(i + 1).init) {
          case ((u, r), d) =>
            u == 0 || d == 1 || r == 1
        }
    }

    if (ans) println("YES")
    else println("NO")
  }
}


object D {
  def main(args: Array[String]) {
    val n = readLine.toInt;
    var a = readLine.split(" ").toList.map(_.toLong)

    a = {
      var sum = 0L
      a.reverse.map { v =>
        if (sum >= 0) {
          sum -= v
          -v
        } else {
          sum += v
          v
        }
      }.reverse
    }

    if (a.sum < 0) {
      a = a.map(-_)
    }
    a.foreach { v =>
      if (v >= 0) {
        print("+")
      } else {
        print("-")
      }
    }
  }
}

object test5 extends App {
    val n=readInt
    val a=readLine.split(" ").map(_.toInt)
    val c=new Array[Char](n)

    var sum=0
    def solve()={
        for(i<-n-1 to 0 by -1){
            if(sum>0){
                sum-=a(i)
                c(i)='-'
            }
            else{
                sum+=a(i)
                c(i)='+'
            }
        }
    }

    solve()

    if(sum>0){
        println(c.mkString)
    }
    else{
        println(c.map(c=>if(c=='+') '-' else '+').mkString)
    }
}

object test5 extends App {
    val n=readInt
    val a=readLine.split(" ").map(_.toInt)
    val c=new Array[Char](n)

    var sum=0

    for(i<-n-1 to 0 by -1){
        if(sum>0){
            sum-=a(i)
            c(i)='-'
        }
        else{
            sum+=a(i)
            c(i)='+'
        }
    }

    if(sum>0){
        println(c.mkString)
    }
    else{
        println(c.map(c=>if(c=='+') '-' else '+').mkString)
    }
}

import scala.util.Sorting._
import math._
import scala.Array

object Contest extends App {
  val n = readInt()
  val arr = readLine().split(" ").map(_.toInt).reverse
  var cur = 0
  var ans: StringBuilder = new StringBuilder
  for (i <- arr){
    if (abs(cur - i) < abs(cur + i)){
      ans.append("-")
      cur -= i
    }
    else{
      ans.append("+")
      cur += i
    }
  }
  ans = ans.reverse
  if (cur < 0){
    var t: StringBuilder = new StringBuilder
    for (c <- ans)
      if (c == '+') t.append("-") else t.append("+")
    ans = t
  }
  println(ans)
}

object test5 extends App {
    val n=readInt
    val a=readLine.split(" ").map(_.toInt)
    val c=new Array[Char](n)

    def solve()={
        for(i<-n-2 to 0 by -1){
            if(sum>0){
                sum-=a(i)
                c(i)='-'
            }
            else{
                sum+=a(i)
                c(i)='+'
            }
        }
    }
    var sum=a.last
    c(n-1)='+'
    solve()

    if(sum>0){
        println(c.mkString)
    }
    else{
        println(c.map(c=>if(c=='+') '-' else '+').mkString)
    }
}

import scala.util.Sorting._
import math._
import scala.Array

object Contest extends App {
  val n = readInt()
  val arr = readLine().split(" ").map(_.toInt).reverse
  var cur = 0
  var ans = ""
  for (i <- arr){
    if (abs(cur - i) < abs(cur + i)){
      ans += "-"
      cur -= i
    }
    else{
      ans += ans
      cur += i
    }
  }
  ans = ans.reverse
  if (cur < 0){
    var t = ""
    for (c <- ans)
      if (c == '+') t += "-" else t += "+"
    ans = t
  }
  println(ans)
}

object test5 extends App {
    val n=readInt
    val a=readLine.split(" ").map(_.toInt)
    val c=new Array[Char](n)

    def solve()={
        for(i<-n-2 to 0 by -1){
            if(sum>0){
                sum-=a(i)
                c(i)='-'
            }
            else{
                sum+=a(i)
                c(i)='+'
            }
        }
    }
    var sum=a.last
    c(n-1)='+'
    solve()

    if(sum>=0 && sum<=a.head){
        println(c.mkString)
        exit(0)
    }

    sum= -a.last
    c(n-1)='-'
    solve()
    if(sum>=0 && sum<=a.head){
        println(c.mkString)
    }
    else println("fuck")
}

object test5 extends App {
    val n=readInt
    val a=readLine.split(" ").map(_.toInt)
    val c=new Array[Char](n)

    def solve()={
        for(i<-n-2 to 0 by -1){
            if(sum>0){
                sum-=a(i)
                c(i)='-'
            }
            else{
                sum+=a(i)
                c(i)='+'
            }
        }
    }
    var sum=a.last
    c(n-1)='+'
    solve()

    if(sum>=0 && sum<=a.head){
        println(c.mkString)
        exit(0)
    }

    sum= -a.last
    c(n-1)='-'
    solve()
    println(c.mkString)
}

object D {
  def main(args: Array[String]) {
    val n = readLine.toInt;
    var a = readLine.split(" ").toList.map(_.toLong)

    var sum = 0L

    a = a.reverse.map { v =>
      if (sum >= 0) {
        sum -= v
        -v
      } else {
        sum += v
        v
      }
    }.reverse

    if (a.sum < 0) {
      a.map(-_)
    }
    a.foreach { v =>
      if (v >= 0) {
        print("+")
      } else {
        print("-")
      }
    }
  }
}


import Utils._, annotation.tailrec, collection.mutable, util._, control._, math.Ordering.Implicits._, collection.Searching._

object _749A extends CodeForcesApp {
  override def apply(io: IO) = {import io.{read, write, writeAll, writeLine}
    val n = read[Int]

    val ans = if (n%2 == 0) {
      Vector.fill(n/2)(2)
    } else {
      Vector.fill((n-3)/2)(2) :+ 3
    }

    write(ans.length).writeLine().writeAll(ans)
  }
}
/****************************[Ignore Template Below]****************************************/
trait CodeForcesApp {
  lazy val isOnlineJudge: Boolean = sys.props.get("ONLINE_JUDGE").exists(_.toBoolean)
  def debug(x: => Any): Unit = if (!isOnlineJudge) println(x)
  def apply(io: IO): io.type
  def main(args: Array[String]): Unit = this(new IO(System.in, System.out)).close()
}
object Utils {
  import scala.collection.mutable, mutable.{Map => Dict}
  implicit class GenericExtensions[A](val a: A) extends AnyVal {
    def in(set: collection.Set[A]): Boolean = set(a)
    def some: Option[A] = Some(a)
    def partialMatch[B](f: PartialFunction[A, B]): Option[B] = f lift a
  }
  implicit class TraversableExtensions[A, C[X] <: Traversable[X]](val t: C[A]) extends AnyVal {
    def onlyElement: A = assuming(t.size == 1)(t.head)
    def groupByIdentifier[K](f: A => K): Map[K, A] = t.groupBy(f).mapValues(_.onlyElement)
    def duplicates(implicit b: C of A): C[A] = {
      val elems = mutable.Set.empty[A]
      (t collect {case i if !elems.add(i) => i}).to[C]
    }
    def sumWith[B](f: A => B)(implicit n: Numeric[B]): B = t.foldLeft(n.zero) {case (p, i) => n.plus(p, f(i))}
    def maxWith[B](f: A => B)(implicit ord: Ordering[B]): Option[B] = t.view.map(f).reduceOption(ord.max)
    def minWith[B](f: A => B)(implicit ord: Ordering[B]): Option[B] = t.view.map(f).reduceOption(ord.min)
    def zipWith[B](f: A => B)(implicit b: C of (A, B)): C[(A, B)] = (t map {i => i -> f(i)}).to[C]
    def mapTo[B](f: A => B)(implicit b: C of (A, B)): Map[A, B] = zipWith(f).toUniqueMap
    def whenNonEmpty[B](f: t.type => B): Option[B] = when(t.nonEmpty)(f(t))  // e.g. grades.whenNonEmpty(_.max)
    def collectFirst[B](f: PartialFunction[A, Option[B]]): Option[B] = t.view collect f collectFirst {case Some(x) => x}
    def toMutableMultiSet: Dict[A, Int] = {
      val c = map[A] to 0
      t.foreach(i => c(i) += 1)
      c
    }
    def toMultiSet: Map[A, Int] = toMutableMultiSet.toMap withDefaultValue 0
    def X[B](s: Traversable[B]): Iterator[(A, B)] = for {a <- t.toIterator; b <- s} yield (a, b)
  }
  implicit class ArrayExtensions[A <: AnyRef](val a: Array[A]) extends AnyVal {
    def sort(from: Int, until: Int)(implicit cmp: Ordering[A]): Unit = java.util.Arrays.sort(a, from, until, cmp) //sort [from, until)
  }
  implicit class ArrayExtensions2[A](val a: Array[A]) extends AnyVal {
    def sort(from: Int, until: Int)(implicit cmp: Ordering[A]): Unit = a.view(from, until).sorted.copyToArray(a, from)
  }
  implicit class SeqExtensions[A, C[X] <: Seq[X]](val s: C[A]) extends AnyVal {
    def isRotationOf(t: Seq[A]): Boolean = s ++ s containsSlice t
  }
  implicit class PairExtensions[A, B](val p: (A, B)) extends AnyVal {
    def key = p._1
    def value = p._2
  }
  implicit class PairsExtensions[A, B, C[X] <: Traversable[X]](t: C[(A, B)]) {
    def toUniqueMap: Map[A, B] = t.groupByIdentifier(_.key).mapValues(_.value)
    def toMultiMap(implicit b: C of B): Map[A, C[B]] = t.groupBy(_.key).mapValues(_.map(_.value).to[C])
    def swap(implicit b: C of (B, A)): C[(B, A)] = t.map(_.swap).to[C]
  }
  implicit class MapExtensions[K, V, C[X, Y] <: Map[X, Y]](val m: C[K, V]) extends AnyVal {
    def lastKeyOption: Option[K] = m.lastOption.map(_.key)
    def firstKeyOption: Option[K] = m.headOption.map(_.key)
    def invert: Map[V, Set[K]] = m.toSet[(K, V)].swap.toMultiMap
    def mapKeys[K2](f: K => K2): Map[K2, V] = m map {case (k, v) => f(k) -> v}
    def toMutable: Dict[K, V] = Dict.empty[K, V] ++ m
  }
  implicit class MutableMapExtensions[K, V](val m: mutable.Map[K, V]) extends AnyVal {
    def removeLast(): Option[(K, V)] = m.lastOption.map(removeNode)
    def removeFirst(): Option[(K, V)] = m.headOption.map(removeNode)
    private def removeNode(kv: (K, V)): (K, V) = {
      m -= kv.key
      kv
    }
  }
  implicit class SetExtensions[A, C[A] <: Set[A]](val s: C[A]) extends AnyVal {
    def notContains(x: A): Boolean = !(s contains x)
    def toMutable = mutable.Set.empty[A] ++ s
  }
  implicit class IterableExtensions[A, C[A] <: Seq[A]](val s: C[A]) extends AnyVal {
    def indicesWhere(f: A => Boolean): Seq[Int] = s.zipWithIndex.collect {case (a, i) if f(a) => i}
    def deleteAtIndex(i: Int, howMany: Int = 1) = s.patch(i, Nil, howMany)
    def insertAtIndex(i: Int, items: Seq[A]) = s.patch(i, items, 0)
    def findWithIndex(f: A => Boolean): Option[(A, Int)] = s.zipWithIndex find {case (a, _) => f(a)}
    def foldUntil[B](z: B)(f: (B, A) => B)(c: B => Boolean): Option[(B, Int)] = s.view.scanLeft(z)(f).findWithIndex(c)
  }
  implicit class BooleanExtensions(val x: Boolean) extends AnyVal {
    def to[A](implicit n: Numeric[A]) = if(x) n.one else n.zero
    def toEnglish = if(x) "YES" else "NO"
  }
  implicit class IntExtensions(val x: Int) extends AnyVal {
    import java.lang.{Integer => JInt}
    def withHighestOneBit: Int = JInt.highestOneBit(x)
    def withLowestOneBit: Int = JInt.lowestOneBit(x)
    def numberOfLeadingZeroes: Int = JInt.numberOfLeadingZeros(x)
    def numberOfTrailingZeroes: Int = JInt.numberOfTrailingZeros(x)
    def bitCount: Int = JInt.bitCount(x)
    def setLowestBits(n: Int): Int = x | (left(n) - 1)
    def clearLowestBits(n: Int): Int = x & left(n, ~0)
    def setBit(i: Int, set: Boolean): Int = if (set) setBit(i) else clearBit(i)
    def setBit(i: Int): Int = x | left(i)
    def clearBit(i: Int): Int = x & ~left(i)
    def toggleBit(i: Int): Int = x ^ left(i)
    def testBit(i: Int): Boolean = ((x >> i) & 1) == 1
    private def left(bits: Int, num: Int = 1) = assuming(bits >= 0 && bits < JInt.SIZE)(num << bits)
    def X(y: Int): Iterator[(Int, Int)] = until(x) X until(y)
  }
  implicit class LongExtensions(val x: Long) extends AnyVal {
    import java.lang.{Long => JLong}
    def withHighestOneBitSet: Long = JLong.highestOneBit(x)
    def withLowestOneBitSet: Long = JLong.lowestOneBit(x)
    def numberOfLeadingZeroes: Int = JLong.numberOfLeadingZeros(x)
    def numberOfTrailingZeroes: Int = JLong.numberOfTrailingZeros(x)
    def bitCount: Int = JLong.bitCount(x)
    def setLowestBits(n: Int): Long = x | (left(n) - 1)
    def clearLowestBits(n: Int): Long = x & left(n, ~0L)
    def setBit(i: Int, set: Boolean): Long = if (set) setBit(i) else clearBit(i)
    def setBit(i: Int): Long = x | left(i)
    def clearBit(i: Int): Long = x & ~left(i)
    def toggleBit(i: Int): Long = x ^ left(i)
    def testBit(i: Int): Boolean = ((x >> i) & 1) == 1
    private def left(bits: Int, num: Long = 1L) = assuming(bits >= 0 && bits < JLong.SIZE)(num << bits)
  }
  implicit class NumericExtensions[A](val x: A)(implicit n: Numeric[A]) {
    import Numeric.Implicits._
    def **(i: Int): A = if (i == 0) n.one else {
      val h = x ** (i/2)
      if (i%2 == 0) h * h else h * h * x
    }
    def distance(y: A) = (x - y).abs()
    def nonNegative: Option[A] = when(n.gteq(x, n.zero))(x)
    def whenNegative(f: => A): A = nonNegative getOrElse f   //e.g. students indexOf "Greg" whenNegative Int.MaxValue
  }
  implicit class IntegralExtensions[A](val x: A)(implicit n: Integral[A]) {
    import scala.collection.immutable.NumericRange, Integral.Implicits._
    def mod(y: A): A = x - ((x / y) * y) //(x/y)*y + (x mod y) == x
    def -->(y: A): NumericRange.Inclusive[A] = NumericRange.inclusive(x, y, if (n.lteq(x, y)) n.one else -n.one)
    def ceilDiv(y: A): A = if (mod(y) == n.zero) x/y else n.one + (x/y)
  }
  implicit def toPoint[A](p: (A, A))(implicit n: Numeric[A]) = new java.awt.geom.Point2D.Double(n.toDouble(p._1), n.toDouble(p._2))
  def desc[A: Ordering]: Ordering[A] = asc[A].reverse  //e.g. students.sortBy(_.height)(desc)
  def asc[A](implicit order: Ordering[A]): Ordering[A] = order
  def map[K] = new {
    def to[V](default: => V): Dict[K, V] = using(_ => default)
    def using[V](f: K => V): Dict[K, V] = new mutable.HashMap[K, V]() {
      override def apply(key: K) = getOrElseUpdate(key, f(key))
    }
  }
  def newMultiMap[K, V] = map[K] to mutable.Set.empty[V]
  def memoize[A, B](f: A => B): A ==> B = map[A] using f
  implicit class FuzzyDouble(val x: Double) extends AnyVal {
    def  >~(y: Double) = x > y + eps
    def >=~(y: Double) = x >= y + eps
    def  ~<(y: Double) = y >=~ x
    def ~=<(y: Double) = y >~ x
    def  ~=(y: Double) = (x distance y) <= eps
  }
  def toPolygon(points: Traversable[(Int, Int)]): java.awt.Polygon = {
    val (xs, ys) = points.unzip
    new java.awt.Polygon(xs.toArray, ys.toArray, points.size)
  }
  def toShape(points: Traversable[(Double, Double)]): java.awt.geom.GeneralPath = {
    val shape = new java.awt.geom.GeneralPath()
    points.headOption foreach {case (x, y) => shape.moveTo(x, y)}
    points.tail foreach {case (x, y) => shape.lineTo(x, y)}
    shape.closePath()
    shape
  }
  def until(until: Int): Range = 0 until until
  def not[A](f: A => Boolean): A => Boolean = {x: A => !f(x)}
  def assuming[A](cond: => Boolean)(f: => A): A = {require(cond); f}
  @inline def when[A](check: Boolean)(f: => A): Option[A] = if (check) f.some else None
  def repeat[U](n: Int)(f: => U): Unit = (1 to n).foreach(_ => f)
  type of[C[_], A] = scala.collection.generic.CanBuild[A, C[A]]
  type ==>[A, B] = collection.Map[A, B]
  val mod: Int = (1e9 + 7).toInt
  val eps: Double = 1e-9
}
/***********************************[Scala I/O]*********************************************/
import Utils._, java.io._, java.util.StringTokenizer

class IO(in: BufferedReader, out: BufferedWriter) extends Iterator[String] with AutoCloseable with Flushable {
  def this(reader: Reader, writer: Writer) = this(new BufferedReader(reader), new BufferedWriter(writer))
  def this(in: InputStream, out: OutputStream) = this(new InputStreamReader(in), new OutputStreamWriter(out))

  private[this] val tokenizers = Iterator.continually(in.readLine()).takeWhile(_ != null).map(new StringTokenizer(_)).buffered
  val printer = new PrintWriter(out, true)

  @inline private[this] def tokenizer() = {
    while(tokenizers.nonEmpty && !tokenizers.head.hasMoreTokens) tokenizers.next()
    when(tokenizers.nonEmpty)(tokenizers.head)
  }

  def read[A](implicit read: IO.Read[A]): A = read.apply(this)
  def read[C[_], A: IO.Read](n: Int)(implicit b: C of A): C[A] = Iterator.fill(n)(read).to[C]

  def readTill(delim: String): String = tokenizer().get.nextToken(delim)
  def readTillEndOfLine(): String = readTill("\n\r")

  override def next() = tokenizer().get.nextToken()
  override def hasNext = tokenizer().nonEmpty

  def write(obj: Any): this.type = {
    printer.print(obj)
    this
  }
  def writeLine(): this.type = {
    printer.println()
    this
  }
  def writeLine(obj: Any): this.type = {
    printer.println(obj)
    this
  }
  def writeAll(obj: Traversable[Any], separator: String = " "): this.type = write(obj mkString separator)
  def query[A: IO.Read](question: Any): A = writeLine(question).read

  override def flush() = printer.flush()
  def close() = {
    flush()
    in.close()
    printer.close()
  }
}
object IO {
  class Read[A](val apply: IO => A) {
    def map[B](f: A => B): Read[B] = new Read(apply andThen f)
  }
  object Read {
    implicit def collection[C[_], A: Read](implicit b: C of A): Read[C[A]]         = new Read(r => r.read[C, A](r.read[Int]))
    implicit val string                                       : Read[String]       = new Read(_.next())
    implicit val char                                         : Read[Char]         = string.map(_.toTraversable.onlyElement)
    implicit val int                                          : Read[Int]          = string.map(_.toInt)
    implicit val long                                         : Read[Long]         = string.map(_.toLong)
    implicit val bigInt                                       : Read[BigInt]       = string.map(BigInt(_))
    implicit val double                                       : Read[Double]       = string.map(_.toDouble)
    implicit val bigDecimal                                   : Read[BigDecimal]   = string.map(BigDecimal(_))
    implicit def tuple2[A: Read, B: Read]                     : Read[(A, B)]       = new Read(r => (r.read[A], r.read[B]))
    implicit def tuple3[A: Read, B: Read, C: Read]            : Read[(A, B, C)]    = new Read(r => (r.read[A], r.read[B], r.read[C]))
    implicit def tuple4[A: Read, B: Read, C: Read, D: Read]   : Read[(A, B, C, D)] = new Read(r => (r.read[A], r.read[B], r.read[C], r.read[D]))
    implicit val boolean                                      : Read[Boolean]      = string map {s =>
      s.toLowerCase match {
        case "yes" | "true" | "1" => true
        case "no" | "false" | "0" => false
        case _ => s.toBoolean
      }
    }
  }
}

object BachgoldProblem extends App{
  val n = io.StdIn.readInt()
  println(n / 2)
  println(n match{
    case n if n <= 3 => n
    case n if n % 2 == 0 => "2" + " 2" * (n / 2 - 1)
    case _ => "2" + " 2" * (n / 2 - 2) + " 3"
  })
}

object A749 {

  import IO._
  import collection.{mutable => cu}

  def main(args: Array[String]): Unit = {
    val Array(n) = readInts(1)
    println(n/2)
    if(n%2 == 1) {
      println("3" ++ " 2" * ((n-2)/2))
    } else {
      println("2 " * (n/2))
    }
  }

  object IO {
    private val input = new java.io.BufferedReader(new java.io.InputStreamReader(System.in))

    //    private val input = new java.io.BufferedReader(new java.io.InputStreamReader(new java.io.FileInputStream("src/input")))
    @inline def read: String = input.readLine()

    @inline def tokenizeLine = new java.util.StringTokenizer(read)

    def readInt: Int = tokenizeLine.nextToken.toInt

    def readInts(n: Int): Array[Int] = {
      val tl = tokenizeLine
      Array.fill(n)(tl.nextToken.toInt)
    }

    def readLongs(n: Int): Array[Long] = {
      val tl = tokenizeLine
      Array.fill(n)(tl.nextToken.toLong)
    }

    def readBigs(n: Int): Array[BigInt] = {
      val tl = tokenizeLine
      Array.fill(n)(BigInt(tl.nextToken))
    }
  }

}

object A749 {

  import IO._
  import collection.{mutable => cu}

  def main(args: Array[String]): Unit = {
    val Array(n) = readInts(1)
    if(n%2 == 1) {
      println("3" ++ " 2" * ((n-2)/2))
    } else {
      println("2 " * (n/2))
    }
  }

  object IO {
    private val input = new java.io.BufferedReader(new java.io.InputStreamReader(System.in))

    //    private val input = new java.io.BufferedReader(new java.io.InputStreamReader(new java.io.FileInputStream("src/input")))
    @inline def read: String = input.readLine()

    @inline def tokenizeLine = new java.util.StringTokenizer(read)

    def readInt: Int = tokenizeLine.nextToken.toInt

    def readInts(n: Int): Array[Int] = {
      val tl = tokenizeLine
      Array.fill(n)(tl.nextToken.toInt)
    }

    def readLongs(n: Int): Array[Long] = {
      val tl = tokenizeLine
      Array.fill(n)(tl.nextToken.toLong)
    }

    def readBigs(n: Int): Array[BigInt] = {
      val tl = tokenizeLine
      Array.fill(n)(BigInt(tl.nextToken))
    }
  }

}

object BachgoldProblem extends App{
  val n = io.StdIn.readInt()
  println(n / 2)
  if(n % 2 == 0) println("2" +  " 2" * (n / 2 - 1))
  else println("2" + " 2" * (n / 2 - 2) + " 3")
}