'use strict';

let cin = '';
let current_line = 0;

function     next() { return cin[current_line++]; }
function  nextInt() { return parseInt(next()); }
function nextList() { return next().split(' ').map(s => parseInt(s)); }
function   print(x) { process.stdout.write(x); }

function ones_in_2x2(s, i, j) {
    let count = 0;
    for (let u = i, k = 0; k < 2; ++k, ++u)
        for (let v = j, l = 0; l < 2; ++l, ++v)
            if (s[u][v] == '1')
                ++count;
    return count; }

function main() {
    for (let t = nextInt(); t > 0; --t) {
        const size = nextList();
        const n = size[0], m = size[1], p = n-1, q = m-1;
        let s = Array(n);
        for (let i = 0; i < n; ++i)
            s[i] = next();
        let elegant = true;
        for (let i = 0; i < p && elegant; ++i)
            for (let j = 0; j < q && elegant; ++j)
                elegant = ones_in_2x2(s,i,j) != 3;
        console.log(elegant? "YES": "NO"); } }

process.stdin.resume();
process.stdin.setEncoding('utf-8');
process.stdin.on('data', line => { cin += line; });
process.stdin.on('end', _ => {
    cin = cin.trim().split('\n').map(line => { return line.trim(); });
    main(); });

// const { readline, print } = require('@ip-algorithmics/codeforces-io');

function hasOnlyOneEmptyInSquare(array, a, b) {
  var countZero = 0;

  for (var i = 0; i < 2; ++i) {
    for (var j = 0; j < 2; ++j) {
      if (array[a+i][b+j] === 0) {
        countZero++;
      }
    }
  }

  return countZero === 1;
}

var line = readline().split(' ').map(x => parseInt(x))
var testCases = line[0]

var n, m
while (testCases--) {
  line = readline().split(' ').map(x => parseInt(x))
  n = line[0]
  m = line[1]

  var array = new Array(n)

  for (var i = 0; i < n ; ++i) {
    array[i] = [];
    line = readline();

    for (var j = 0; j < line.length; ++j) {
      array[i].push(parseInt(line[j]));
    }
  }

  if (n < 2 || m < 2) {
    print('YES')
    continue;
  }

  var isElegant = true;
  for (i = 0; i < array.length - 1; i++) {
    for (j = 0; j < array[i].length - 1; j++) {
      if (hasOnlyOneEmptyInSquare(array, i, j)) {
        isElegant = false;
      }
    }
  }

  print (isElegant ? 'YES' : 'NO')
}

var tests = parseInt(readline());
    var n,m;
    var mat;
    var visited;

    function checkSquare(i, j) {
        visited[i][j] = 1;
        var col = j;
        while (col < m && mat[i][col]) {
            col++;
            visited[i][col] = 1;
        }
        var width = col - j;

        var row = i+1;
        while (row < n && mat[row][j]) {
            if (j > 0 && mat[row][j-1]) {
                return false;
            }
            col = j;
            while (col < j + width) {
                visited[row][col] = 1;
                if (!mat[row][col]) {
                    return false;
                }
                col++;
            }
            visited[row][col] = 1;
            if (mat[row][col]) {
                return false;
            }
            row++;
        }
        if (row < n) {
            for (var c = j; c < j + width; c++) {
                visited[row][c] = 1;
                if (mat[row][c]) {
                    return false;
                }
            }
        }

        return true;
    }

    for (var t=0; t < tests; t++) {
        var nm = readline().split(' ').map(x=>parseInt(x));
        n = nm[0];
        m = nm[1];
        mat = [];
        visited = [];
        for (var i = 0; i < n; i++) {
            mat.push(readline().split('').map(x=>parseInt(x)));
            visited.push(new Array(m).fill(0));
        }

        var ans = 'YES';
        for (var i = 0; i < n; i++) {
            for (var j = 0; j < m; j++) {
                if (visited[i][j]) {
                    continue;
                }
                if (!mat[i][j]) {
                    visited[i][j] = 1;
                    continue;
                }
                if (checkSquare(i, j)) {
                    continue;
                }
                ans = 'NO';
            }
        }
        print(ans);
    }

"use strict";

process.stdin.resume();
process.stdin.setEncoding("utf-8");

let inputStr = "";
let currentLne = 0;

process.stdin.on('data' , inputStdin =>{

	 inputStr += inputStdin;
})

process.stdin.on('end' , _ =>{
    inputStr = inputStr.trim().split('\n').map( string =>{
    	return string.trim();
    })

main();
})

function readline(){
   return inputStr[currentLne++];
}

function main(){

    let t = parseInt(readline());
    let c= 0;
    while(c<t){

      let m = readline().split(" ").map(a =>parseInt(a));
      let m0 = m[0];
      let countRow = 0 ;
      let arr =[];
      while(countRow < m0){
        let row = readline().split("").map(b=>parseInt(b));
        arr.push(row);
        countRow++;
      }

    let eleg =true;
    let count =0;
    // for (var k = 0; k < arr.length; k++) {
    //     if(arr[k].indexOf(1) == -1 ){
    //          count++;
    //     }
    // }

    for (var i = 0; i < arr.length-1; i++) {

        for (var j = 0; j < arr[0].length; j++) {

           //

          if(arr[i][j] ==1){
               if(j !==0 && arr[i+1][j] ==1 && arr[i+1][j-1] ==1 && arr[i][j-1] ==0 ){
                    eleg= false;
                    // console.log(i,j,"1st")
                    break;
               }

               if(arr[i+1][j]==1 && arr[i+1][j+1] ==1 && arr[i][j+1] == 0){
                   eleg =false;
                    // console.log(i,j,"2st")
                    break;
               }

               if(arr[i][j+1]==1 && arr[i+1][j]==1 && arr[i+1][j+1] ==0){
                eleg = false;
                 // console.log(i,j,"3st")
                 break;
               }

               if(i !== 0 && arr[i][j+1]==1 && arr[i-1][j+1] == 1 && arr[i-1][j] == 0){
                eleg =false;
                 // console.log(i,j,"4st")
                 break;
               }

               if(arr[i][j+1]==1 && arr[i+1][j+1] ==1 && arr[i+1][j]==0){
                eleg =false;
                // console.log(i,j,"5st")
                break;
               }
          }
        }

    }

    // if(count == arr.length*arr[0].length ){
    //     eleg = false;
    // }

    if(eleg){
        console.log("yes");
    }else{
        console.log("no");
    }

    c++;
    }

}

//type input.txt | node code.js

"use strict";
const fs = require("fs");
//let filename = "0.txt";
let filename = 0;
const input = fs.readFileSync(filename, "utf8").trim().split("\n");
let line = 0;
function readline() {
  return input[line++];
}
const solve = () => {
  let t = parseInt(readline());
  while (t--) {
    let [n, m] = readline()
      .trim()
      .split(" ")
      .map((cur) => parseInt(cur));
    let arr = [];
    for (let i = 0; i < n; i++)
      arr.push(
        readline()
          .trim()
          .split("")
          .map((cur) => parseInt(cur))
      );
    let sum = true;
    for (let i = 0; i < n - 1; i++) {
      for (let j = 0; j < m - 1; j++) {
        if (
          arr[i][j] + arr[i + 1][j] + arr[i][j + 1] + arr[i + 1][j + 1] ===
          3
        ) {
          sum = false;
          break;
        }
      }
    }
    console.log(`${sum === true ? "YES" : "NO"}`);
  }
};
solve();

'use strict';

var readline = require('readline');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var readline__default = /*#__PURE__*/_interopDefaultLegacy(readline);

function solve(n, m, s) {
  const sum = Array.from({
    length: n
  }, () => new Array(m).fill(0));
  for (let i = 0; i < n; i++) {
    for (let j = 0; j < m; j++) {
      var _sum$j, _sum, _sum$i, _sum2, _sum3;
      sum[i][j] = ((_sum$j = (_sum = sum[i - 1]) === null || _sum === void 0 ? void 0 : _sum[j]) !== null && _sum$j !== void 0 ? _sum$j : 0) + ((_sum$i = sum[i][j - 1]) !== null && _sum$i !== void 0 ? _sum$i : 0) - ((_sum2 = (_sum3 = sum[i - 1]) === null || _sum3 === void 0 ? void 0 : _sum3[j - 1]) !== null && _sum2 !== void 0 ? _sum2 : 0) + Number(s[i][j]);
    }
  }
  const p = [...new Array(n * m).keys()];
  const find = i => {
    if (p[i] !== i) p[i] = find(p[p[i]]);
    return p[i];
  };
  const union = (i, j) => {
    const ri = find(i),
          rj = find(j);
    if (ri !== rj) p[rj] = ri;
  };
  const dirs = [[0, 1], [1, 0]];
  for (let i = 0; i < n; i++) {
    for (let j = 0; j < m; j++) {
      const a = i * m + j;
      if (s[i][j] === '0') {
        p[a] = -1;
        continue;
      }
      for (let [di, dj] of dirs) {
        const ni = di + i,
              nj = dj + j,
              b = ni * m + nj;
        if (ni < 0 || ni >= n || nj < 0 || nj >= m || s[ni][nj] === '0') continue;
        union(a, b);
      }
    }
  }
  const scc = new Map();
  for (let [i, num] of p.entries()) {
    if (num === -1) continue;
    const root = find(num);
    if (!scc.has(root)) scc.set(root, []);
    scc.get(root).push(i);
  }
  for (let [, points] of scc) {
    var _sum$rb, _sum$rr, _sum4, _sum5, _sum6;
    let ll = Infinity,
        lt = Infinity,
        rr = -Infinity,
        rb = -Infinity;
    for (let num of points) {
      const i = Math.floor(num / m),
            j = num % m;
      ll = Math.min(ll, j);
      lt = Math.min(lt, i);
      rr = Math.max(rr, j);
      rb = Math.max(rb, i);
    }
    if (sum[rb][rr] - ((_sum$rb = sum[rb][ll - 1]) !== null && _sum$rb !== void 0 ? _sum$rb : 0) - ((_sum$rr = (_sum4 = sum[lt - 1]) === null || _sum4 === void 0 ? void 0 : _sum4[rr]) !== null && _sum$rr !== void 0 ? _sum$rr : 0) + ((_sum5 = (_sum6 = sum[lt - 1]) === null || _sum6 === void 0 ? void 0 : _sum6[ll - 1]) !== null && _sum5 !== void 0 ? _sum5 : 0) !== (rr - ll + 1) * (rb - lt + 1)) return 'NO';
  }
  return 'YES';
}

async function main(read) {
  let t = Number(await read());
  let res = [];
  while (t--) {
    const [n, m] = (await read()).split(' ').map(Number);
    const a = [];
    let tmp = n;
    while (tmp--) {
      a.push(await read());
    }
    res.push(solve(n, m, a));
  }
  console.log(res.join('\n'));
}

const rl = readline__default["default"].createInterface({
  input: process.stdin,
  output: process.stdout
});
void async function () {
  const inputs = [];
  let i = 0;
  for await (const line of rl) {
    inputs.push(line.trim());
  }
  function read() {
    return inputs[i++];
  }
  await main(read);
}();

'use strict';

let cin = '';
let current_line = 0;

function     next() { return cin[current_line++]; }
function  nextInt() { return parseInt(next()); }
function nextList() { return next().split(' ').map(s => parseInt(s)); }
function   print(x) { process.stdout.write(x); }

function ones_in_2x2(s, i, j) {
    let count = 0;
    for (let u = i, k = 0; k < 2; ++k, ++u)
        for (let v = j, l = 0; l < 2; ++l, ++v)
            if (s[u][v] == '1')
                ++count;
    return count; }

