codeparrot/codecomplex huggingface dataset:
// Problem: 1209_E1. Rotate Columns (easy version)
// Time complexity: np
import java.util.*;
import java.io.*;

public class E {
    public static void main(String[] args) {
        FastScanner scanner = new FastScanner();
        PrintWriter out = new PrintWriter(System.out, false);
        int t = scanner.nextInt();
        while(t-->0) {
            int n = scanner.nextInt();
            int m = scanner.nextInt();
            int[][] cols = new int[m][n];
            for(int i = 0; i < n; i++) {
                for(int j =0; j < m; j++) {
                    cols[j][i] = scanner.nextInt();
                }
            }
            int maxMask = 1 << n;
            int[] dp = new int[maxMask];
            Arrays.fill(dp, -1);
            dp[0] = 0;
            for(int i = 0; i < m; i++) {
                for(int mask = maxMask-1; mask>=0; mask--) {
                    int[] arr = cols[i].clone();
                    for(int j = 0; j < n; j++) {
                        for(int smask = mask; smask >= 0; smask = (smask-1)&mask) {
                            if (dp[smask] == -1) continue;
                            int add = 0;
                            for(int k = 0; k < n; k++) {
                                if (((mask^smask)&(1 << k)) > 0) add += arr[k];
                            }
                            dp[mask] = Math.max(dp[mask], dp[smask] + add);
                            if (smask == 0) break;
                        }
                        arr = shift(arr);
                    }
                }
            }
            out.println(dp[maxMask-1]);
        }
        out.flush();
    }
    static int[] shift (int[] a) {
        int[] b = new int[a.length];
        b[0] = a[a.length-1];
        for(int i = 0; i < a.length-1; i++) {
            b[i+1] = a[i];
        }
        return b;
    }
    public static class FastScanner {
        BufferedReader br;
        StringTokenizer st;
        
        public FastScanner(Reader in) {
            br = new BufferedReader(in);
        }
        
        public FastScanner() {
            this(new InputStreamReader(System.in));
        }
        
        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
        
        int nextInt() {
            return Integer.parseInt(next());
        }
        
        long nextLong() {
            return Long.parseLong(next());
        }
        
        double nextDouble() {
            return Double.parseDouble(next());
        }
        
