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

public class TestsGen implements Runnable {
	public static void main(String[] args) {
		if (args.length < 1) {
			for (testNum = 1; (new File(getName(testNum) + ".t")).exists(); testNum++)
				;
		} else {
			testNum = Integer.parseInt(args[0]);
		}
		new Thread(new TestsGen()).start();
	}

	PrintWriter out;

	Random rand = new Random(6439586L);

	static int testNum;

	static String getName(int i) {
		return i / 10 + "" + i % 10;
	}

	void open() {
		try {
			System.out.println("Generating test " + testNum);
			out = new PrintWriter(getName(testNum));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			System.exit(-1);
		}
	}

	void close() {
		out.close();
		testNum++;
	}
	
	final String ALPHA = "abcdefghijklmnopqrstuvwxyz";

	String randString(int len, String alpha) {
		StringBuilder ans = new StringBuilder();
		int k = alpha.length();
		for (int i = 0; i < len; i++) {
			ans.append(alpha.charAt(rand.nextInt(k)));
		}
		return ans.toString();
	}


	long rand(long l, long r) {
		if (r - l + 1 == 0) {
			System.exit(0);
		}
		return l + (rand.nextLong() >>> 1) % (r - l + 1);
	}

	int rand(int l, int r) {
		return l + rand.nextInt(r - l + 1);
	}

	final int MAXN = 1000;
	final long MAXA = 1000;
	