function main() {
    for (let t = nextInt(); t > 0; --t) {
        const size = nextList();
        const n = size[0], p = n-1, q = size[1]-1;
        let s = Array(n);
        for (let i = 0; i < n; ++i)
            s[i] = next();
        let elegant = true;
        for (let i = 0; i < p && elegant; ++i)
            for (let j = 0; j < q && elegant; ++j)
                elegant = ones_in_2x2(s,i,j) != 3;
        console.log(elegant? "YES": "NO"); } }

process.stdin.resume();
process.stdin.setEncoding('utf-8');
process.stdin.on('data', line => { cin += line; });
process.stdin.on('end', _ => {
    cin = cin.trim().split('\n').map(line => { return line.trim(); });
    main(); });

'use strict'; /*jslint node:true, browser:true, es6:true*/
const print = console.log
const E = {};
const lines = []
if (!process.env.JEST_WORKER_ID){
  const rl = require('readline').createInterface({ input: process.stdin, output: process.stdout })
  rl.on('line', line => {
      lines.push(line);
  });
  rl.on('close', main)
}
let rli = 0;
function readline() {
    return lines[rli++];
}

module.exports = E;

const ra = ()=>readline().split(' ').map(a=>+a)

const EPS = 1e-6;

E.s2a = (arr)=>arr.map(a=>a.split('').map(c=>c=='0' ? 0 : 1))

let di = [0, 0, -1, 1]
let dj = [-1, 1, 0, 0]

const dfs = E.dfs = (arr, i, j)=>{
  if (arr[i][j]!=1)
    return [];
  let n = arr.length;
  let m = arr[0].length;
  let members = [];
  let q = [[i, j]], qi = 0;
  while (qi<q.length){
    let [ti, tj] = q[qi++];
    if (arr[ti][tj]==2)
      continue;
    arr[ti][tj] = 2;
    members.push([ti, tj]);
    for (let k=0; k<4; k++){
      let I = ti+di[k];
      let J = tj+dj[k];
      if (I<0 || J<0 || I>=n || J>=m)
        continue;
      if (arr[I][J]==1)
        q.push([I, J])
    }
  }
  return members;
};

const isFine = E.isFine = (members)=>{
  let minI = members[0][0]
  let maxI = members[0][0]
  let minJ = members[0][1]
  let maxJ = members[0][1]
  for (let [i, j] of members){
    minI = Math.min(minI, i);
    minJ = Math.min(minJ, j);
    maxI = Math.max(maxI, i);
    maxJ = Math.max(maxJ, j);
  }
  return (maxI-minI+1)*(maxJ-minJ+1)==members.length;
};

const calc = (arr)=>{
  arr = E.s2a(arr);
  let n = arr.length;
  let m = arr[0].length;
  for (let i=0; i<n; i++){
    for (let j=0; j<m; j++){
      let members = dfs(arr, i, j);
      if (members.length>0 && !isFine(members))
        return false;
    }
  }
  return true
};

function main() {
    let T = +readline();
    while (T--){
      let [n, m] = ra();
      let arr = [];
      for (let i=0;i<n;i++)
        arr.push(readline());
      print(calc(arr) ? 'yes' : 'no');
    }
}

E.calc = calc;

process.stdin.resume();
process.stdin.setEncoding("utf-8");

let inputString = "";
let currentLine = 0;

process.stdin.on("data", (inputStdin) => {
  inputString += inputStdin;
});

process.stdin.on("end", (_) => {
  inputString = inputString
    .trim()
    .split("\n")
    .map((string) => {
      return string.trim();
    });

  main();
});

function readline() {
  return inputString[currentLine++];
}

function main() {
  const testCases = Number(readline());
  for (let t = 0; t < testCases; t++) {
    const [m, n] = readline().split(" ").map(Number);
    let board = [];

    for (let i = 0; i < m; i++) {
      let row = readline().split("").map(Number);
      board.push(row);
    }

    let broken = false;
    for (let i = 0; i < m; i++) {
      if (broken) break;
      for (let j = 0; j < n; j++) {
        if (board[i][j] === 1) {
          if (i !== 0) {
            if (j !== 0) {
              // check top left
              if (
                board[i][j - 1] === 1 &&
                board[i - 1][j] === 1 &&
                board[i - 1][j - 1] === 0
              ) {
                broken = true;
                break;
              }
            }

            if (j !== n - 1) {
              // check top right
              if (
                board[i][j + 1] === 1 &&
                board[i - 1][j] === 1 &&
                board[i - 1][j + 1] === 0
              ) {
                broken = true;
                break;
              }
            }
          }

          if (i !== m - 1) {
            if (j !== 0) {
              // check bottom left
              if (
                board[i][j - 1] === 1 &&
                board[i + 1][j] === 1 &&
                board[i + 1][j - 1] === 0
              ) {
                broken = true;
                break;
              }
            }

            if (j !== n - 1) {
              // check bottom right
              if (
                board[i][j + 1] === 1 &&
                board[i + 1][j] === 1 &&
                board[i + 1][j + 1] === 0
              ) {
                broken = true;
                break;
              }
            }
          }
        }
      }
    }

    if (broken) {
      console.log("NO");
    } else {
      console.log("YES");
    }
  }
}

function solve() {
   const [n, m] = readArray(Number);
   const matr = [];
   function get(x, y) {
      if (x < 0 || x >= n) {
         return '0';
      }
      if (y < 0 || y >= m) {
         return '0'
      }
      return matr[x][y];
   }
   for (let i = 0; i < n; i++) {
      matr.push(read().split(''));
   }
   for (let i = 0; i < n; i++) {
      for (let j = 0; j < m; j++) {
         if (get(i, j) === '0') {
            continue;
         }
         let i1 = i;
         while(get(i1, j) === '1') {
            i1++;
         }
         let j1 = j;
         while(get(i, j1) === '1') {
            j1++;
         }
         for (let x = i; x < i1; x++) {
            for (let y = j; y < j1; y++) {
               if (get(x, y) !== '1') {
                  write('NO');
                  return;
               }
               matr[x][y] = '0';
            }
         }
         for (let x = i; x < i1; x++) {
            if (get(x, j - 1) !== '0') {
               write('NO');
               return;
            }
            if (get(x, j1) !== '0') {
               write('NO');
               return;
            }
         }
         for (let y = j; y < j1; y++) {
            if (get(i - 1, y) !== '0') {
               write('NO');
               return;
            }
            if (get(i1, y) !== '0') {
               write('NO');
               return;
            }
         }
      }
   }
   write('YES');
}

function init() {
   try {
      var T = 1;
      T = Number(read());
      for (var t = 0; t < T; t++) {
         solve();
      }
   } catch (e) {
      write(e + e.stack);
   }
}

function modPow(a, n, mod) {
   let d = a;
   let curN = n;
   let ans = 1n;
   while(curN) {
      if (curN & 1n) {
         ans = modMul(ans, d, mod);
      }
      curN >>= 1n;
      d = modMul(d, d, mod);
   }
   return ans;
}

function modMul(a, b, mod) {
   return (a * b) % mod;
}

function modSum(a, b, mod) {
   return (a + b) % mod;
}

function modDiff(a, b, mod) {
   return (a + mod - b) % mod;
}

function isZero(num) {
   if (typeof num === 'bigint') {
      return num === 0n;
   }
   return num === 0;
}
function div(a, b) {
   if (typeof a !== typeof b) {
      throw new Error('Different types on division');
   }
   if (typeof a === 'bigint') {
      return a / b;
   }
   return Math.floor(a / b);
}

function gcd(x, y) {
   if (isZero(y)) {
      return x;
   }
   return gcd(y, x % y);
}

function lcm(x, y) {
   return div(x * y, gcd(x, y));
}

function allIndexOf(arr, value) {
   var index = arr.indexOf(value)
   var ans = [];
   while (index !== -1) {
      ans.push(index);
      index = arr.indexOf(value, index + 1);
   }
   return ans;
}

function xor(a, b, k) {
   if (k === 2) {
      return a ^ b;
   }
}

function decToBin(num) {
   let n = num;
   const ans = [];
   while(n > 0) {
      ans.push(n & 1);
      n = (n >> 1);
   }
   return ans;
}

function binToDec(binN) {
   let ans = 0;
   for (let i = binN.length - 1; i >= 0; i--) {
      ans = ((ans << 1) | binN[i]);
   }
   return ans;
}

function sortF(a, b) {
   if (a < b) {
      return -1;
   }
   if (a > b) {
      return 1;
   }
   return 0;
}

function readArray(f) {
   return read().split(' ').map(f);
}

var isNode = typeof console !== 'undefined';
var MEMORY = [];
var MEMORY_INDEX = 0;
if (isNode) {
   var fs = require('fs');

   var path = require('path');

   var inTextName;
   var outTextName;

   var needWriteFile = false;

   var writeResult = '';
   if (process.argv.length === 5) {
      inTextName = process.argv[3];
      outTextName = process.argv[4];
   }

   if (inTextName) {
      fs.readFile(path.join(__dirname, inTextName), 'utf8', (err, data) => {
         MEMORY = data.split('\r\n');
         init();
         if (needWriteFile) {
            fs.writeFile(path.join(__dirname, outTextName), writeResult, (err) => {
               err ? console.error(err) : console.log('good')
            });
         }
      });
   } else {
      MEMORY = fs.readFileSync(0, 'utf-8').split('\r\n');
      init();
   }
} else {
   init();
}

function write(value) {
   if (needWriteFile) {
      writeResult += value + '\n';
      return;
   }
   isNode ? console.log('' + value) : print('' + value);
}
function read() {
   return isNode ? MEMORY[MEMORY_INDEX++] : readline();
}
function readAll() {
   return isNode && MEMORY;
}

//const { readline, print } = require('@ip-algorithmics/codeforces-io');

function isBlack (array) {
  return !array.some(row => row.some(n => n !== 1))
}

function isWhite (array) {
  return !array.some(row => row.some(n => n !== 0))
}

function expand (array, i, j) {
  if (i >= array.length || i < 0 ||
  j < 0 || j >= array[0].length || array[i][j] !== 1) return;

  array[i][j] = 0;

  expand(array, i+1, j)
  expand(array, i-1, j)
  expand(array, i, j+1)
  expand(array, i, j-1)
}

var line = readline().split(' ').map(x => parseInt(x))
var testCases = line[0]

var n, m
while (testCases--) {
  line = readline().split(' ').map(x => parseInt(x))
  n = line[0]
  m = line[1]

  var array = new Array(n)

  for (var i = 0; i < n ; ++i) {
    array[i] = [];
    line = readline();

    for (var j = 0; j < line.length; ++j) {
      array[i].push(parseInt(line[j]));
    }
  }

  if (isBlack(array)) {
    print('YES')
    continue;
  }

  var elegantCount = 0;
  for (var i = 0; i < array.length; ++i) {
    for (var j = 0; j < array[i].length; ++j) {
      if (array[i][j] === 1) {
        expand(array, i, j);
        elegantCount++;
      }
    }
  }

  if (array.length=== 1 || array[0].length === 1) {
    print ('YES')
  } else
    print (elegantCount > 1 ? 'YES' : 'NO')
}

//const { readline, print } = require('@ip-algorithmics/codeforces-io');

function isBlack (array) {
  return !array.some(row => row.some(n => n !== 1))
}

function expand (array, i, j) {
  if (i >= array.length || i < 0 ||
  j < 0 || j >= array[0].length || array[i][j] !== 1) return;

  array[i][j] = 0;

  expand(array, i+1, j)
  expand(array, i-1, j)
  expand(array, i, j+1)
  expand(array, i, j-1)
}

var line = readline().split(' ').map(x => parseInt(x))
var testCases = line[0]