        String readNextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}
// Problem: 1209_E1. Rotate Columns (easy version)
// Time complexity: np
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.IntStream;

public class Test {
  static PrintWriter writer =
      new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));

  static int readInt() {
    int ans = 0;
    boolean neg = false;
    try {
      boolean start = false;
      for (int c = 0; (c = System.in.read()) != -1; ) {
        if (c == '-') {
          start = true;
          neg = true;
          continue;
        } else if (c >= '0' && c <= '9') {
          start = true;
          ans = ans * 10 + c - '0';
        } else if (start) break;
      }
    } catch (IOException e) {
    }
    return neg ? -ans : ans;
  }

  static long readLong() {
    long ans = 0;
    boolean neg = false;
    try {
      boolean start = false;
      for (int c = 0; (c = System.in.read()) != -1; ) {
        if (c == '-') {
          start = true;
          neg = true;
          continue;
        } else if (c >= '0' && c <= '9') {
          start = true;
          ans = ans * 10 + c - '0';
        } else if (start) break;
      }
    } catch (IOException e) {
    }
    return neg ? -ans : ans;
  }

  static String readLine() {
    StringBuilder b = new StringBuilder();
    try {
      boolean start = false;
      for (int c = 0; (c = System.in.read()) != -1; ) {
        if (Character.isLetterOrDigit(c)) {
          start = true;
          b.append((char) c);
        } else if (start) break;
      }
    } catch (IOException e) {
    }
    return b.toString();
  }

  public static void main(String[] args) {
    Test te = new Test();
    te.start();
    writer.flush();
  }

  int[] buf = new int[10];

  int best(int[][] a, int c) {
    int ans = 0;
    if (c == a[0].length) {
      for (int i = 0; i < a.length; i++)
        ans += Arrays.stream(a[i]).max().getAsInt();
      return ans;
    }
    for (int r = 0; r <= a.length; r++) {
      for (int i = 0; i < a.length; i++)
        buf[(i+1) % a.length] = a[i][c];
      for (int i = 0; i < a.length; i++)
        a[i][c] = buf[i];
      ans = Math.max(ans, best(a, c+1));
    }
    return ans;
  }

  void start() {
    int t = readInt();
    while (t-- > 0) {
      int n = readInt(), m = readInt();
      int[][] a = new int[n][m];
      int[][] e = new int[n*m][];
      for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++) {
          a[i][j] = readInt();
          e[i*m+j] = new int[]{a[i][j], j};
        }
      Arrays.sort(e, (x, y) -> x[0] == y[0] ? Integer.compare(x[1], y[1])
          : Integer.compare(y[0], x[0]));
      Set<Integer> cols = new HashSet<>();
      for (int[] x : e) {
        cols.add(x[1]);
        if (cols.size() >= n) break;
      }
      int[] cs = cols.stream().mapToInt(Integer::intValue).toArray();
      int[][] na = new int[n][cs.length];
      for (int i = 0; i < n; i++)
        for (int j = 0; j < cs.length; j++)
          na[i][j] = a[i][cs[j]];
      writer.println(best(na, 0));
    }
  }
}
// Problem: 1209_E1. Rotate Columns (easy version)
// Time complexity: np
import java.util.*;
import java.io.*;
public class E584 {
    public static void main(String[] args) {
        MyScanner sc = new MyScanner();
        PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
        int t = sc.nextInt();
        while (t > 0) {
            int n = sc.nextInt(); int m = sc.nextInt();
            int [][] a = new int[m][n];
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    a[j][i] = sc.nextInt();
                }
            }
            int [][] dp = new int[m + 1][(1 << n)];
            for (int i = 1; i <= m; i++) {
                for (int j = 0; j < (1 << n); j++) {
                    dp[i][j] = Math.max(dp[i][j], dp[i - 1][j]);
                    int [] b = a[i - 1];
                    for (int start = 0; start < n; start++) {
                        int [] c = new int[n];
                        for (int p = 0; p < n; p++) c[p] = b[(start + p) % n];
                        for (int k = 0; k < (1 << n); k++) {
                            if ((k | j) == j) {
                                int sum = 0;
                                for (int p = 0; p < n; p++) {
                                    if (((k >> p) & 1) == 0 && ((j >> p) & 1) == 1) sum += c[p];
                                }
                                dp[i][j] = Math.max(dp[i][j], dp[i - 1][k] + sum);
                            }
                        }
                    }
                }
            }
            out.println(dp[m][(1 << n) - 1]);
            t--;
        }

        out.close();
    }



    //-----------MyScanner class for faster input----------
    public static class MyScanner {
        BufferedReader br;
        StringTokenizer st;

        public MyScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }


    }

}
// Problem: 1209_E1. Rotate Columns (easy version)
// Time complexity: np
import java.util.*;
import java.io.*;

public class Main {
	public static void main(String args[]) {new Main().run();}

