# The problem statement is at
# https://cw.felk.cvut.cz/brute/data/ae/release/2019l_be5b33pge/pge19/evaluation/input.php?task=secure
# and also at the end of this file as a comment.


matrix = []
M, N = 0, 0
resTable = []

def checkL( row1, col1, lastRow, lastCol):
    count = 0
    # check last row
    for j in range(col1, lastCol+1):
        if matrix[lastRow][j] == 1:
            count += 1
    # check last column
    for i in range(row1, lastRow):  # note unnecessary -1
        if matrix[i][lastCol] == 1:
            count += 1
    return count

'''
               col1  lastCol
               |     |
         +-----------------------+
         | ... ...     ...   ... |
    row1 | ... 1 A B C ... Z ... |
         | ... A A B C ... Z ... |
         | ... B B B C ... Z ... |
 lastRow | ... C C C C ... Z ... |
         | ... ...     ... Z ... |
         | ... Z Z Z Z ... Z ... |
         +-----------------------+

Start at position (row1, col1) and investigate the sequence
of square sub-matrices of growing size, by appending
consecutive L-shaped regions denoted by A's, B's, C's etc.
LastRow and lastCol indexes move down and right, respectively.
'''

# upper left corner at (row1, col1)
def checkAllAreas(row1, col1):
    global resTable
    size = 1
    countOnes = 1            # because of 1 in upper left corner
    while True:
        size += 1            # size of mx starts at 2
        lastRow = row1 + size - 1
        lastCol = col1 + size - 1
        # check boundaries
        if lastRow >= M: return
        if lastCol >= N: return

        countOnes += checkL(row1, col1, lastRow, lastCol)
        if countOnes < 3: continue
        if countOnes == 3:
            resTable[size] += 1
            #print("found at", row1, col1, size)
        else: return


def solution():
    for i in range(M):
        for j in range(N):
            if matrix[i][j] == 1:
                checkAllAreas(i, j)
    return resTable


# ---------------------------------------------------------------------------
#        M A I N
# ---------------------------------------------------------------------------


# load data and initialize
M, N = map(int, input().split())
for i in range(M):
    row =  list( map( int, input().split() ) )
    matrix.append(row)

resTable = [0] * (max(M, N) + 1)

# solve
solution()

# output result
for i in range(len(resTable)):
    if resTable[i] > 0:
        print(i, resTable[i])

# ---------------------------------------------------------------------------
#       P R O B L E M    S T A T E M E N T
#              with  examples
# ---------------------------------------------------------------------------

'''
Secure Matrix Areas

In this problem, we consider a matrix consisting of M×N cells.
Each matrix cell is either empty or it is marked by some symbol.
The marked cells are named secure cells.
A secure area in the matrix is a square submatrix Q with the following properties:

    1. Q consists of at least two rows (or columns).
    2. There are exactly three secure cells in Q.
    3. The cell in the upper left corner of Q is a secure cell.

The size of a secure area is equal to the number of rows (columns) in it.


The task
--------
Determine the number of all secure areas of different sizes in the given matrix.

Input
-----

The first input line contains two integers M and N representing the number of rows
and the number of columns of the input matrix. Next, there are exactly M lines.
Each line contains N values, the values correspond to the values
in a particular matrix row. Each value is 0 or 1.
Value 0 represents empty cell, value 1 represents a secure cell.
All values are separated by single space.
It is guaranteed that the input matrix always contains at least one secure area.
It holds 2 ≤ M, N ≤ 1000.

Output
------
The output contains one or more text lines. Each line contains two integers s, C(s),
separated by space. Value s represents the size of a secure area,
value C(s) represents the number secure areas which size is exactly s.
The lines are sorted in ascending order of values s.
Only positive values of C(s) are printed.
When C(s) = 0 neither s nor C(s) is printed. The output contains no empty line.



---------------------
Example 1
Input

7 10
0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 1 0
0 0 0 0 0 1 0 0 0 0
0 0 0 1 0 0 0 0 1 1
0 0 0 0 0 0 1 0 0 1
1 0 0 0 0 0 0 0 0 0
0 1 0 0 0 1 0 0 0 0

Output

2 1
4 2
5 1

---------------------
Example 2
Input

8 9
1 1 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0

Output

2 1
3 1
4 1
5 1
6 1
7 2

---------------------
Example 3
Input

6 6
1 0 0 0 1 0
0 1 0 1 0 1
0 0 1 0 0 0
0 1 0 1 0 0
1 0 0 0 1 0
0 1 0 0 0 1

Output

3 5
---------------------


'''