var n, m
while (testCases--) {
  line = readline().split(' ').map(x => parseInt(x))
  n = line[0]
  m = line[1]

  var array = new Array(n)

  for (var i = 0; i < n ; ++i) {
    array[i] = [];
    line = readline();

    for (var j = 0; j < line.length; ++j) {
      array[i].push(parseInt(line[j]));
    }
  }

  if (isBlack(array)) {
    print('YES')
    continue;
  }

  var elegantCount = 0;
  for (var i = 0; i < array.length; ++i) {
    for (var j = 0; j < array[i].length; ++j) {
      if (array[i][j] === 1) {
        expand(array, i, j);
        elegantCount++;
      }
    }
  }

  if (array.length=== 1 || array[0].length === 1) {
    print ('YES')
  } else
    print (elegantCount > 1 ? 'YES' : 'NO')
}

// const { readline, print } = require('@ip-algorithmics/codeforces-io');

function expand (array, i, j) {
  if (i >= array.length || i < 0 ||
  j < 0 || j >= array[0].length || array[i][j] !== 1) return;

  array[i][j] = 0;

  expand(array, i+1, j)
  expand(array, i-1, j)
  expand(array, i, j+1)
  expand(array, i, j-1)
}

var line = readline().split(' ').map(x => parseInt(x))
var testCases = line[0]

var n, m
while (testCases--) {
  line = readline().split(' ').map(x => parseInt(x))
  n = line[0]
  m = line[1]

  var array = new Array(n)

  for (var i = 0; i < n ; ++i) {
    array[i] = [];
    line = readline();

    for (var j = 0; j < line.length; ++j) {
      array[i].push(parseInt(line[j]));
    }
  }

  var elegantCount = 0;
  for (var i = 0; i < array.length; ++i) {
    for (var j = 0; j < array[i].length; ++j) {
      if (array[i][j] === 1) {
        expand(array, i, j);
        elegantCount++;
      }
    }
  }

  if (array.length=== 1 || array[0].length === 1) {
    print ('YES')
  } else
    print (elegantCount > 1 ? 'YES' : 'NO')
}

var tests = parseInt(readline());
    var n,m;
    var mat;
    var visited;

    function checkSquare(i, j) {
        visited[i][j] = 1;
        var col = j;
        while (col < m && mat[i][col]) {
            col++;
            visited[i][col] = 1;
        }
        var width = col - j;

        var row = i+1;
        while (row < n && mat[row][j]) {
            col = j;
            while (col < j + width) {
                visited[row][col] = 1;
                if (!mat[row][col]) {
                    return false;
                }
                col++;
            }
            visited[row][col] = 1;
            if (mat[row][col]) {
                return false;
            }
            row++;
        }
        if (row < n) {
            for (var c = j; c < j + width; c++) {
                visited[row][c] = 1;
                if (mat[row][c]) {
                    return false;
                }
            }
        }

        return true;
    }

    for (var t=0; t < tests; t++) {
        var nm = readline().split(' ').map(x=>parseInt(x));
        n = nm[0];
        m = nm[1];
        mat = [];
        visited = [];
        for (var i = 0; i < n; i++) {
            mat.push(readline().split('').map(x=>parseInt(x)));
            visited.push(new Array(m).fill(0));
        }

        var ans = 'YES';
        for (var i = 0; i < n; i++) {
            for (var j = 0; j < m; j++) {
                if (visited[i][j]) {
                    continue;
                }
                if (!mat[i][j]) {
                    visited[i][j] = 1;
                    continue;
                }
                if (checkSquare(i, j)) {
                    continue;
                }
                ans = 'NO';
            }
        }
        print(ans);
    }

"use strict";

process.stdin.resume();
process.stdin.setEncoding("utf-8");

let inputStr = "";
let currentLne = 0;

process.stdin.on('data' , inputStdin =>{

	 inputStr += inputStdin;
})

process.stdin.on('end' , _ =>{
    inputStr = inputStr.trim().split('\n').map( string =>{
    	return string.trim();
    })

main();
})

function readline(){
   return inputStr[currentLne++];
}

function main(){

    let t = parseInt(readline());
    let c= 0;
    while(c<t){

      let m = readline().split(" ").map(a =>parseInt(a));
      let m0 = m[0];
      let countRow = 0 ;
      let arr =[];
      while(countRow < m0){
        let row = readline().split("").map(b=>parseInt(b));
        arr.push(row);
        countRow++;
      }

    let eleg =true;
    let count =0;
    // for (var k = 0; k < arr.length; k++) {
    //     if(arr[k].indexOf(1) == -1 ){
    //          count++;
    //     }
    // }

    for (var i = 0; i < arr.length-1; i++) {

        for (var j = 0; j < arr[0].length; j++) {

          if(arr[i][j] ==1){
               if(j !==0 && arr[i+1][j] ==1 && arr[i+1][j-1] ==1 && arr[i][j-1] ==0 ){
                    eleg= false;
                    // console.log(i,j,"1st")
                    break;
               }

               if(arr[i+1][j]==1 && arr[i+1][j+1] ==1 && arr[i][j+1] == 0){
                   eleg =false;
                    // console.log(i,j,"2st")
                    break;
               }

               if(arr[i][j+1]==1 && arr[i+1][j]==1 && arr[i+1][j+1] ==0){
                eleg = false;
                 // console.log(i,j,"3st")
                 break;
               }

               if(i !== 0 && arr[i][j+1]==1 && arr[i-1][j] == 1 && arr[i-1][j-1] == 0){
                eleg =false;
                 // console.log(i,j,"4st")
                 break;
               }

               if(arr[i][j+1]==1 && arr[i+1][j+1] ==1 && arr[i+1][j]==0){
                eleg =false;
                // console.log(i,j,"5st")
                break;
               }
          }
        }

    }

    // if(count == arr.length*arr[0].length ){
    //     eleg = false;
    // }

    if(eleg){
        console.log("yes");
    }else{
        console.log("no");
    }

    c++;
    }

}

//type input.txt | node code.js

"use strict";

process.stdin.resume();
process.stdin.setEncoding("utf-8");

let inputStr = "";
let currentLne = 0;

process.stdin.on('data' , inputStdin =>{

	 inputStr += inputStdin;
})

process.stdin.on('end' , _ =>{
    inputStr = inputStr.trim().split('\n').map( string =>{
    	return string.trim();
    })

main();
})

function readline(){
   return inputStr[currentLne++];
}

function main(){

    let t = parseInt(readline());
    let c= 0;
    while(c<t){

      let m = readline().split(" ").map(a =>parseInt(a));
      let m0 = m[0];
      let countRow = 0 ;
      let arr =[];
      while(countRow < m0){
        let row = readline().split("").map(b=>parseInt(b));
        arr.push(row);
        countRow++;
      }

    let eleg =true;
    let count =0;
    // for (var k = 0; k < arr.length; k++) {
    //     if(arr[k].indexOf(1) == -1 ){
    //          count++;
    //     }
    // }

    for (var i = 0; i < arr.length-1; i++) {

        for (var j = 0; j < arr[0].length-1; j++) {

          if(arr[i][j] ==1){
               if(j !==0 && arr[i+1][j] ==1 && arr[i+1][j-1] ==1 && arr[i][j-1] ==0 ){
                    eleg= false;
                    // console.log(i,j,"1st")
                    break;
               }

               if(arr[i+1][j]==1 && arr[i+1][j+1] ==1 && arr[i][j+1] == 0){
                   eleg =false;
                    // console.log(i,j,"2st")
                    break;
               }

               if(arr[i][j+1]==1 && arr[i+1][j]==1 && arr[i+1][j+1] ==0){
                eleg = false;
                 // console.log(i,j,"3st")
                 break;
               }

               if(i !== 0 && arr[i][j+1]==1 && arr[i-1][j] == 1 && arr[i-1][j-1] == 0){
                eleg =false;
                 // console.log(i,j,"4st")
                 break;
               }

               if(arr[i][j+1]==1 && arr[i+1][j+1] ==1 && arr[i+1][j]==0){
                eleg =false;
                // console.log(i,j,"5st")
                break;
               }
          }
        }

    }

    // if(count == arr.length*arr[0].length ){
    //     eleg = false;
    // }

    if(eleg){
        console.log("yes");
    }else{
        console.log("no");
    }

    c++;
    }

}

//type input.txt | node code.js

"use strict";

process.stdin.resume();
process.stdin.setEncoding("utf-8");

let inputStr = "";
let currentLne = 0;

process.stdin.on('data' , inputStdin =>{

	 inputStr += inputStdin;
})

process.stdin.on('end' , _ =>{
    inputStr = inputStr.trim().split('\n').map( string =>{
    	return string.trim();
    })

main();
})

function readline(){
   return inputStr[currentLne++];
}

function main(){

    let t = parseInt(readline());
    let c= 0;
    while(c<t){

      let m = readline().split(" ").map(a =>parseInt(a));
      let m0 = m[0];
      let countRow = 0 ;
      let arr =[];
      while(countRow < m0){
        let row = readline().split("").map(b=>parseInt(b));
        arr.push(row);
        countRow++;
      }

    let eleg =true;
    let count =0;
    // for (var k = 0; k < arr.length; k++) {
    //     if(arr[k].indexOf(1) == -1 ){
    //          count++;
    //     }
    // }

    for (var i = 0; i < arr.length-1; i++) {

        for (var j = 0; j < arr[0].length-1; j++) {

            if(arr[i][j] ==1){
               if(j !==0 && arr[i+1][j] ==1 && arr[i+1][j-1] ==1 && arr[i][j-1] ==0 ){
                    eleg= false;
               }

               if(arr[i+1][j]==1 && arr[i+1][j+1] ==1 && arr[i][j+1] == 0){
                   eleg =false;
               }

               if(arr[i][j+1]==1 && arr[i+1][j]==1 && arr[i+1][j+1] ==0){
                eleg = false;
               }

               if(i !== 0 && arr[i][j+1]==1 && arr[i-1][j] == 1 && arr[i+1][j+1] == 0){
                eleg =false;
               }

               if(arr[i][j+1]==1 && arr[i+1][j+1] ==1 && arr[i+1][j]==0){
                eleg =false;
               }
          }
        }

    }

    // if(count == arr.length*arr[0].length ){
    //     eleg = false;
    // }

    if(eleg){
        console.log("yes");
    }else{
        console.log("no");
    }

    c++;
    }

}

//type input.txt | node code.js

"use strict";

process.stdin.resume();
process.stdin.setEncoding("utf-8");

let inputStr = "";
let currentLne = 0;

process.stdin.on('data' , inputStdin =>{

	 inputStr += inputStdin;
})

process.stdin.on('end' , _ =>{
    inputStr = inputStr.trim().split('\n').map( string =>{
    	return string.trim();
    })

main();
})

function readline(){
   return inputStr[currentLne++];
}

function main(){

    let t = parseInt(readline());
    let c= 0;
    while(c<t){

      let m = readline().split(" ").map(a =>parseInt(a));
      let m0 = m[0];
      let countRow = 0 ;
      let arr =[];
      while(countRow < m0){
        let row = readline().split("").map(b=>parseInt(b));
        arr.push(row);
        countRow++;
      }

    let eleg =true;
    let count =0;
    // for (var k = 0; k < arr.length; k++) {
    //     if(arr[k].indexOf(1) == -1 ){
    //          count++;
    //     }
    // }

    for (var i = 0; i < arr.length-1; i++) {

        for (var j = 0; j < arr[0].length-1; j++) {

            if(arr[i][j] ==1 && arr[i][j+1] ==1){
            if((arr[i+1][j] == 1 && arr[i+1][j+1] == 0 ) ||  (arr[i+1][j] == 0 && arr[i+1][j+1] == 1 ) ){
                  eleg =false;
                }

                if( j !==0){
                if(arr[i][j-1] == 0 && arr[i+1][j-1] ==1 && arr[i+1][j] ==1 ){
                   eleg =false;
                }
                }

                if(i !==0 ){
                if((arr[i-1][j+1] ==1 && arr[i-1][j]==0) || (arr[i-1][j+1] ==0 && arr[i-1][j]==1)){
                    eleg =false;
                }
            }
            }
        }

    }

    // if(count == arr.length*arr[0].length ){
    //     eleg = false;
    // }

    if(eleg){
        console.log("yes");
    }else{
        console.log("no");
    }

    c++;
    }

}