	FastReader in = new FastReader();
	PrintWriter out = new PrintWriter(System.out);
	void run(){
		int q=in.nextInt();
		for(int i=0;i<q;i++) {
			out.println(work());
		}
		out.flush();
	}
	long mod=1000000007;
	long gcd(long a,long b) {
		return b==0?a:gcd(b,a%b);
	}
	int id[];
	long work() {
		int n=in.nextInt();
		int m=in.nextInt();
		long ret=0;
		PriorityQueue<int[]> pq=new PriorityQueue<>(new Comparator<int[]>() {
			public int compare(int[] arr1,int[] arr2) {
				return arr1[2]-arr2[2];
			}
		});
		long sum=0;
		for(int i=0;i<n;i++) {
			for(int j=0;j<m;j++) {
				int v=in.nextInt();
				pq.add(new int[] {i,j,v});
				sum+=v;
				if(pq.size()>6)pq.poll();
			}
		}
		if(m==1)return sum;
		if(n<=3) {
			while(pq.size()>0) {
				int[] p=pq.poll();
				if(pq.size()<n) {
					ret+=p[2];
				}
			}
			return ret;
		}
		int[][] A=new int[6][];
		for(int i=0;pq.size()>0;i++) {
			A[i]=pq.poll();
		}
		
		for(int i=0;i<6;i++) {
			for(int j=i+1;j<6;j++) {
				for(int k=j+1;k<6;k++) {
					out:
					for(int p=k+1;p<6;p++) {
						int s=A[i][2]+A[j][2]+A[k][2]+A[p][2];
						HashMap<Integer,ArrayList<Integer>> map=new HashMap<>();
						if(map.get(A[i][1])==null) {
							map.put(A[i][1],new ArrayList<>());
						}
						if(map.get(A[j][1])==null) {
							map.put(A[j][1],new ArrayList<>());
						}
						if(map.get(A[k][1])==null) {
							map.put(A[k][1],new ArrayList<>());
						}
						if(map.get(A[p][1])==null) {
							map.put(A[p][1],new ArrayList<>());
						}
						map.get(A[i][1]).add(A[i][0]);
						map.get(A[j][1]).add(A[j][0]);
						map.get(A[k][1]).add(A[k][0]);
						map.get(A[p][1]).add(A[p][0]);
						if(map.size()!=2) {
							ret=Math.max(ret, s);
							continue;
						}
						Integer l1=null,l2=null,r1=null,r2=null;
						for(int key:map.keySet()) {
							ArrayList<Integer> list=map.get(key);
							if(map.get(key).size()!=2) {
								ret=Math.max(ret, s);
								continue out;
							}
							if(l1==null) {
								l1=list.get(0);
								l2=list.get(1);
							}else {
								r1=list.get(0);
								r2=list.get(1);
							}
						}
						if((Math.abs(l1-l2)==2&&Math.abs(r1-r2)==2)||(Math.abs(l1-l2)!=2&&Math.abs(r1-r2)!=2)) {
							ret=Math.max(ret, s);
						}
					}
				}
			}
		}
		
		return ret;
	}
}



class FastReader
{
	BufferedReader br;
	StringTokenizer st;

	public FastReader()
	{
		br=new BufferedReader(new InputStreamReader(System.in));
	}

