

import java.io.*;
import java.util.*;


public class B {

    static long modulo = 1000000007l;

    static class Pair<U extends Comparable<U>, V extends Comparable<V>>
            implements Comparable<Pair<U,V>>{

        public U a;
        public V b;

        private Pair(U a, V b) {
            this.a = a;
            this.b = b;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o)
                return true;
            if (o == null || getClass() != o.getClass())
                return false;

            Pair<?, ?> pair = (Pair<?, ?>) o;
            if (!a.equals(pair.a))
                return false;
            return b.equals(pair.b);
        }

        @Override
        public int hashCode() {
            return 31 * a.hashCode() + b.hashCode();
        }

        @Override
        public String toString() {
            return "(" + a + ", " + b + ")";
        }

        @Override
        public int compareTo(Pair<U, V> o) {
            if(this.a.equals(o.a)){
                return getV().compareTo(o.getV());
            }
            return getU().compareTo(o.getU());
        }
        private U getU() {
            return a;
        }
        private V getV() {
            return b;
        }
    }

    static BufferedReader inp = new BufferedReader(new InputStreamReader(System.in));
    static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));

    static class Graph{
        int vertices;
        ArrayList<Pair<Integer,Integer>>[] edge;
        Graph(int v){
            this.vertices = v;
            edge = new ArrayList[v+1];
            for(int i=0;i<=v;i++){
                edge[i] = new ArrayList<>();
            }
        }
        void addEdge(int a,int b,int c){
            edge[a].add(new Pair<>(b,c));
            edge[b].add(new Pair<>(a,c));
        }

        Set<Integer>  DFS(){

            boolean[] visited = new boolean[vertices+1];
            int[] cost = new int[vertices+1];
            Arrays.fill(cost,Integer.MAX_VALUE/2);
            Set<Integer>[] sets = new HashSet[vertices+1];
            for(int i=0;i<=vertices;i++){
                sets[i] = new HashSet<>();
                sets[i].add(i);
            }
            cost[vertices] = 0;
            DFSutil(1,visited,cost,sets);
            return sets[1];        }

        void DFSutil(int v,boolean[] visited,int[] cost,Set<Integer>[] sets){
            visited[v] = true;
            Iterator<Pair<Integer,Integer>> iterator = edge[v].listIterator();
            while (iterator.hasNext()){
                Pair<Integer,Integer> pair = iterator.next();
                if(!visited[pair.a]){
                    if(pair.a==vertices){
                        visited[pair.a] = true;
                        cost[v] = pair.b;
                        sets[v] = new HashSet<>(sets[pair.a]);
                        sets[v].add(v);
                    }
                    else{
                        DFSutil(pair.a,visited,cost,sets);
                        if(cost[pair.a]<cost[v]){
                            cost[v] = pair.b+cost[pair.a];
                            sets[v] = new HashSet<>(sets[pair.a]);
                            sets[v].add(v);
                        }
                    }
                }
                else{
                    if(cost[pair.a]<cost[v]){
                        cost[v] = pair.b+cost[pair.a];
                        sets[v] = new HashSet<>(sets[pair.a]);
                        sets[v].add(v);
                    }
                }
            }

        }


    }

    public static void main(String[] args) throws IOException {

        int t = Integer.parseInt(inp.readLine());
        while (t-->0){
            int size = Integer.parseInt(inp.readLine());
            int[][] given = new int[size][size];
            for(int i=0;i<size;i++){
                String[] s1 = inp.readLine().split("");
                for(int j=0;j<size;j++) {
                    if (i == 0) {
                        if(j>0)
                        given[i][j] = Integer.parseInt(s1[j]);
                    }
                    else if(i==size-1){
                        if(j<size-1)
                        given[i][j] = Integer.parseInt(s1[j]);
                    }
                    else{
                        given[i][j] = Integer.parseInt(s1[j]);
                    }
                }
            }

            given[0][0] = -1;
            given[size-1][size-1] = -1;

            Graph graph = new Graph(size*size);

            int count = 1;

            for(int i=0;i<size;i++){
                for(int j=0;j<size;j++){
                    if(j-1>=0){
                        if(given[i][j-1]==given[i][j] || given[i][j]==-1){
                            graph.addEdge(count,count-1,0);
                        }
                        else{
                            graph.addEdge(count,count-1,1);
                        }
                    }
                    if(j+1<size){
                        if(given[i][j+1]==given[i][j] || given[i][j]==-1){
                            graph.addEdge(count,count+1,0);
                        }
                        else{
                            graph.addEdge(count,count+1,1);
                        }
                    }

                    if(i-1>=0){
                        if(given[i-1][j]==given[i][j] || given[i][j]==-1){
                            graph.addEdge(count,count-size,0);
                        }
                        else{
                            graph.addEdge(count,count-size,1);
                        }
                    }

                    if(i+1<size){
                        if(given[i+1][j]==given[i][j]  || given[i][j]==-1){
                            graph.addEdge(count,count+size,0);
                        }
                        else{
                            graph.addEdge(count,count+size,1);
                        }
                    }
                    count++;
                }
            }

            Set<Integer> set = graph.DFS();

            ArrayList<Pair<Integer,Integer>> one = new ArrayList<>();
            ArrayList<Pair<Integer,Integer>> two = new ArrayList<>();

            count = 1;

            for(int i=0;i<size;i++){
                for(int j=0;j<size;j++){

                    if(count!=1 && count!=size*size && set.contains(count)){
                        if(given[i][j]==1 ){
                            one.add(new Pair<>(i,j));
                        }
                        else{
                            two.add(new Pair<>(i,j));
                        }
                    }

                    count++;
                }
            }



            if(one.size()<two.size()){
                out.write(one.size()+"\n");
                for(int i=0;i<one.size();i++){
                    out.write(one.get(i).a+" "+one.get(i).b+"\n");
                }
            }
            else{
                out.write(two.size()+"\n");
                for(int i=0;i<two.size();i++){
                    out.write(two.get(i).a+" "+two.get(i).b+"\n");
                }
            }


        }

        out.flush();

    }
    static void decToBinary(int n,int[] coutt)
    {

        for (int i = 31; i >= 0; i--) {
            int k = n >> i;
            if ((k & 1) > 0) {
                coutt[i]++;
                return;
            }
        }
    }
    static int gcd(int a, int b)
    {
        if (b == 0)
            return a;
        return gcd(b, a % b);
    }
    static void print(int[] array){
        for(int j=0;j<array.length;j++){
            System.out.print(array[j]+" ");
        }
        System.out.println();
    }
    static void print(int[][] array){
        for(int i=0;i< array.length;i++) {
            for (int j = 0; j < array[0].length; j++) {
                System.out.print(array[i][j] + " ");
            }
            System.out.println();
        }
    }
    static void print(boolean[] array){
        for(int j=0;j<array.length;j++){
            System.out.print(array[j]+" ");
        }
        System.out.println();
    }
    static void print(boolean[][] array){
        for(int i=0;i< array.length;i++) {
            for (int j = 0; j < array[0].length; j++) {
                System.out.print(array[i][j] + " ");
            }
            System.out.println();
        }
    }
    static void print(long[] array){
        for(int j=0;j<array.length;j++){
            System.out.print(array[j]+" ");
        }
        System.out.println();
    }
    static void print(long[][] array){
        for(int i=0;i< array.length;i++) {
            for (int j = 0; j < array[0].length; j++) {
                System.out.print(array[i][j] + " ");
            }
            System.out.println();
        }
    }
    static void print(String[] array){
        for(int j=0;j<array.length;j++){
            System.out.print(array[j]+" ");
        }
        System.out.println();
    }
    static void print(String[][] array){
        for(int i=0;i< array.length;i++) {
            for (int j = 0; j < array[0].length; j++) {
                System.out.print(array[i][j] + " ");
            }
            System.out.println();
        }
    }
}