//type input.txt | node code.js

"use strict";

process.stdin.resume();
process.stdin.setEncoding("utf-8");

let inputStr = "";
let currentLne = 0;

process.stdin.on('data' , inputStdin =>{

	 inputStr += inputStdin;
})

process.stdin.on('end' , _ =>{
    inputStr = inputStr.trim().split('\n').map( string =>{
    	return string.trim();
    })

main();
})

function readline(){
   return inputStr[currentLne++];
}

function main(){

    let t = parseInt(readline());
    let c= 0;
    while(c<t){

      let m = readline().split(" ").map(a =>parseInt(a));
      let m0 = m[0];
      let countRow = 0 ;
      let arr =[];
      while(countRow < m0){
        let row = readline().split("").map(b=>parseInt(b));
        arr.push(row);
        countRow++;
      }

    let eleg =true;
    let count =0;
    // for (var k = 0; k < arr.length; k++) {
    //     if(arr[k].indexOf(1) == -1 ){
    //          count++;
    //     }
    // }

    for (var i = 0; i < arr.length-1; i++) {

        for (var j = 0; j < arr[0].length-1; j++) {

            if(arr[i][j] ==1 && arr[i][j+1] ==1){
            if((arr[i+1][j] == 1 && arr[i+1][j+1] == 0 ) ||  (arr[i+1][j] == 0 && arr[i+1][j+1] == 1 ) ){
                  eleg =false;
                }

                if( j !==0){
                if(arr[i][j-1] == 0 && arr[i+1][j-1] ==1 && arr[i+1][j] ==1 ){
                   eleg =false;
                }
                }
            }
        }

    }

    // if(count == arr.length*arr[0].length ){
    //     eleg = false;
    // }

    if(eleg){
        console.log("yes");
    }else{
        console.log("no");
    }

    c++;
    }

}

//type input.txt | node code.js

"use strict";

process.stdin.resume();
process.stdin.setEncoding("utf-8");

let inputStr = "";
let currentLne = 0;

process.stdin.on('data' , inputStdin =>{

	 inputStr += inputStdin;
})

process.stdin.on('end' , _ =>{
    inputStr = inputStr.trim().split('\n').map( string =>{
    	return string.trim();
    })

main();
})

function readline(){
   return inputStr[currentLne++];
}

function main(){

    let t = parseInt(readline());
    let c= 0;
    while(c<t){

      let m = readline().split(" ").map(a =>parseInt(a));
      let m0 = m[0];
      let countRow = 0 ;
      let arr =[];
      while(countRow < m0){
        let row = readline().split("").map(b=>parseInt(b));
        arr.push(row);
        countRow++;
      }

    let eleg =true;
    let count =0;
    // for (var k = 0; k < arr.length; k++) {
    //     if(arr[k].indexOf(1) == -1 ){
    //          count++;
    //     }
    // }

    for (var i = 0; i < arr.length-1; i++) {

        for (var j = 0; j < arr[0].length-1; j++) {

            if(arr[i][j] ==1 && arr[i][j+1] ==1){
                if(arr[i+1][j] == 1 && arr[i+1][j+1] == 0){
                  eleg =false;
                }

                if( j !==0){
                  if(arr[i][j-1] == 0 && arr[i+1][j-1] ==1 ){
                   eleg =false;
                }
                }
            }
        }

    }

    // if(count == arr.length*arr[0].length ){
    //     eleg = false;
    // }

    if(eleg){
        console.log("yes");
    }else{
        console.log("no");
    }

    c++;
    }

}

//type input.txt | node code.js

"use strict";

process.stdin.resume();
process.stdin.setEncoding("utf-8");

let inputStr = "";
let currentLne = 0;

process.stdin.on('data' , inputStdin =>{

	 inputStr += inputStdin;
})

process.stdin.on('end' , _ =>{
    inputStr = inputStr.trim().split('\n').map( string =>{
    	return string.trim();
    })

main();
})

function readline(){
   return inputStr[currentLne++];
}

function main(){

    let t = parseInt(readline());
    let c= 0;
    while(c<t){

      let m = readline().split(" ").map(a =>parseInt(a));
      let m0 = m[0];
      let countRow = 0 ;
      let arr =[];
      while(countRow < m0){
        let row = readline().split("").map(b=>parseInt(b));
        arr.push(row);
        countRow++;
      }

    let eleg =true;
    let count =0;
    for (var k = 0; k < arr.length; k++) {
        if(arr[k].indexOf(1) == -1 ){
             count++;
        }
    }

    for (var i = 0; i < arr.length-1; i++) {

        for (var j = 0; j < arr[0].length-1; j++) {

            if(arr[i][j] ==1 && arr[i][j+1] ==1){
                if(arr[i+1][j] == 1 && arr[i+1][j+1] == 0){
                  eleg =false;
                }

                if( j !==0){
                  if(arr[i][j-1] == 0 && arr[i+1][j-1] ==1 ){
                   eleg =false;
                }
                }
            }
        }

    }

    if(count == arr.length*arr[0].length ){
        eleg = false;
    }

    if(eleg){
        console.log("yes");
    }else{
        console.log("no");
    }

    c++;
    }

}

//type input.txt | node code.js

"use strict";

process.stdin.resume();
process.stdin.setEncoding("utf-8");

let inputStr = "";
let currentLne = 0;

process.stdin.on('data' , inputStdin =>{

	 inputStr += inputStdin;
})

process.stdin.on('end' , _ =>{
    inputStr = inputStr.trim().split('\n').map( string =>{
    	return string.trim();
    })

main();
})

function readline(){
   return inputStr[currentLne++];
}

function main(){

    let t = parseInt(readline());
    let c= 0;
    while(c<t){

      let m = readline().split(" ").map(a =>parseInt(a));
      let m0 = m[0];
      let countRow = 0 ;
      let arr =[];
      while(countRow < m0){
        let row = readline().split("").map(b=>parseInt(b));
        arr.push(row);
        countRow++;
      }

    let eleg =true;
    let count =0;
    for (var k = 0; k < arr.length; k++) {
        if(arr[k].indexOf(1) == -1 ){
             count++;
        }
    }

    for (var i = 0; i < arr.length-1; i++) {

        for (var j = 0; j < arr[0].length-1; j++) {

            if(arr[i][j] ==1 && arr[i][j+1] ==1){
                if(arr[i+1][j] == 1 && arr[i+1][j+1] == 0){
                  eleg =false;
                }

                if( j !==0){
                  if(arr[i][j-1] == 0 && arr[i+1][j-1] ==1 ){
                   eleg =false;
                }
                }
            }
        }

    }

    if(count == arr.length){
        eleg = false;
    }

    if(eleg){
        console.log("yes");
    }else{
        console.log("no");
    }

    c++;
    }

}

//type input.txt | node code.js

"use strict";

process.stdin.resume();
process.stdin.setEncoding("utf-8");

let inputStr = "";
let currentLne = 0;

process.stdin.on('data' , inputStdin =>{

	 inputStr += inputStdin;
})

process.stdin.on('end' , _ =>{
    inputStr = inputStr.trim().split('\n').map( string =>{
    	return string.trim();
    })

main();
})

function readline(){
   return inputStr[currentLne++];
}

function main(){

    let t = parseInt(readline());
    let c= 0;
    while(c<t){

      let m = readline().split(" ").map(a =>parseInt(a));
      let m0 = m[0];
      let countRow = 0 ;
      let arr =[];
      while(countRow < m0){
        let row = readline().split("").map(b=>parseInt(b));
        arr.push(row);
        countRow++;
      }

    let eleg =true;
    let count =0;
    for (var i = 0; i < arr.length-1; i++) {

        for (var j = 0; j < arr[0].length-1; j++) {

           if(arr[i][j] == 0 && arr[i][j+1]==0 ){
              if(arr[i+1][j] == 0 && arr[i+1][j+1] == 0){
                  count++;
                }
           }

            if(arr[i][j] ==1 && arr[i][j+1] ==1){
                if(arr[i+1][j] == 1 && arr[i+1][j+1] == 0){
                  eleg =false;
                }

                if( j !==0){
                  if(arr[i][j-1] == 0 && arr[i+1][j-1] ==1 ){
                   eleg =false;
                }
                }
            }
        }

    }
    let tot = Math.floor(((arr.length * arr[0].length)-1)/2);
    //console.log(count,tot)
    if(count == tot){
    eleg =false;
    }

    if(eleg){
        console.log("yes");
    }else{
        console.log("no");
    }

    c++;
    }

}

//type input.txt | node code.js

"use strict";

process.stdin.resume();
process.stdin.setEncoding("utf-8");

let inputStr = "";
let currentLne = 0;

process.stdin.on('data' , inputStdin =>{

	 inputStr += inputStdin;
})

process.stdin.on('end' , _ =>{
    inputStr = inputStr.trim().split('\n').map( string =>{
    	return string.trim();
    })

main();
})

function readline(){
   return inputStr[currentLne++];
}

function main(){

    let t = parseInt(readline());
    let c= 0;
    while(c<t){

      let m = readline().split(" ").map(a =>parseInt(a));
      let m0 = m[0];
      let countRow = 0 ;
      let arr =[];
      while(countRow < m0){
        let row = readline().split("").map(b=>parseInt(b));
        arr.push(row);
        countRow++;
      }

    let eleg =true;
    for (var i = 0; i < arr.length-1; i++) {

        for (var j = 0; j < arr[0].length; j++) {
            if(arr[i][j] ==1 && arr[i][j+1] ==1){
                if(arr[i+1][j] == 1 && arr[i+1][j+1] == 0){
                  eleg =false;
                }

                if( j !==0){
                  if(arr[i][j-1] == 0 && arr[i+1][j-1] ==1 ){
                   eleg =false;
                }
                }
            }
        }

    }
    if(eleg){
        console.log("yes");
    }else{
        console.log("no");
    }

    c++;
    }

}

//type input.txt | node code.js

"use strict";

process.stdin.resume();
process.stdin.setEncoding("utf-8");

let inputStr = "";
let currentLne = 0;

process.stdin.on('data' , inputStdin =>{

	 inputStr += inputStdin;
})

process.stdin.on('end' , _ =>{
    inputStr = inputStr.trim().split('\n').map( string =>{
    	return string.trim();
    })

main();
})

function readline(){
   return inputStr[currentLne++];
}

function main(){

    let t = parseInt(readline());
    let c= 0;
    while(c<t){

      let m = readline().split(" ").map(a =>parseInt(a));
      let m0 = m[0];
      let countRow = 0 ;
      let arr =[];
      while(countRow < m0){
        let row = readline().split("").map(b=>parseInt(b));
        arr.push(row);
        countRow++;
      }

    let eleg =true;
    for (var i = 0; i < arr.length-1; i++) {

        for (var j = 0; j < arr[0].length; j++) {
            if(arr[i][j] ==1 && arr[i][j+1] ==1){
                if(arr[i+1][j] == 1 && arr[i+1][j+1] == 0){
                  eleg =false;
                }
            }
        }

    }
    if(eleg){
        console.log("yes");
    }else{
        console.log("no");
    }

    c++;
    }

}