	public String next() 
	{
		if(st==null || !st.hasMoreElements())
		{
			try {
				st = new StringTokenizer(br.readLine());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return st.nextToken();
	}

	public int nextInt() 
	{
		return Integer.parseInt(next());
	}

	public long nextLong()
	{
		return Long.parseLong(next());
	}
}
// Problem: 1209_E1. Rotate Columns (easy version)
// Time complexity: np
import java.io.*;
import java.util.*;

public class C {

	static int n, m, a[][];
	static int[][] memo;
	static Integer[] indices;

	static int[] getCol(int col, int shift) {
		int[] ans = new int[n];
		for (int i = 0, j = shift; i < n; i++, j = (j + 1) % n) {
			ans[i] = a[j][col];
		}
		return ans;

	}

	static int dp(int col, int msk) {
		if (col == n||col==m)
			return 0;
		if (memo[msk][col] != -1)
			return memo[msk][col];
		int ans = 0;
		for (int shift = 0; shift < n; shift++) {
			int[] currCol = getCol(indices[col], shift);
			for (int nxtMsk = 0; nxtMsk < 1 << n; nxtMsk++) {
				if ((nxtMsk & msk) != msk)
					continue;
				int curr = 0;
				int diff = msk ^ nxtMsk;
				for (int i = 0; i < n; i++)
					if ((diff & 1 << i) != 0)
						curr += currCol[i];
				ans = Math.max(ans, dp(col + 1, nxtMsk) + curr);
			}
		}
		return memo[msk][col] = ans;
	}

	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner();
		PrintWriter out = new PrintWriter(System.out);
		int tc = sc.nextInt();
		while (tc-- > 0) {
			n = sc.nextInt();
			m = sc.nextInt();
			indices = new Integer[m];

			memo = new int[1 << n][m];
			for (int[] x : memo)
				Arrays.fill(x, -1);
			a = new int[n][m];
			int[] max = new int[m];
			for (int i = 0; i < n; i++)
				for (int j = 0; j < m; j++) {
					a[i][j] = sc.nextInt();
					max[j] = Math.max(max[j], a[i][j]);
				}
			for (int j = 0; j < m; j++) {
				indices[j] = j;
			}
			Arrays.sort(indices, Comparator.comparingInt(i -> -max[i]));
			out.println(dp(0, 0));
		}
		out.close();

	}

	static class Scanner {
		BufferedReader br;
		StringTokenizer st;

		Scanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		Scanner(String fileName) throws FileNotFoundException {
			br = new BufferedReader(new FileReader(fileName));
		}

		String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		String nextLine() throws IOException {
			return br.readLine();
		}

		int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		long nextLong() throws NumberFormatException, IOException {
			return Long.parseLong(next());
		}

		double nextDouble() throws NumberFormatException, IOException {
			return Double.parseDouble(next());
		}

		boolean ready() throws IOException {
			return br.ready();
		}

	}

	static void sort(int[] a) {
		shuffle(a);
		Arrays.sort(a);
	}

	static void shuffle(int[] a) {
		int n = a.length;
		Random rand = new Random();
		for (int i = 0; i < n; i++) {
			int tmpIdx = rand.nextInt(n);
			int tmp = a[i];
			a[i] = a[tmpIdx];
			a[tmpIdx] = tmp;
		}
	}

}
// Problem: 1209_E1. Rotate Columns (easy version)
// Time complexity: np
import java.util.*;
public class Main {
    public static int n, m;
    public static int[][] arr;
    public static class Item implements Comparable<Item> {
        int i, x;
        public Item(int i, int x) {
            this.i = i;
            this.x = x;
        }
        public int compareTo(Item other) {
            if (x == other.x) {
                return i - other.i;
            }
            return other.x - x;
        }
    }
    public static int calc(int[] cols, int k, String mask) {
        if (k == cols.length) {
            int res = 0;
            for (int i = 0; i < n; i++) {
                int max = 0;
                for (int j = 0; j < cols.length; j++) {
                    int shift = mask.charAt(j) - '0';
                    max = Math.max(max, arr[(shift + i) % n][cols[j]]);
                }
                res += max;
            }
            return res;
        } else {
            int best = 0;
            for (int i = 0; i < n; i++) {
                best = Math.max(best, calc(cols, k + 1, mask + i));
            }
            return best;
        }
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        for (int i = 0; i < t; i++) {
            n = in.nextInt();
            m = in.nextInt();
            arr = new int[n][m];
            for (int j = 0; j < n; j++) {
                for (int k = 0; k < m; k++) {
                    arr[j][k] = in.nextInt();
                }
            }
            Item[] max = new Item[m];
            for (int j = 0; j < m; j++) {
                max[j] = new Item(j, 0);
                for (int k = 0; k < n; k++) {
                    max[j].x = Math.max(max[j].x, arr[k][j]);
                }
            }
            Arrays.sort(max);
            int[] cols = new int[Math.min(n, m)];
            for (int j = 0; j < cols.length; j++) {
                cols[j] = max[j].i;
            }
            System.out.println(calc(cols, 0, ""));
        }
    }
}
// Problem: 1209_E1. Rotate Columns (easy version)
// Time complexity: np
import java.io.*;
import java.util.*;

public class r584p5 {
    private static BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
    private static PrintWriter pw = new PrintWriter(System.out);
    private static int n, m, arr[][];
    private static ArrayList<HashSet<Integer>> chls;

    private static void gench(){
        chls.add(new HashSet<>());
        chls.get(0).add(0);

        for(int i=1; i<(1<<n); i++){
            int des = i^Integer.highestOneBit(i);
            HashSet<Integer> st = new HashSet<>();
            for(int z : chls.get(des)){
                st.add(z);
                st.add(z|Integer.highestOneBit(i));
            }
            chls.add(st);
        }
    }

