from django.contrib import admin, messages

from .models import Menu, Item

class ItemAdmin(admin.ModelAdmin):

    def save_model(self, request, obj, form, change):

        if request.POST["parent"] == "":
            return super(ItemAdmin, self).save_model(request, obj, form, change)

        if not obj.menu == obj.parent.menu:
            messages.set_level(request, messages.ERROR)
            messages.add_message(request, messages.ERROR, f"Invalid menu: item's menu must be same as its parents "
                                                          f"({obj.parent.menu.title})")
        super(ItemAdmin, self).save_model(request, obj, form, change)

class ItemInline(admin.TabularInline):
    model = Item

class MenuAdmin(admin.ModelAdmin):
    inlines = [ItemInline, ]

admin.site.register(Menu, MenuAdmin)
admin.site.register(Item, ItemAdmin)


import numpy as np

def first_correlation():
    # Два вектора - число автомобилей
    var1 = np.array([80, 98, 75, 91, 78])
    var2 = np.array([100, 82, 105, 89, 102])

    # Расчет коэффициента корреляции
    print(np.corrcoef(var1, var2)[0, 1])

first_correlation()


def find(si, sj, N):
    global arr
    row, col = 1, 1
    ni, nj = si, sj
    for _ in range(N):
        ni += 1
        if ni < N and arr[ni][sj]:
            row += 1
        else:
            break
    for _ in range(N):
        nj += 1
        if nj < N and arr[si][nj]:
            col += 1
        else:
            break
    for i in range(si, ni):
        for j in range(sj, nj):
            arr[i][j] = 0
    return row, col
T = int(input())
for tc in range(1, T + 1):
    N = int(input())
    arr = [list(map(int, input().split())) for _ in range(N)]
    result = []
    for i in range(N):
        for j in range(N):
            if arr[i][j]:
                result.append(find(i, j, N))
    result = sorted(result, key=lambda x: (x[0] * x[1], x[0]))
    print('#{} {}'.format(tc, len(result)), end=' ')
    for i in result:
        print(i[0], i[1], end=' ')
    print()