"use strict";
const fs = require("fs");
//let filename = "0.txt";
let filename = 0;
const input = fs.readFileSync(filename, "utf8").trim().split("\n");
let line = 0;
function readline() {
  return input[line++];
}
const solve = () => {
  let t = parseInt(readline());
  while (t--) {
    let [n, m] = readline()
      .trim()
      .split(" ")
      .map((cur) => parseInt(cur));
    let arr = [];
    for (let i = 0; i < n; i++)
      arr.push(
        readline()
          .trim()
          .split("")
          .map((cur) => parseInt(cur))
      );
    if (n === 1) {
      let ans = false;
      for (let i = 0; i < m; i++) {
        if (arr[0][i] === 1) {
          ans = true;
          break;
        }
      }
      console.log(`${ans === true ? "YES" : "NO"}`);
      continue;
    }
    if (m === 1) {
      let ans = false;
      for (let i = 0; i < n; i++) {
        if (arr[i][0] === 1) {
          ans = true;
          break;
        }
      }
      console.log(`${ans === true ? "YES" : "NO"}`);
      continue;
    }
    const ne = (r, c, n, m) => {
      let arr1 = [];
      if (r !== n - 1 && arr[r + 1][c] === 1) arr1.push([r + 1, c]);
      if (r !== 0 && arr[r - 1][c] === 1) arr1.push([r - 1, c]);
      if (c !== 0 && arr[r][c - 1] === 1) arr1.push([r, c - 1]);
      if (c !== m - 1 && arr[r][c + 1] === 1) arr1.push([r, c + 1]);
      return arr1;
    };
    const dfs = (row, col, m, n) => {
      let obj = {};
      const helper = (row, col) => {
        arr[row][col] = 0;
        obj[[row, col]] = 1;
        let neigh = ne(row, col, n, m);
        for (let i of neigh) if (!obj[(i[0], i[1])]) helper(i[0], i[1]);
      };
      helper(row, col);
    };
    let ist = false,
      ans = false;
    for (let i = 0; i < n; i++) {
      for (let j = 0; j < m; j++) {
        if (arr[i][j] === 1) {
          if (ist) {
            ans = true;
            break;
          }
          dfs(i, j, m, n);
          ist = true;
        }
      }
      if (ans) break;
    }
    if (ans && ist) console.log("YES");
    else if (ist) console.log("NO");
    else console.log("YES");
  }
};
solve();

"use strict";
const fs = require("fs");
//let filename = "0.txt";
let filename = 0;
const input = fs.readFileSync(filename, "utf8").trim().split("\n");
let line = 0;
function readline() {
  return input[line++];
}
const solve = () => {
  let t = parseInt(readline());
  while (t--) {
    let [n, m] = readline()
      .trim()
      .split(" ")
      .map((cur) => parseInt(cur));
    let arr = [];
    for (let i = 0; i < n; i++)
      arr.push(
        readline()
          .trim()
          .split("")
          .map((cur) => parseInt(cur))
      );
    if (n === 1) {
      let ans = false;
      for (let i = 0; i < m; i++) {
        if (arr[0][i] === 1) {
          ans = true;
          break;
        }
      }
      console.log(`${ans === true ? "YES" : "NO"}`);
      continue;
    }
    const ne = (r, c, n, m) => {
      let arr1 = [];
      if (r !== n - 1 && arr[r + 1][c] === 1) arr1.push([r + 1, c]);
      if (r !== 0 && arr[r - 1][c] === 1) arr1.push([r - 1, c]);
      if (c !== 0 && arr[r][c - 1] === 1) arr1.push([r, c - 1]);
      if (c !== m - 1 && arr[r][c + 1] === 1) arr1.push([r, c + 1]);
      return arr1;
    };
    const dfs = (row, col, m, n) => {
      let obj = {};
      const helper = (row, col) => {
        arr[row][col] = 0;
        obj[[row, col]] = 1;
        let neigh = ne(row, col, n, m);
        for (let i of neigh) if (!obj[(i[0], i[1])]) helper(i[0], i[1]);
      };
      helper(row, col);
    };
    let ist = false,
      ans = false;
    for (let i = 0; i < n; i++) {
      for (let j = 0; j < m; j++) {
        if (arr[i][j] === 1) {
          if (ist) {
            ans = true;
            break;
          }
          dfs(i, j, m, n);
          ist = true;
        }
      }
      if (ans) break;
    }
    console.log(`${ans === true ? "YES" : "NO"}`);
  }
};
solve();

'use strict';

var readline = require('readline');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var readline__default = /*#__PURE__*/_interopDefaultLegacy(readline);

function solve(n, m, s) {
  const sum = Array.from({
    length: n
  }, () => new Array(m).fill(0));
  for (let i = 0; i < n; i++) {
    for (let j = 0; j < m; j++) {
      var _sum$j, _sum, _sum$i, _sum2, _sum3;
      sum[i][j] = ((_sum$j = (_sum = sum[i - 1]) === null || _sum === void 0 ? void 0 : _sum[j]) !== null && _sum$j !== void 0 ? _sum$j : 0) + ((_sum$i = sum[i][j - 1]) !== null && _sum$i !== void 0 ? _sum$i : 0) - ((_sum2 = (_sum3 = sum[i - 1]) === null || _sum3 === void 0 ? void 0 : _sum3[j - 1]) !== null && _sum2 !== void 0 ? _sum2 : 0) + Number(s[i][j]);
    }
  }
  const p = [...new Array(n * m).keys()];
  const find = i => {
    if (p[i] !== i) p[i] = find(p[p[i]]);
    return p[i];
  };
  const union = (i, j) => {
    const ri = find(i),
          rj = find(j);
    if (ri !== rj) p[rj] = ri;
  };
  const dirs = [[0, 1], [1, 0]];
  for (let i = 0; i < n; i++) {
    for (let j = 0; j < m; j++) {
      const a = i * m + j;
      if (s[i][j] === '0') {
        p[a] = -1;
        continue;
      }
      for (let [di, dj] of dirs) {
        const ni = di + i,
              nj = dj + j,
              b = ni * m + nj;
        if (ni < 0 || ni >= n || nj < 0 || nj >= m || s[ni][nj] === '0') continue;
        union(a, b);
      }
    }
  }
  const scc = new Map();
  for (let [i, num] of p.entries()) {
    if (num === -1) continue;
    const root = find(num);
    if (!scc.has(root)) scc.set(root, []);
    scc.get(root).push(i);
  }
  for (let [, points] of scc) {
    var _sum$rb, _sum$rr, _sum4, _sum5, _sum6;
    let ll = Infinity,
        lt = Infinity,
        rr = -Infinity,
        rb = -Infinity;
    for (let num of points) {
      const i = Math.floor(num / m),
            j = num % m;
      ll = Math.min(ll, i);
      lt = Math.min(lt, j);
      rr = Math.max(rr, j);
      rb = Math.max(rb, i);
    }
    if (sum[rb][rr] - ((_sum$rb = sum[rb][ll - 1]) !== null && _sum$rb !== void 0 ? _sum$rb : 0) - ((_sum$rr = (_sum4 = sum[lt - 1]) === null || _sum4 === void 0 ? void 0 : _sum4[rr]) !== null && _sum$rr !== void 0 ? _sum$rr : 0) + ((_sum5 = (_sum6 = sum[lt - 1]) === null || _sum6 === void 0 ? void 0 : _sum6[ll - 1]) !== null && _sum5 !== void 0 ? _sum5 : 0) !== (rr - ll + 1) * (rb - lt + 1)) return 'NO';
  }
  return 'YES';
}

async function main(read) {
  let t = Number(await read());
  let res = [];
  while (t--) {
    const [n, m] = (await read()).split(' ').map(Number);
    const a = [];
    let tmp = n;
    while (tmp--) {
      a.push(await read());
    }
    res.push(solve(n, m, a));
  }
  console.log(res.join('\n'));
}

const rl = readline__default["default"].createInterface({
  input: process.stdin,
  output: process.stdout
});
void async function () {
  const inputs = [];
  let i = 0;
  for await (const line of rl) {
    inputs.push(line.trim());
  }
  function read() {
    return inputs[i++];
  }
  await main(read);
}();


function trim(s) {
	return s.replace(/^\s+|\s+$/gm, '');
}

function tokenize(s) {
	return trim(s).split(/\s+/);
}

function tokenizeIntegers(s) {
	var tokens = tokenize(s);
	for (var i = 0; i < tokens.length; i += 1) {
		tokens[i] = parseInt(tokens[i], 10);
	};
	return tokens;
}

function main() {
	var data = tokenizeIntegers(readline()),
		a = data[0], b = data[1],
		a2 = a*a;
	for (var y = 1; y*y < a2; ++y) {
		var x = Math.sqrt(a2 - y*y);
		if (x == Math.floor(x)) {
			if ((b*y)%a == 0 && (b*x)%a == 0 && x != b*y/a) {
				print('YES');
				print(0, 0);
				print(x, y);
				print(b*y/a, b*-x/a);
				return;
			}
		}
	}
	print('NO');
}

main();

function trim(s) {
	return s.replace(/^\s+|\s+$/gm, '');
}

function tokenize(s) {
	return trim(s).split(/\s+/);
}

function tokenizeIntegers(s) {
	var tokens = tokenize(s);
	for (var i = 0; i < tokens.length; i += 1) {
		tokens[i] = parseInt(tokens[i], 10);
	};
	return tokens;
}

function main() {
	var data = tokenizeIntegers(readline()),
		a = data[0], b = data[1],
		a2 = a*a;
	for (var y = 1; 2*y*y <= a2; ++y) {
		var x = Math.sqrt(a2 - y*y);
		if (x == Math.floor(x)) {
			if ((b*y)%a == 0 && (b*x)%a == 0 && x != b*y/a) {
				print('YES');
				print(0, 0);
				print(x, y);
				print(b*y/a, b*-x/a);
				return;
			}
		}
	}
	print('NO');
}

main();


'use strict';

function solve(n, a) {
  for (let len = n - 1; len > 0; len--) {
    const b = [];
    for (let i = 0; i < a.length - 1; i++) {
      const num = a[i + 1] - a[i];
      if (num) b.push(num);
    }
    if (b.length < len) b.push(0);
    a = b.sort((a, b) => a - b);
  }
  return a[0];
}

async function main(read) {
  try {
    let t = Number(await read());
    let res = new Array(t);
    for (let i = 0; i < t; i++) {
      const n = Number(await read());
      const a = (await read()).split(' ').map(Number);
      res[i] = solve(n, a);
    }
    return res.join('\n');
  } catch (error) {
    console.log(error);
  }
}

let inputs,
    str = '';