    private static void cal(){
        int val[][] = new int[(1<<n)][m];

        for(int j=0; j<m; j++){
            val[0][j] = 0;
            for(int mask=1; mask<(1<<n); mask++){
                int max = 0;
                for(int begin=0; begin<n; begin++){
                    int sum = 0;
                    for(int ptr=begin, pos=0; pos<n; ptr=(ptr+1)%n, pos++){
                        if((mask&(1<<pos)) > 0)
                            sum += arr[ptr][j];
                    }
                    max = Math.max(max, sum);
                }
                val[mask][j] = max;
            }
        }

        int dp[][] = new int[(1<<n)][m];

        for(int mask=0; mask<(1<<n); mask++)
            dp[mask][0] = val[mask][0];

        for(int j=1; j<m; j++){
            dp[0][j] = 0;
            for(int mask=1; mask<(1<<n); mask++){

                dp[mask][j] = 0;
                for(int ch1 : chls.get(mask)){
                    int ch2 = mask^ch1;

                    dp[mask][j] = Math.max(dp[mask][j], val[ch1][j]+dp[ch2][j-1]);
                }
            }
        }

        pw.println(dp[(1<<n)-1][m-1]);
    }

    private static void run()throws IOException{
        StringTokenizer tk = new StringTokenizer(r.readLine());
        n = Integer.parseInt(tk.nextToken());
        m = Integer.parseInt(tk.nextToken());
        arr = new int[n][m];
        chls = new ArrayList<>();

        for(int i=0; i<n; i++){
            tk = new StringTokenizer(r.readLine());
            for(int j=0; j<m; j++)
                arr[i][j] = Integer.parseInt(tk.nextToken());
        }

        gench();
        cal();
    }

    public static void main(String args[])throws IOException{
        int t = Integer.parseInt(r.readLine());

        while(t-->0)
            run();

        pw.flush();
        pw.close();
    }
}
// Problem: 1209_E1. Rotate Columns (easy version)
// Time complexity: np
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.IntStream;

public class Test {
  static PrintWriter writer =
      new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));

  static int readInt() {
    int ans = 0;
    boolean neg = false;
    try {
      boolean start = false;
      for (int c = 0; (c = System.in.read()) != -1; ) {
        if (c == '-') {
          start = true;
          neg = true;
          continue;
        } else if (c >= '0' && c <= '9') {
          start = true;
          ans = ans * 10 + c - '0';
        } else if (start) break;
      }
    } catch (IOException e) {
    }
    return neg ? -ans : ans;
  }

  static long readLong() {
    long ans = 0;
    boolean neg = false;
    try {
      boolean start = false;
      for (int c = 0; (c = System.in.read()) != -1; ) {
        if (c == '-') {
          start = true;
          neg = true;
          continue;
        } else if (c >= '0' && c <= '9') {
          start = true;
          ans = ans * 10 + c - '0';
        } else if (start) break;
      }
    } catch (IOException e) {
    }
    return neg ? -ans : ans;
  }

  static String readLine() {
    StringBuilder b = new StringBuilder();
    try {
      boolean start = false;
      for (int c = 0; (c = System.in.read()) != -1; ) {
        if (Character.isLetterOrDigit(c)) {
          start = true;
          b.append((char) c);
        } else if (start) break;
      }
    } catch (IOException e) {
    }
    return b.toString();
  }

  public static void main(String[] args) {
    Test te = new Test();
    te.start();
    writer.flush();
  }

  void start() {
    int t = readInt();
    while (t-- > 0) {
      int n = readInt(), m = readInt();
      int[][] a = new int[n][m];
      for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
          a[i][j] = readInt();
      int[] dp = new int[1<<n];
      Arrays.fill(dp, -1);
      dp[0] = 0;
      for (int c = 0; c < m; c++) {
        for (int i = (1 << n) - 1; i >= 0; i--) {
          int u = (1 << n) - 1 - i;
          int p = u;
          if (dp[i] >= 0)
            while (p > 0) {
              for (int r = 0; r < n; r++) {
                  int sum = 0;
                  for (int j = 0; j < n; j++) if (((p >> j) & 1) != 0) sum += a[(j + r) % n][c];
                  dp[i | p] = Math.max(dp[i | p], dp[i] + sum);
                }
              p = (p - 1) & u;
            }
        }
      }
      writer.println(dp[(1<<n) - 1]);
    }
  }
}