description: You are given an array a[0 … n - 1] = [a_0, a_1, …, a_{n - 1}] of zeroes and ones only. Note that in this problem, unlike the others, the array indexes are numbered from zero, not from one.
  
  In one step, the array a is replaced by another array of length n according to the following rules: 
  
    1. First, a new array a^{→ d} is defined as a cyclic shift of the array a to the right by d cells. The elements of this array can be defined as a^{→ d}_i = a_{(i + n - d) mod n}, where (i + n - d) mod n is the remainder of integer division of i + n - d by n. 
  
  It means that the whole array a^{→ d} can be represented as a sequence $$$a^{→ d} = [a_{n - d}, a_{n - d + 1}, …, a_{n - 1}, a_0, a_1, …, a_{n - d - 1}]$$$
  
    2. Then each element of the array a_i is replaced by a_i  \&  a^{→ d}_i, where \& is a logical "AND" operator. 
  
  
  
  For example, if a = [0, 0, 1, 1] and d = 1, then a^{→ d} = [1, 0, 0, 1] and the value of a after the first step will be [0  \&  1, 0  \&  0, 1  \&  0, 1  \&  1], that is [0, 0, 0, 1].
  
  The process ends when the array stops changing. For a given array a, determine whether it will consist of only zeros at the end of the process. If yes, also find the number of steps the process will take before it finishes.
  
  Input
  
  The first line contains an integer t (1 ≤ t ≤ 1000) — the number of test cases.
  
  The next 2t lines contain descriptions of the test cases. 
  
  The first line of each test case description contains two integers: n (1 ≤ n ≤ 10^6) — array size and d (1 ≤ d ≤ n) — cyclic shift offset. The second line of the description contains n space-separated integers a_i (0 ≤ a_i ≤ 1) — elements of the array.
  
  It is guaranteed that the sum of n over all test cases does not exceed 10^6.
  
  Output
  
  Print t lines, each line containing the answer to the corresponding test case. The answer to a test case should be a single integer — the number of steps after which the array will contain only zeros for the first time. If there are still elements equal to 1 in the array after the end of the process, print -1.
  
  Example
  
  Input
  
  
  5
  2 1
  0 1
  3 2
  0 1 0
  5 2
  1 1 0 1 0
  4 2
  0 1 0 1
  1 1
  0
  
  
  Output
  
  
  1
  1
  3
  -1
  0
  
  Note
  
  In the third sample test case the array will change as follows: 
  
    1. At the beginning a = [1, 1, 0, 1, 0], and a^{→ 2} = [1, 0, 1, 1, 0]. Their element-by-element "AND" is equal to $$$[1  \&  1, 1  \&  0, 0  \&  1, 1  \&  1, 0  \&  0] = [1, 0, 0, 1, 0]$$$ 
    2. Now a = [1, 0, 0, 1, 0], then a^{→ 2} = [1, 0, 1, 0, 0]. Their element-by-element "AND" equals to $$$[1  \&  1, 0  \&  0, 0  \&  1, 1  \&  0, 0  \&  0] = [1, 0, 0, 0, 0]$$$ 
    3. And finally, when a = [1, 0, 0, 0, 0] we get a^{→ 2} = [0, 0, 1, 0, 0]. Their element-by-element "AND" equals to $$$[1  \&  0, 0  \&  0, 0  \&  1, 0  \&  0, 0  \&  0] = [0, 0, 0, 0, 0]$$$ 
  
  Thus, the answer is 3 steps.
  
  In the fourth sample test case, the array will not change as it shifts by 2 to the right, so each element will be calculated as 0  \&  0 or 1  \&  1 thus not changing its value. So the answer is -1, the array will never contain only zeros.