function read() {
  return inputs.next().value.trim();
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', input => str += input);
process.stdin.on('end', async () => {
  inputs = str.split('\n').values();
  const output = await main(read);
  process.stdout.write(output);
});

const main = () => {

	var t = readInt();
	var allans = [];
	for (var zz = 0; zz < t; zz++) {
		var n = readInt();
		var a = readIntArr();
		var b;

		// there won't be too many moves before most/everyone becomes 0
		var ans = 0;
		var zerocnt = 0;
		while (a.length > 1) {
			a.sort((x, y) => (y - x)); // sort descending
			while (a.length > 1 && a[a.length - 1] === 0) {
				zerocnt++;
				a.pop();
			}
			if (zerocnt > 0 && a.length > 0 && a[a.length - 1] !== 0) {
				a.push(0);
				zerocnt--;
			}
			b = [];
			for (var i  = 1; i < a.length; i++)
				b.push(a[i - 1] - a[i]);
			a = b;
		}
		if (a.length === 1)
			ans = a[0];
		allans.push(ans);
	}
	multiLineArrayPrint(allans);

	return;
}

const readInt = () => parseInt(readline());
const readString = () => readline();
const readIntArr = () => readline().split(" ").map(zzzz => parseInt(zzzz));
const parseLong = (string) => { // javascript's largest long is < (1<<62)
	var n = string.length;
	var res = 0;
	for (var i = 0; i < n; i++) {
		res *= 10;
		res += parseInt(string[i]);
	}
	return res;
}
const readLong = () => parseLong(readLong());
const readLongArr = () => readline().split(" ").map(zzzz => parseLong(zzzz));
const oneLineArrayPrint = (arr) => print(arr.map(zzzz => zzzz.toString()).join(' '));
const multiLineArrayPrint = (arr) => print(arr.map(zzzz => zzzz.toString()).join('\n'));
const multiLineArrayOfArraysPrint = (arr) => print(arr.map(yyyy => yyyy.map(zzzz => zzzz.toString()).join(' ')).join('\n'));
const makeArr = (defaultVal, dimensionsArr) => {
	var n = dimensionsArr.length;
	if (n === 1) return Array(dimensionsArr[0]).fill(defaultVal);
	else {
		var temp = [];
		for (var i = 0; i < dimensionsArr[0]; i++)
			temp.push(makeArr(defaultVal, dimensionsArr.slice(1)));
		return temp;
	}
}

for (_ = 0; _ < 1; _++) {
	main();
}

// newline for testing
 //newline for testing
// codeforces js cannot use "let" keyword. use "var" instead
// codeforces js cannot destructure arrays


const lines = [];
const rl = require('readline').createInterface({ input: process.stdin, output: process.stdout });
rl.on('line', line => lines.push(line));
rl.on('close', main);
let rli = 0;
function readline() {
    return lines[rli++];
}

const ra = ()=>readline().split(' ').map(a=>+a);

function div(val, by){
    return (val - val % by) / by;
}

const cnk = (n, k) => {
    let res = 1;
    for (let i = 1; i <= k; i++)
        res = res * (n - k + i) / i;
    return Math.round(res + 0.01);
}

const sumab = (a, b) => {
    let x = (b - a + 1);
    let y = a + b;
    if (x % 2 === 0) {
        x = Math.round(x / 2);
    } else {
        y = Math.round(y / 2);
    }
    return BigInt(x) * BigInt(y);
}

const calc = (a)=>{
    let n = a.length;
    if (n === 1) return 1;

    let result = 1;
    let pos1 = 0;
    let pos0 = n - 1;

    for (let i = 0; i < n; i++) {
        if (a[i] === '0') {
            pos0 = i;
            break;
        }
    }

    for (let i = n - 1; i >= 0; i--) {
        if (a[i] === '1') {
            pos1 = i;
            break;
        }
    }

    if (pos0 < pos1) return n;
    return pos0 - pos1 + 1;
};

function main() {
    let T = +readline();
    for (let t = 1; t <= T; t++){
        // let n = +readline();
        // let a = ra();
        let a = readline().split('');
        console.log(`${calc(a)}`);
    }
}

'use strict';

var readline = require('readline');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var readline__default = /*#__PURE__*/_interopDefaultLegacy(readline);

function solve(s) {
  if (s.length === 1) {
    console.log(1);
    return;
  }
  const n = s.length;
  let l = 0,
      r = n - 1;
  for (let i = 0; i < n; i++) {
    if (s[i] === '1') {
      l = i;
    }
  }
  for (let i = n - 1; i >= 0; i--) {
    if (s[i] === '0') {
      r = i;
    }
  }
  if (r < l) {
    console.log(1);
    return;
  }
  console.log(r - l + 1);
}

async function main(read) {
  let t = Number(await read());
  while (t--) {
    const s = await read();
    solve(s);
  }
}

const rl = readline__default["default"].createInterface({
  input: process.stdin,
  output: process.stdout
});
void async function () {
  const inputs = [];
  let i = 0;
  for await (const line of rl) {
    inputs.push(line.trim());
  }
  function read() {
    return inputs[i++];
  }
  await main(read);
}();

const rdline = require('readline');
const rl = rdline.createInterface({
    input: process.stdin
});

let lines = [];
rl.on('line', (line) => {
    lines.push(line);
}).on('close', () => {
    // input in lines
    solve();
});

let line = 0;
const readline = () => lines[line++];
solve = () => {
    const t = parseInt(readline());
    for (let i = 0; i < t; i++) {
        const s = readline();
        const l = s.lastIndexOf('1');
        const f = s.indexOf('0');
        var n = s.length;
        if (n === 1) {
            console.log(1);
        } else  if (s[0] === '0') {
            console.log(1);
        } else
        if (s[n - 1] === '1') {
            console.log(1);
        } else
        if (l === -1 && f === -1) {
            console.log(n);
        } else
        if (l === -1) {
            console.log(f + 1);
        } else
        if (f === -1) {
            console.log(n - l);
        } else {
          console.log(f - l + 1);
        }

    }
}

const solve = (str)=>{
     let yes = 0;
     for(let i=0;i<str.length;i++){
          if(str[i]==="1") yes = i;
          else if(str[i]==="0"){
               return i - yes + 1;
          }
     }
     return yes===0 ? str.length : str.length - yes;
}
function main() {
  const fs = require("fs");
  const input = fs.readFileSync(0, "utf8").trim().split("\n");
  let line = 0;
  function readline() {
    return input[line++];
  }
  let t = parseInt(readline());
  for (let i = 0; i < t; i++) {
       console.log(solve(readline().trim()));
  }
}
main();

let readline = require("readline");
let rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false,
});

let lines = 0;
rl.on("line", function (line) {
  if (lines !== 0) {
    let input = line.split("");
    if (input.length === 1) {
      return console.log(1);
    }
    if (input[input.length - 1] === "1") {
      return console.log(1);
    }
    if (input[0] === "0") {
      return console.log(1);
    }
    const indexOf0 = input.indexOf("0");
    const indexOf1 = input.lastIndexOf("1");

    if (indexOf0 === -1 && indexOf1 === -1) {
      return console.log(input.length);
    }
    if (indexOf1 === -1) {
      return console.log(indexOf0 + 1);
    }
    if (indexOf0 === -1) {
      return console.log(input.length - indexOf1);
    }
    console.log(indexOf0 - indexOf1 + 1);
  }
  lines++;
});

const readline = require('readline')

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
})
const lines = []
rl.on('line', (input) => {
    lines.push(input);
})
rl.on('close', () => {
// (function() {
    // const lines = require('fs').readFileSync('test.in', 'utf8').split('\n')
    let l = 0;
    let t = +lines[l++]
    const output = []
    for (let i = 0; i < t; i++) {
        const str = lines[l++]
        output[i] = solve(str)
    }
    console.log(output.join('\n'))
// })()
})

function solve(str) {
    // no 1 after, no 0 before
    const one = []
    const zero = []
    for (let i = 0; i < str.length; i++) {
        let now = 0
        if (str[i] === '0') now = 1
        zero[i] = i ? zero[i - 1] + now : now
    }
    for (let i = str.length - 1; i >= 0; i--) {
        let now = 0
        if (str[i] === '1') now = 1
        one[i] = i < str.length - 1 ? one[i + 1] + now : now
    }
    let ans = 0
    for (let i = 0; i < str.length; i++) {
        if (!zero[i - 1] && !one[i + 1]) ans++
    }
    return ans
}

'use strict';
// HEADER:
const print = console.log;
let IS_MULTITEST, testN;
const pcalc = testN=>{
  let res = calc(testN);
  if (Array.isArray(res)){
    pa(res);
  } else if (Number.isFinite(res) || typeof res=='string'){
    print(res);
  }
};

const main = ()=>{
  if (IS_MULTITEST){
    let T = +readline()
    for (testN=1; testN<=T; testN++)
      pcalc(T);
  } else
   pcalc();
};

const lines = [];
const rl = require('readline').createInterface({
   input: process.stdin, output: process.stdout});
rl.on('line', line=>{
  lines.push(line);
});
rl.on('close', main);
let rli = 0;
const readline = ()=>lines[rli++];
const L = process.env.DEBUG ? console.log : ()=>{};
const LT = obj=>L(`Test #${testN}`, obj);

const ra = ()=>readline().split(' ').map(a=>+a)
const pa = arr=>print(arr.join(' '));

// SOLUTION:
IS_MULTITEST = 1;

const calc = ()=>{
  // READING:
  let s = readline();
  let n = s.length;
  LT({n, s});
  // PROCESSING:
  let ones = [];
  let zeros = [];
  for (let i=0; i<n; i++){
    if (s[i]=='1'){
      ones.push(i);
    }
    if (s[i]=='0'){
      zeros.push(i);
    }
  }
  let from = ones.length>=1 ? ones[ones.length-1] : 0;
  let to = zeros.length>=1 ? zeros[0] : n-1;
  return to-from+1;
};

'use strict';

var readline = require('readline');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var readline__default = /*#__PURE__*/_interopDefaultLegacy(readline);

function solve(s) {
  if (s.length === 1) {
    console.log(1);
    return;
  }
  const n = s.length;
  let cache = new Map();
  const check = i => {
    if (cache.has(i)) return cache.get(i);
    if (i < 0) {
      cache.set(i, true);
    } else if (s[i] === '1') {
      cache.set(i, check(i - 1));
    } else if (s[i] === '?') {
      cache.set(i, check(i - 1));
    } else {
      cache.set(i, false);
    }
    return cache.get(i);
  };
  let res = new Set();
  for (let i = n - 1; i >= 0; i--) {
    if (check(i - 1)) res.add(i);
    if (s[i] === '1') break;
  }
  console.log(res.size);
}

async function main(read) {
  let t = Number(await read());
  while (t--) {
    const s = await read();
    solve(s);
  }
}

const rl = readline__default["default"].createInterface({
  input: process.stdin,
  output: process.stdout
});
void async function () {
  const inputs = [];
  let i = 0;
  for await (const line of rl) {
    inputs.push(line.trim());
  }
  function read() {
    return inputs[i++];
  }
  await main(read);
}();

'use strict';

var readline = require('readline');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var readline__default = /*#__PURE__*/_interopDefaultLegacy(readline);

function solve(s) {
  if (s.length === 1) {
    console.log(1);
    return;
  }
  const n = s.length;
  let cache = new Map();
  const check = i => {
    if (cache.has(i)) return cache.get(i);
    if (i < 0) {
      cache.set(i, true);
    } else if (s[i] === '1') {
      cache.set(i, true);
    } else if (s[i] === '?') {
      cache.set(i, check(i - 1));
    } else {
      cache.set(i, false);
    }
    return cache.get(i);
  };
  let res = new Set();
  for (let i = n - 1; i >= 0; i--) {
    if (check(i - 1)) res.add(i);
    if (s[i] === '1') break;
  }
  console.log(res.size);
}

async function main(read) {
  let t = Number(await read());
  while (t--) {
    const s = await read();
    solve(s);
  }
}

const rl = readline__default["default"].createInterface({
  input: process.stdin,
  output: process.stdout
});
void async function () {
  const inputs = [];
  let i = 0;
  for await (const line of rl) {
    inputs.push(line.trim());
  }
  function read() {
    return inputs[i++];
  }
  await main(read);
}();

'use strict';

var readline = require('readline');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var readline__default = /*#__PURE__*/_interopDefaultLegacy(readline);