	void genNOtest(long min, long max) {
		open();
		int n = (int) rand(2, Math.min(MAXN, max));
		out.println(n);
		long[][] matrix = new long[n][n];
		for (int i = 0; i < n; ++i) {
			matrix[i][i] = rand(min + 1, max / 2 - 1);
			long sum = 0;
			for (int j = 0; j < n - 1; ++j) {
				if (j != i) {
					matrix[i][j] = rand(0, matrix[i][i] - sum);
					sum += matrix[i][j];
				}	
			}
			if (i != n - 1) {
				matrix[i][n - 1] = matrix[i][i] - sum;
			} else {
				matrix[i][n - 2] += matrix[i][i] - sum;
			}
		}
		int greater = rand(0, n - 2) + 1;
		int less = rand(1, n - greater);
		int[] used = new int[n];
		for (int i = 0; i < greater - 1; ++i) {
			int j = rand(0, n - 1);
			while (used[j] == 1) {
				j = rand(0, n - 1);
			}
			used[j] = 1;
			matrix[j][j] = rand(matrix[j][j] + 1, max / 2);
		}
		if (used[n - 1] == 0) {
			used[n - 1] = 1;
			matrix[n - 1][n - 1] = rand(matrix[n - 1][n - 1] + 1, max / 2);
		}
		for (int i = 0; i < less; ++i) {
			int j = rand(0, n - 1);
			while (used[j] == 1) {
				j = rand(0, n - 1);
			}
			used[j] = 1;
			matrix[j][j] = rand(0, matrix[j][j] - 1);
		}
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < n; ++j) {
				out.print(matrix[i][j] + " ");
			}
			out.println();
		}
		close();
	}

	void genYEStest(long min, long max, int zzz) {
		open();
		int n = (int) rand(2, Math.min(MAXN, max));
		out.println(n);
		long[][] matrix = new long[n][n];
		for (int i = 0; i < n; ++i) {
			matrix[i][i] = rand(min, max / 2 - 1);
			long sum = 0;
			for (int j = 0; j < n - 1; ++j) {
				if (j != i) {
					matrix[i][j] = rand(0, matrix[i][i] - sum);
					sum += matrix[i][j];
				}	
			}
			if (i != n - 1) {
				matrix[i][n - 1] = matrix[i][i] - sum;
			} else {
				matrix[i][n - 2] += matrix[i][i] - sum;
			}
		}
		int greater = 0;
		if (zzz == 1) {
			greater = n;
		} else {
			greater = rand(1, n - 1);
		}
		int[] used = new int[n];
		for (int i = 0; i < greater; ++i) {
			int j = rand(0, n - 1);
			while (used[j] == 1) {
				j = rand(0, n - 1);
			}
			used[j] = 1;
			matrix[j][j] = rand(matrix[j][j] + 1, max / 2);
		}
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < n; ++j) {
				out.print(matrix[i][j] + " ");
			}
			out.println();
		}
		close();
	}

	void genEQtest(long min, long max) {
		open();
		int n = (int) rand(2, Math.min(MAXN, max));
		out.println(n);
		long[][] matrix = new long[n][n];
		for (int i = 0; i < n; ++i) {
			matrix[i][i] = rand(min, max / 2);
			long sum = 0;
			for (int j = 0; j < n - 1; ++j) {
				if (j != i) {
					matrix[i][j] = rand(0, matrix[i][i] - sum);
					sum += matrix[i][j];
				}	
			}
			if (i != n - 1) {
				matrix[i][n - 1] = matrix[i][i] - sum;
			} else {
				matrix[i][n - 2] += matrix[i][i] - sum;
			}
		}
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < n; ++j) {
				out.print(matrix[i][j] + " ");
			}
			out.println();
		}
		close();
	}

	public void solve() throws IOException {
		for (int i = 0; i < 12; ++i) {
			genYEStest(0, Byte.MAX_VALUE, 1);
			genYEStest(Byte.MAX_VALUE + 1, MAXA, 1);
		}
		for (int i = 0; i < 5; ++i) {
			genYEStest(0, Byte.MAX_VALUE, 0);
			genYEStest(Byte.MAX_VALUE + 1, MAXA, 0);
		}
		for (int i = 0; i < 5; ++i) {
			genEQtest(Byte.MAX_VALUE + 1, MAXA);			
		}
		for (int i = 0; i < 4; ++i) {
			genEQtest(0, Byte.MAX_VALUE);
		}
		genNOtest(0, Byte.MAX_VALUE);
		genNOtest(Byte.MAX_VALUE + 1, MAXA);

		for (int t = 0; t < 4; ++t) {
			open();
			int n = rand(150, MAXN);
			out.println(n);
			long[][] matrix = new long[n][n];
			for (int i = 0; i < n; ++i) {
				for (int j = 0; j < n; ++j) {
					matrix[i][j] = rand(300, MAXA);
				}
			}
			int ge = rand(0, n - 2);
			int[] used = new int[n];
			for (int k = 0; k < ge; ++k) {
				int i = rand(0, n - 1);
				while (used[i] == 1) {
					i = rand(0, n - 1);
				}
				matrix[i][i] = rand(0, MAXA);
				long sum = 0;
				for (int j = 0; j < n; ++j) {
					if (j != i) {
						matrix[i][j] = rand(0, matrix[i][i] - sum);
						sum += matrix[i][j];
					}
				}
			}
			matrix[n - 1][n - 1] = rand(0, MAXA);
			long sum = 0;
			for (int j = 0; j < n - 1; ++j) {
				matrix[n - 1][j] = rand(0, matrix[n - 1][n - 1] - sum - 1);
				sum += matrix[n - 1][j];
			}
			for (int i = 0; i < n; ++i) {
				for (int j = 0; j < n; ++j) {
					out.print(matrix[i][j] + " ");
				}
			out.println();
			}
			close();
		}
		
		open();
		out.println(MAXN);
		for (int i = 0; i < MAXN; ++i) {
			for (int j = 0; j < MAXN; ++j) {
				out.print(MAXA + " ");
			}
			out.println();
		}
		close();

	}

	void myAssert(boolean e, String msg) {
		if (!e) {
			throw new Error(msg);
		}
	}

	public void run() {
		try {
			solve();
		} catch (Throwable e) {
			e.printStackTrace();
			System.exit(-1);
		}
	}
}