solution: import java.io.*;
  import java.util.*;
  
  public class Main {
      static PrintWriter pw;
      static Scanner sc;
      static long ceildiv(long x, long y) { return (x+y-1)/y; }
      static int mod(long x, int m) { return (int)((x%m+m)%m); }
      static void put(Map<Integer, Integer> map, Integer p){if(map.containsKey(p)) map.replace(p, map.get(p)+1); else map.put(p, 1); }
      static void rem(Map<Integer, Integer> map, Integer p){ if(map.get(p)==1) map.remove(p);else map.replace(p, map.get(p)-1); }
      static int Int(boolean x){ return x?1:0; }
      static final int inf=(int)1e9, mod= inf + 7;
      static final long infL=inf*1l*inf;
      static final double eps=1e-9;
      public static long gcd(long x, long y) { return y==0? x: gcd(y, x%y); }
      public static void main(String[] args) throws Exception {
          sc = new Scanner(System.in);
          pw = new PrintWriter(System.out);
          int t = sc.nextInt();
          while (t-- > 0)
              testCase();
          pw.close();
      }
      static int[] adj, ans;
      static boolean[] visited;
      static void testCase() throws IOException{
          int n = sc.nextInt(), d = sc.nextInt();
          int[] arr = sc.nextArr(n);
          if(d == n){
              boolean ans = true;
              for (int i = 0; i < n && ans; i++)
                  ans = arr[i] == 0;
              pw.println(ans ? 0 : -1);
              return;
          }
          adj = new int[n];
          for (int i = 0; i < n; i++)
              adj[i] = arr[i] == 0 ? -1 : mod(i-d, n);
          visited = new boolean[n];
          ans = new int[n];
          Arrays.fill(ans, -1);
          int max = 0;
          for (int i = 0; i < n && max != -1; i++) {
              if(!visited[i]){
                  int dist = dist(i);
                  if(dist == -1)
                      max = -1;
                  else
                      max = Math.max(dist, max);
              }
          }
          pw.println(max);
      }
  
      static int dist(int i){
          if(visited[i])
              return ans[i];
          if(adj[i] == -1)
              return 0;
          visited[i] = true;
          int next = dist(adj[i]);
          ans[i] = next == -1 ? -1 : next+1;
          return ans[i];
      }
  
      static void printArr(int[] arr) {
          for (int i = 0; i < arr.length; i++)
              pw.print(arr[i] + " ");
          pw.println();
      }
  
      static void printArr(long[] arr) {
          for (int i = 0; i < arr.length; i++)
              pw.print(arr[i] + " ");
          pw.println();
      }
  
      static void printArr(double[] arr) {
          for (int i = 0; i < arr.length; i++)
              pw.print(arr[i] + " ");
          pw.println();
      }
  
      static void printArr(Integer[] arr) {
          for (int i = 0; i < arr.length; i++)
              pw.print(arr[i] + " ");
          pw.println();
      }
  
      static void printIter(Iterable list) {
          for (Object o : list)
              pw.print(o + " ");
          pw.println();
      }
  
      static class Scanner {
          StringTokenizer st;
          BufferedReader br;
  
          public Scanner(InputStream s) {
              br = new BufferedReader(new InputStreamReader(s));
          }
  
          public Scanner(FileReader r) {
              br = new BufferedReader(r);
          }
  
          public String next() throws IOException {
              while (st == null || !st.hasMoreTokens())
                  st = new StringTokenizer(br.readLine());
              return st.nextToken();
          }
  
          public int nextInt() throws IOException {
              return Integer.parseInt(next());
          }
  
          public long nextLong() throws IOException {
              return Long.parseLong(next());
          }
  
          public String nextLine() throws IOException {
              return br.readLine();
          }
  
          public double nextDouble() throws IOException {
              return Double.parseDouble(next());
          }
  
          public int[] nextDigits() throws IOException {
              String s = nextLine();
              int[] arr = new int[s.length()];
              for (int i = 0; i < arr.length; i++)
                  arr[i] = s.charAt(i) - '0';
              return arr;
          }
  
          public int[] nextArr(int n) throws IOException {
              int[] arr = new int[n];
              for (int i = 0; i < arr.length; i++)
                  arr[i] = nextInt();
              return arr;
          }
  
          public Integer[] nextsort(int n) throws IOException {
              Integer[] arr = new Integer[n];
              for (int i = 0; i < n; i++)
                  arr[i] = nextInt();
              return arr;
          }
  
          public Pair nextPair() throws IOException {
              return new Pair(nextInt(), nextInt());
          }
  
          public long[] nextLongArr(int n) throws IOException {
              long[] arr = new long[n];
              for (int i = 0; i < n; i++)
                  arr[i] = nextLong();
              return arr;
          }
  
          public Pair[] nextPairArr(int n) throws IOException {
              Pair[] arr = new Pair[n];
              for (int i = 0; i < n; i++)
                  arr[i] = nextPair();
              return arr;
          }
  
          public boolean hasNext() throws IOException {
              return (st != null && st.hasMoreTokens()) || br.ready();
          }
      }
  
      static class Pair implements Comparable<Pair> {
          int x;
          int y;
  
          public Pair(int x, int y) {
              this.x = x;
              this.y = y;
          }
  
          public Pair(Map.Entry<Integer, Integer> a) {
              x = a.getKey();
              y = a.getValue();
          }
  
          public int hashCode() {
              return (int) ((this.x*1l*100003 + this.y) % mod);
          }
  
          public int compareTo(Pair p) {
              if(x != p.x)
                  return x - p.x;
              return y - p.y;
          }
  
          public boolean equals(Object obj) {
              if (obj == null) {
                  return false;
              }
              if (this.getClass() != obj.getClass()) {
                  return false;
              }
              Pair p = (Pair) obj;
              return this.x == p.x && this.y == p.y;
          }
  
          public Pair clone() {
              return new Pair(x, y);
          }
  
          public String toString() {
              return this.x + " " + this.y;
          }
  
          public void subtract(Pair p) {
              x -= p.x;
              y -= p.y;
          }
  
          public void add(Pair p) {
              x += p.x;
              y += p.y;
          }
      }
  }
  