function solve(s) {
  console.log(s);
  if (s.length === 1) {
    console.log(1);
    return;
  }
  const n = s.length;
  let cache = new Map();
  const check = i => {
    if (cache.has(i)) return cache.get(i);
    if (i < 0) {
      cache.set(i, true);
    } else if (s[i] === '1') {
      cache.set(i, true);
    } else if (s[i] === '?') {
      cache.set(i, check(i - 1));
    } else {
      cache.set(i, false);
    }
    return cache.get(i);
  };
  let res = new Set();
  for (let i = n - 1; i >= 0; i--) {
    if (check(i - 1)) {
      res.add(i);
    }
    if (s[i] === '1') {
      break;
    }
  }
  console.log(res.size);
}

async function main(read) {
  let t = Number(await read());
  while (t--) {
    const s = await read();
    solve(s);
  }
}

const rl = readline__default["default"].createInterface({
  input: process.stdin,
  output: process.stdout
});
void async function () {
  const inputs = [];
  let i = 0;
  for await (const line of rl) {
    inputs.push(line.trim());
  }
  function read() {
    return inputs[i++];
  }
  await main(read);
}();


const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
    terminal: false
});
let n, m;
const cells = [];
let numberOfGoodCell = 0;

// listCount = new Array(1001).fill(0);
function solve() {

}

function check() {
    for (let i = 0; i < cells.length; i++) {
        for (let j = 0; j < cells[i].length; j++) {
            if ((cells[i][j] !== '.' || cells[i][j] !== '-') && !checkCellAdjustable(i, j, cells[i][j])) {
                return false;
            }
        }
    }
    return true;
}

function checkCellAdjustable(i, j, char) {
    return !(cells[i][j + 1] === char || cells[i][j - 1] === char || cells[i + 1][j] === char || cells[i - 1][j] === char);
}

rl.on('line', (line) => {
    if (!n && !m) {
        [n, m] = line.split(' ').map(Number);
        return;
    }
    const list = line.split('');
    cells.push(list);
    if (cells.length === n) {
        let charI = 'B';
        for (let i = 0; i < cells.length; i++) {
            let charJ = charI;
            let output = '';
            for (let j = 0; j < cells[i].length; j++) {
                if (cells[i][j] === '.') {
                    cells[i][j] = charJ;
                }
                if (charJ === 'B') {
                    charJ = 'W';
                } else {
                    charJ = 'B';
                }
                output += cells[i][j];
            }
            console.log(output);

            if (charI === 'B') {
                charI = 'W';
            } else {
                charI = 'B';
            }
        }
    }
});

// Generated by CoffeeScript 1.10.0
(function() {
  var a, i, j, l, line, m, n, o, ref, ref1, ref2, results;

  ref = readline().split(' '), n = ref[0], m = ref[1];

  a = [];

  for (i = j = 0, ref1 = n; 0 <= ref1 ? j < ref1 : j > ref1; i = 0 <= ref1 ? ++j : --j) {
    a.push(readline());
  }

  for (i = l = 0, ref2 = n; 0 <= ref2 ? l < ref2 : l > ref2; i = 0 <= ref2 ? ++l : --l) {
    line = (function() {
      results = [];
      for (var o = 0; 0 <= m ? o < m : o > m; 0 <= m ? o++ : o--){ results.push(o); }
      return results;
    }).apply(this).map(function(k) {
      if (a[i][k] === '-') {
        return '-';
      } else if ((i + k) % 2 === 0) {
        return 'B';
      } else {
        return 'W';
      }
    });
    print(line.join(''));
  }

}).call(this);

// Generated by CoffeeScript 1.10.0
(function() {
  var j, m, n, ref, results;

  print(((ref = readline().split(' '), n = ref[0], m = ref[1], ref), (function() {
    results = [];
    for (var j = 0; 0 <= n ? j < n : j > n; 0 <= n ? j++ : j--){ results.push(j); }
    return results;
  }).apply(this).map(function(i) {
    var a, j, results;
    a = readline();
    return (function() {
      results = [];
      for (var j = 0; 0 <= m ? j < m : j > m; 0 <= m ? j++ : j--){ results.push(j); }
      return results;
    }).apply(this).map(function(k) {
      if (a[k] === '-') {
        return '-';
      } else if ((i + k) % 2 === 0) {
        return 'B';
      } else {
        return 'W';
      }
    });
  })).join('\n').split(',').join(''));

}).call(this);

function trim(s) {
	return s.replace(/^\s+|\s+$/gm, '');
}

function tokenize(s) {
	return trim(s).split(/\s+/);
}

function tokenizeIntegers(s) {
	var tokens = tokenize(s);
	for (var i = 0; i < tokens.length; i += 1) {
		tokens[i] = parseInt(tokens[i], 10);
	};
	return tokens;
}

function main() {
	var data = tokenizeIntegers(readline()),
		rowNum = data[0], colNum = data[1],
		rows = [];
	for (var r = 0; r < rowNum; ++r) {
		var line = trim(readline()),
			chars = new Array(colNum);
		for (var c = 0; c < colNum; ++c) {
			if (line.charAt(c) == '.') {
				chars.push((r+c)%2 == 0 ? 'W' : 'B');
			} else {
				chars.push('-');
			}
		}
		rows.push(chars.join(''));
	}
	print(rows.join('\n'));
}

main();

{
    var num = readline(), arr = [];
    num = num.split(' ');
    for (var i = 1; i <= +num[0]; i++)
    {
        var input = readline();
        arr.push(input.split(''));
    }
    var result = function(arr)
    {
        for (var i = 0; i <= arr.length - 1; i++)
        {
            for (var count = 0; count <= arr[i].length - 1; count++)
            {
                if ( i % 2 === 0)
                {
                    if ( (arr[i][count] === '.') && (count % 2 === 0) )
                    {
                        arr[i].splice(count, 1, 'B');
                    }
                    else if (arr[i][count] === '.') arr[i].splice(count, 1, 'W');
                }
                else
                {
                    if ( (arr[i][count] === '.') && (count % 2 === 0) )
                    {
                        arr[i].splice(count, 1, 'W');
                    }
                    else if (arr[i][count] === '.') arr[i].splice(count, 1, 'B');
                }
            }
        }
        return arr;
    }
    var x = result(arr);
	for (var count = 0; count <= x.length - 1; count++)
	{
		print(x[count].join(''));
	}
}

let inputString = "";
let currentLine = 0;
process.stdin.on("data", (data) => {
  inputString += data;
});
process.stdin.on("end", function () {
  inputString = inputString
    .trim()
    .split("\n")
    .map((str) => str.trim());
  main();
});
function readLine() {
  return inputString[currentLine++];
}
function main() {
  const [r, c] = readLine()
    .split(" ")
    .map((n) => parseInt(n));
  const board = [];
  const result = [...Array(r).fill(0)].map((_) => Array(c).fill(0));

  for (let i = 0; i < r; i++) {
    const _row = readLine().split("");
    board.push(_row);
  }

  for (let row = 0; row < r; row++) {
    for (let col = 0; col < c; col++) {
      if (board[row][col] === ".") {
        if (row % 2 === 0) {
          if (col % 2 === 0) result[row][col] = "W";
          else result[row][col] = "B";
        } else {
          if (col % 2 !== 0) result[row][col] = "W";
          else result[row][col] = "B";
        }
      } else {
        result[row][col] = board[row][col];
      }
    }
  }

  for (let row = 0; row < r; row++) {
    console.log(result[row].join(""));
  }
}

let inputString = '';
let currentLine = 0;
process.stdin.on('data', data => {
	inputString += data;
});
process.stdin.on('end', function () {
	inputString = inputString
		.trim()
		.split('\n')
		.map(str => str.trim());
	main();
});

function readLine() {
	return inputString[currentLine++];
}

function main() {
	let [n, m] = readLine()
		.split(' ')
		.map(x => x >> 0);
	let brd = [];
	for (let i = 0; i < n; i++) {
		brd[i] = readLine().split('');
	}

	for (let i = 0; i < n; i++) {
		for (let j = 0; j < m; j++) {
			let ci = i + j;
			if (brd[i][j] !== '-') {
				if (ci & 1) {
					brd[i][j] = 'W';
				} else {
					brd[i][j] = 'B';
				}
			}
		}
	}

	for (let i = 0; i < n; i++) {
		console.log(brd[i].join(''));
	}
}

const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
    terminal: false
});
let n, m;
const cells = [];
let numberOfGoodCell = 0;

// listCount = new Array(1001).fill(0);
function solve() {

}

function check() {
    for (let i = 0; i < cells.length; i++) {
        for (let j = 0; j < cells[i].length; j++) {
            if ((cells[i][j] !== '.' || cells[i][j] !== '-') && !checkCellAdjustable(i, j, cells[i][j])) {
                return false;
            }
        }
    }
    return true;
}

function checkCellAdjustable(i, j, char) {
    return !(cells[i][j + 1] === char || cells[i][j - 1] === char || cells[i + 1][j] === char || cells[i - 1][j] === char);
}

rl.on('line', (line) => {
    if (!n && !m) {
        [n, m] = line.split(' ').map(Number);
        return;
    }
    const list = line.split('');
    cells.push(list);
    if (cells.length === n) {
        let charI = 'B';
        for (let i = 0; i < cells.length; i++) {
            let charJ = charI;
            let output = '';
            for (let j = 0; j < cells[i].length; j++) {
                if (cells[i][j] === '.') {
                    cells[i][j] = charJ;
                }
                if (charJ === 'B') {
                    charJ = 'A';
                } else {
                    charJ = 'B';
                }
                output += cells[i][j];
            }
            console.log(output);

            if (charI === 'B') {
                charI = 'A';
            } else {
                charI = 'B';
            }
        }
    }
});

{
    var num = readline(), arr = [];
    num = num.split(' ');
    for (var i = 1; i <= +num[0]; i++)
    {
        var input = readline();
        arr.push(input.split(''));
    }
    var result = function(arr)
    {
        for (var i = 0; i <= arr.length - 1; i++)
        {
            for (var count = 0; count <= arr[i].length - 1; count++)
            {
                if ( i % 2 === 0)
                {
                    if ( (arr[i][count] === '.') && (count % 2 === 0) )
                    {
                        arr[i].splice(count, 1, 'B');
                    }
                    else if (arr[i][count] === '.') arr[i].splice(count, 1, 'W');
                }
                else
                {
                    if ( (arr[i][count] === '.') && (count % 2 === 0) )
                    {
                        arr[i].splice(count, 1, 'W');
                    }
                    else if (arr[i][count] === '.') arr[i].splice(count, 1, 'B');
                }
            }
        }
        return arr;
    }
    print(result(arr));
}

{
    var num = readline(), arr = [];
    num = num.split(' ');
    for (var i = 1; i <= +num[0]; i++)
    {
        var input = readline();
        arr.push(input.split(''));
    }
    var result = function(arr)
    {
        for (var i = 0; i <= arr.length - 1; i++)
        {
            for (var count = 0; count <= arr[i].length - 1; count++)
            {
                if ( i % 2 === 0)
                {
                    if ( (arr[i][count] === '.') && (count % 2 === 0) )
                    {
                        arr[i].splice(count, 1, 'B');
                    }
                    else if (arr[i][count] === '.') arr[i].splice(count, 1, 'W');
                }
                else
                {
                    if ( (arr[i][count] === '.') && (count % 2 === 0) )
                    {
                        arr[i].splice(count, 1, 'W');
                    }
                    else if (arr[i][count] === '.') arr[i].splice(count, 1, 'B');
                }
            }
        }
        return arr;
    }
    var x = result(arr);
	for (var count = 0; count <= x.length - 1; count++)
	{
		print(x[count]);
	}
}


