Problem:
Score : 1000 points

Problem Statement

Note the unusual memory limit.

For a rectangular grid where each square is painted white or black, we define its complexity as follows:

If all the squares are black or all the squares are white, the complexity is 0.

Otherwise, divide the grid into two subgrids by a line parallel to one of the sides of the grid, and let c_1 and c_2 be the complexities of the subgrids. There can be multiple ways to perform the division, and let m be the minimum value of \max(c_1, c_2) in those divisions. The complexity of the grid is m+1.

You are given a grid with H horizontal rows and W vertical columns where each square is painted white or black.
HW characters from A_{11} to A_{HW} represent the colors of the squares.
A_{ij} is # if the square at the i-th row from the top and the j-th column from the left is black, and A_{ij} is . if that square is white.

Find the complexity of the given grid.

Constraints

1 \leq H,W \leq 185

A_{ij} is # or ..

Input

Input is given from Standard Input in the following format:

H W
A_{11}A_{12}...A_{1W}
:
A_{H1}A_{H2}...A_{HW}

Output

Print the complexity of the given grid.

Sample Input 1

3 3
...
.##
.##

Sample Output 1

2

Let us divide the grid by the boundary line between the first and second columns.
The subgrid consisting of the first column has the complexity of 0, and the subgrid consisting of the second and third columns has the complexity of 1, so the whole grid has the complexity of at most 2.

Sample Input 2

6 7
.####.#
#....#.
#....#.
#....#.
.####.#
#....##

Sample Output 2

4

Solution in C# (Mono 4.6.2.0) (judge verdict: Runtime Error)

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Remoting.Contexts;

namespace Contest
{
    class Program
    {
        private int H, W;
        private string[] A;
        private int[,,,] Memo;
        public void Solve()
        {
            var sc = new Scanner();
            H = sc.NextInt();
            W = sc.NextInt();
            A = new string[H];
            for (int i = 0; i < H; i++)
            {
                A[i] = sc.Next();
            }

            Memo = new int[H, W, H, W];
            Console.WriteLine(Comp(0, 0, H, W));
        }



        private int Comp(int aY, int aX, int bY, int bX)
        {
            //y;
            bool b = true;
            Dictionary<int, int> a;

            for (int yy = aY; yy < bY; yy++)
            {
                if (!b)
                {
                    break;
                }
                for (int xx = aX; xx < bX; xx++)
                {
                    if (A[yy][xx] != A[aY][aX])
                    {
                        b = false;
                        break;
                    }
                }
            }

            if (b) return 0;
            if (Memo[aY, aX, bY - 1, bX - 1] != 0)
            {
                return Memo[aY, aX, bY - 1, bX - 1];
            }
            int ans = int.MaxValue;
            if (bY - aY > 1)
            {
                for (int y = aY + 1; y < bY; y++)
                {
                    ans = Math.Min(ans, Math.Max(Comp(aY, aX, y, bX), Comp(y, aX, bY, bX)));
                }
            }

            if (bX - aX > 1)
            {
                for (int x = aX + 1; x < bX; x++)
                {
                    ans = Math.Min(ans, Math.Max(Comp(aY, aX, bY, x), Comp(aY, x, bY, bX)));
                }
            }

            Memo[aY, aX, bY - 1, bX - 1] = ans + 1;
            return ans + 1;
        }

        static void Main() => new Program().Solve();
    }
}

class Scanner
{
    public Scanner()
    {
        _stream = new StreamReader(Console.OpenStandardInput());
        _pos = 0;
        _line = new string[0];
        _separator = ' ';
    }
    private char _separator;
    private StreamReader _stream;
    private int _pos;
    private string[] _line;
    #region get a element
    public string Next()
    {
        if (_pos >= _line.Length)
        {
            _line = _stream.ReadLine().Split(_separator);
            _pos = 0;
        }
        return _line[_pos++];
    }
    public int NextInt()
    {
        return int.Parse(Next());
    }
    public long NextLong()
    {
        return long.Parse(Next());
    }
    public double NextDouble()
    {
        return double.Parse(Next());
    }
    #endregion
    #region convert array
    private int[] ToIntArray(string[] array)
    {
        var result = new int[array.Length];
        for (int i = 0; i < array.Length; i++)
        {
            result[i] = int.Parse(array[i]);
        }

        return result;
    }
    private long[] ToLongArray(string[] array)
    {
        var result = new long[array.Length];
        for (int i = 0; i < array.Length; i++)
        {
            result[i] = long.Parse(array[i]);
        }

        return result;
    }
    private double[] ToDoubleArray(string[] array)
    {
        var result = new double[array.Length];
        for (int i = 0; i < array.Length; i++)
        {
            result[i] = double.Parse(array[i]);
        }

        return result;
    }
    #endregion
    #region get array
    public string[] Array()
    {
        if (_pos >= _line.Length)
            _line = _stream.ReadLine().Split(_separator);
        _pos = _line.Length;
        return _line;
    }
    public int[] IntArray()
    {
        return ToIntArray(Array());
    }
    public long[] LongArray()
    {
        return ToLongArray(Array());
    }
    public double[] DoubleArray()
    {
        return ToDoubleArray(Array());
    }
    #endregion
}