Under construction

Still being built. Things may change

Cyclically Shifted Maze

Time limit2sMemory limit512 MB

Summary
Given a maze that was cyclically shifted by some rows and columns, list every (row, column) shift that restores a connected maze.
Level

Medium6 of 10

Topics
Matrix, BFS, Brute force
Solved
No attempts yet

Problem

Let's call a maze a rectangular grid of cells, where each cell is either empty or a wall. From an empty cell, you can move in four directions to an adjacent empty cell.

A maze is connected if every empty cell can be reached from every other empty cell by moving in four directions.

A connected maze of size n×mn \times m was cyclically shifted: some rows were moved down and some columns were moved right. The exact shifts are unknown. Find all possible shifts.

Input

The first line contains two integers nn and mm (1≤n,m≤2001 \le n, m \le 200), the size of the maze. Each of the next nn lines contains mm characters, either '.' for an empty cell or '#' for a wall. The maze has at least one empty cell.

Output

On the first line, print the number of possible shifts kk (0≤k≤n⋅m0 \le k \le n \cdot m). Each of the next kk lines contains two integers rir_i and cic_i (0≤ri<n0 \le r_i < n, 0≤ci<m0 \le c_i < m): the original maze was shifted down by rir_i rows and right by cic_i columns. The original maze must be connected for each listed pair. Print the pairs (ri,ci)(r_i, c_i) in lexicographical order.

Examples2

  1. Example 1

    Input
    5 6
    ..####
    .###..
    ...#.#
    ##...#
    .###..
    
    Expected output
    9
    0 2
    0 3
    0 4
    1 2
    1 3
    1 4
    4 2
    4 3
    4 4
    
  2. Example 2

    Input
    8 10
    ##########
    ..........
    #.####..##
    ..###..##.
    #....##...
    ######..##
    ....###...
    ....###...
    
    Expected output
    2
    0 5
    1 5