var dataitr;
var stdin_input = "";
process.stdin.resume();
process.stdin.setEncoding("ascii");
process.stdin.on("data", function (input) { stdin_input += input; });
process.stdin.on("end", function () { dataitr = stdin_input.split('\n').values(); main(); });

const rl = () => dataitr.next().value.trim();
const rn = () => Number(rl());
const ra = () => rl().split(' ');
const rna = () => ra().map(x => Number(x));

const isPrime = num => {
	for(let i = 2, s = Math.sqrt(num); i <= s; i++)
		if(num % i === 0) return false;
	return num > 1;
}

function main () {
	let T = rn();
	while (T--) {
		let [n, e] = rna();
		let a = rna();

		let l = [], r = [];
		for (let i = 0; i < e; i++) {
			for (let j = i, cnt = 0; j < n; j += e) {
				if (a[j] == 1) {
					cnt++;
				}
				else {
					l[j] = cnt;
					cnt = 0;
				}
			}
			for (let j = n - i - 1, cnt = 0; j >= 0; j -= e) {
				if (a[j] == 1) {
					cnt++;
				}
				else {
					r[j] = cnt;
					cnt = 0;
				}
			}
		}

		let ans = 0;
		for (let i = 0; i < n; i++) {
			if (isPrime(a[i])) {
				ans += (l[i] + 1)*(r[i] + 1) - 1;
			}
		}
		console.log(ans);
	}
}

var isPrime = [];
for(var i=0; i<1000002; i++){
  isPrime.push(1);
}

function complex_market_analysis(){

  var inp = readline().split(" ");
	var n = parseInt(inp[0]);
	var e = parseInt(inp[1]);
	var a = readline().split(" ").map(x=>parseInt(x));

	var w=[];
	for(var i=0; i<n; i++){
	  w.push(1);
	}
	var r = 0;
	for(var i=0; i<n; i++){
		if (w[i]){
			var j = 0;
			var z = i;
			var last = -1;
			var last1 = -1;
			var p = 0;
			while (z < n){
				w[z] = 0;
				var v = a[z];

				if (v > 1){
				  last1 = last;
					last = j;
					p = isPrime[v];
					if (p){
						r -= 1;
					}
				}
				if (p){
					r += (last - last1)
				}
				z += e
				j += 1
			}
			}

	}
	print(r);
}

function prime_pairs(){
	var M = isPrime.length;
	isPrime[1] = 0;
	isPrime[0] = 0;
	var i = 2;
	while (i * i < M){
		if (isPrime[i]){
			var j = i * i;
			while (j < M){
				isPrime[j] = 0;
				j += i;
			}
		}
		i += 1
	}
var t = parseInt(readline());
for (var i=0; i<t; i++){
  complex_market_analysis();
}
}

prime_pairs();

"use strict";
process.stdin.resume();
process.stdin.setEncoding("utf-8");

let inputString = "";
let currentLine = 0;

process.stdin.on("data", (inputStdin) => {
  inputString += inputStdin;
});
process.stdin.on("end", () => {
  inputString = inputString.split("\n");
  main();
});
function readline() {
  return inputString[currentLine++];
}

function main() {
  var t = +(readline());
  while (t--) {
    solution();
  }
}

function solution() {
  let inputLine = readline().split(" ");
  let n = +inputLine[0];
  let e = +inputLine[1];

  let array = [], dp = new Array(n).fill(0);

  let arrayLine = readline().split(" ");
  for (let i = 0; i < n; i++)
    array[i] = +arrayLine[i];

  for (let i = n-1; i >= 0; i--) {
    if (isPrime(array[i])) {
      let k = 1;
      while(i + k*e < n && array[i + k*e] === 1)
        k++;
      dp[i] = k-1;
    }
    else if (array[i] === 1 && i + e < n) {
        if (isPrime(array[i + e])) dp[i] = dp[i + e]+1;
        else if (array[i + e] === 1) dp[i] = dp[i + e];
    }
  }

  const sum = dp.reduce(function(total, item) {
    return total+item;
  });

  console.log(sum);
}

function isPrime(num) {
  if (num === 2) return true;
    if (num === 1 || num%2 === 0 ) return false;

    let root = Math.floor(Math.sqrt(num));
    root += (root%2)-1;
    while (root > 1) {
      if (num%root === 0)
        return false;
      root -= 2;
    }
    return true;
}

"use strict";
process.stdin.resume();
process.stdin.setEncoding("utf-8");

let inputString = "";
let currentLine = 0;

process.stdin.on("data", (inputStdin) => {
  inputString += inputStdin;
});
process.stdin.on("end", () => {
  inputString = inputString.split("\n");
  main();
});
function readline() {
  return inputString[currentLine++];
}

function main() {
  var t = +(readline());
  while (t--) {
    solution();
  }
}

function solution() {
  let inputLine = readline().split(" ");
  let n = +inputLine[0];
  let e = +inputLine[1];

  let array = [], dp = new Array(n).fill(0);

  let arrayLine = readline().split(" ");
  for (let i = 0; i < n; i++)
    array[i] = +arrayLine[i];

  for (let i = n-1; i >= 0; i--) {
    let k = 1;
    if (isPrime(array[i])) {
      while(i + k*e < n && array[i + k*e] === 1)
        k++;
      dp[i] = k-1;
    }
    else if (array[i] === 1 && i + k*e < n) {
        if (isPrime(array[i + k*e])) dp[i] = dp[i + k*e]+1;
        else if (array[i + k*e] === 1) dp[i] = dp[i + k*e];
    }
  }

  const sum = dp.reduce(function(total, item) {
    return total+item;
  });

  console.log(sum);
}

function isPrime(num) {
  if (num === 2) return true;
    if (num === 1 || num%2 === 0 ) return false;

    let root = Math.floor(Math.sqrt(num));
    root += (root%2)-1;
    while (root > 1) {
      if (num%root === 0)
        return false;
      root -= 2;
    }
    return true;
}

    "use strict";
    process.stdin.resume();
    process.stdin.setEncoding("utf-8");

    let inputString = "";
    let currentLine = 0;

    process.stdin.on("data", (inputStdin) => {
      inputString += inputStdin;
    });
    process.stdin.on("end", () => {
      inputString = inputString.split("\n");
      main();
    });
    function readline() {
      return inputString[currentLine++];
    }

    function main() {
      var t = +(readline());
      while (t--) {
        solution();
      }
    }

    function solution() {
      let inputLine = readline().split(" ");
      let n = +inputLine[0];
      let e = +inputLine[1];

      let array = [], dp = new Array(n).fill(0);

      let arrayLine = readline().split(" ");
      for (let i = 0; i < n; i++)
        array[i] = +arrayLine[i];

      for (let i = n-1; i >= 0; i--) {
        let k = 1;
        if (isPrime(array[i])) {
          while(i + k*e < n && array[i + k*e] === 1)
            k++;
          dp[i] = k-1;
        }
        else if (array[i] === 1) {
            if (isPrime(array[i + k*e])) { dp[i] = dp[i + k*e]+1; break;}
            else if (array[i + k*e] === 1) {dp[i] = dp[i + k*e]; break;}
        }
      }

      const sum = dp.reduce(function(total, item) {
        return total+item;
      });

      console.log(sum);
    }

    function isPrime(num) {
      if (num === 2) return true;
        if (num === 1 || num%2 === 0 ) return false;

        let root = Math.floor(Math.sqrt(num));
        root += (root%2)-1;
        while (root > 1) {
          if (num%root === 0)
            return false;
          root -= 2;
        }
        return true;
    }

"use strict";
process.stdin.resume();
process.stdin.setEncoding("utf-8");

let inputString = "";
let currentLine = 0;

process.stdin.on("data", (inputStdin) => {
  inputString += inputStdin;
});
process.stdin.on("end", () => {
  inputString = inputString.split("\n");
  main();
});
function readline() {
  return inputString[currentLine++];
}

// ********** Code Start **********
function main() {
  let t = parseInt(readline());
  while (t--)
    solution();
}

function solution() {
  let inputLine = readline().split(" ");
  let n = +inputLine[0];
  let e = +inputLine[1];

  let array = [], dp = [];
  array[0] = 0;

  let arrayLine = readline().split(" ");
  for (let i = 0; i < n; i++)
    array[i] = +arrayLine[i];

  // for (let i = 0; i < n; i++) {
  //   if (isPrime(array[i])) {
  //         let t = i-e;
  //         while (t >= 0 && t < n && array[t] === 1) {
  //           dp[i]++;
  //       }
  //     }
  //   }
  console.log(array);
}

function isPrime(num) {
  if (num === 2) return true;
    if (num === 1 || num%2 === 0 ) return false;

    let root = Math.floor(Math.sqrt(num));
    root += (root%2)-1;
    while (root > 1) {
      if (num%root === 0)
        return false;
      root -= 2;
    }
    return true;
}

var isPrime = [];
for(var i=0; i<1000002; i++){
  isPrime.push(1);
}

function complex_market_analysis(){
  // var n = 9;
  // var e = 3;
  // var a = [2,4,2,1,1,1,1,4,2];
  var inp = readline().split(" ");
	var n = parseInt(inp[0]);
	print('n', n);
	var e = parseInt(inp[0]);
	print('e', e);
	var a = readline().split(" ").map(x=>parseInt(x));
	print('a', a);

	var w=[];
	for(var i=0; i<n; i++){
	  w.push(1);
	}
	var r = 0;
	for(var i=0; i<n; i++){
		if (w[i]){
			var j = 0;
			var z = i;
			var last = -1;
			var last1 = -1;
			var p = 0;
			while (z < n){
				w[z] = 0;
				var v = a[z];

				if (v > 1){
				  last1 = last;
					last = j;
					p = isPrime[v];
					if (p){
						r -= 1;
					}
				}
				if (p){
					r += (last - last1)
				}
				z += e
				j += 1
			}
			}

	}
	print(r);
}

function prime_pairs(){
	var M = isPrime.length;
	isPrime[1] = 0;
	isPrime[0] = 0;
	var i = 2;
	while (i * i < M){
		if (isPrime[i]){
			var j = i * i;
			while (j < M){
				isPrime[j] = 0;
				j += i;
			}
		}
		i += 1
	}
var t = parseInt(readline());
for (var i=0; i<t; i++){
  complex_market_analysis();
}
}

prime_pairs();

var isPrime = [];
for(var i=0; i<1000002; i++){
  isPrime.push(1);
}

function complex_market_analysis(){
  // var n = 9;
  // var e = 3;
  // var a = [2,4,2,1,1,1,1,4,2];
  var inp = readline().split(" ");
	var n = parseInt(inp[0]);
	var e = parseInt(inp[0]);
	var a = readline().split(" ").map(x=>parseInt(x));

	var w=[];
	for(var i=0; i<n; i++){
	  w.push(1);
	}
	var r = 0;
	for(var i=0; i<n; i++){
		if (w[i]){
			var j = 0;
			var z = i;
			var last = -1;
			var last1 = -1;
			var p = 0;
			while (z < n){
				w[z] = 0;
				var v = a[z];

				if (v > 1){
				  last1 = last;
					last = j;
					p = isPrime[v];
					if (p){
						r -= 1;
					}
				}
				if (p){
					r += (last - last1)
				}
				z += e
				j += 1
			}
			}

	}
	print(r);
}

function prime_pairs(){
	var M = isPrime.length;
	isPrime[1] = 0;
	isPrime[0] = 0;
	var i = 2;
	while (i * i < M){
		if (isPrime[i]){
			var j = i * i;
			while (j < M){
				isPrime[j] = 0;
				j += i;
			}
		}
		i += 1
	}
var t = parseInt(readline());
for (var i=0; i<t; i++){
  complex_market_analysis();
}
}

prime_pairs();