#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <assert.h>

using namespace std;

bool compare(pair<int, int> &f, pair<int, int> &s)
{
	return (f.first != s.first) ? (f.first > s.first) : f.second < s.second;
}

int main()
{
	freopen("iguana.in", "r", stdin);
	//freopen("iguana.out", "w", stdout);

	int test;
	cin >> test;

	int a[128][128];

	for(int tt=0; tt<test; tt++)
	{
		int n;
		cin >> n;

		int cnt[128]={0};
		for(int i=0; i<n; i++)
		{
			for(int j=0; j<n; j++)
			{
				cin >> a[i][j];
				if (a[i][j]==1) cnt[i]++;				
			}
		}

		vector<pair<int, int>> s;
		s.reserve(n);
		
		for(int i=0; i<n; i++)
		{
			s.push_back(make_pair(cnt[i], i));
		}

		sort(s.begin(), s.end(), compare);

		int mark = 0;
		int c[128]={0};
		int color = 1;
		
		while(mark < n)
		{
			set<int> cur;

			for(int i=0; i<n; i++)
			{
				int index = s[i].second;
				if (c[index] != 0) continue;

				bool found = false;
				for(set<int>::iterator iter = cur.begin(); iter!= cur.end(); iter++)
				{
					int added = *iter;
					if (a[index][added] != 0)
					{
						found = true;
						break;
					}
				}

				if (!found)
				{
					c[index] = color;
					cur.insert(index);
					mark++;
				}				
			}

			color++;
		}

		cout << color - 1 << endl;
		
	}

	return 0;
}