Language: C++

#pragma GCC optimize(3)
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/priority_queue.hpp>
#define MAXN 190
#define INF 1000000000
#define MOD 1000000007
#define F first
#define S second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> P;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
typedef __gnu_pbds::priority_queue<int,greater<int>,pairing_heap_tag> pq;
int n,m;
char mp[MAXN][MAXN];
int toright[MAXN][MAXN],todown[MAXN][MAXN];
short row[12][MAXN][MAXN][MAXN],col[12][MAXN][MAXN][MAXN];
void update(int &a,int b) {a=max(a,b);}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++) scanf("%s",mp[i]+1);
    for(int i=1;i<=n;i++)
    {
        toright[i][m]=m;
        for(int j=m-1;j>=1;j--)
        {
            if(mp[i][j]==mp[i][j+1]) toright[i][j]=toright[i][j+1];
            else toright[i][j]=j;
        }
    }
    for(int i=1;i<=m;i++)
    {
        todown[n][i]=n;
        for(int j=n-1;j>=1;j--)
        {
            if(mp[j][i]==mp[j+1][i]) todown[j][i]=todown[j+1][i];
            else todown[j][i]=j;
        }
    }
    for(int i=0;i<=10;i++)
        for(int j=1;j<=m;j++)
            for(int l=1;l<=n;l++)
                for(int r=l;r<=n;r++)
                    row[i][j][l][r]=-1;
    for(int i=0;i<=10;i++)
        for(int j=1;j<=n;j++)
            for(int l=1;l<=m;l++)
                for(int r=l;r<=m;r++)
                    col[i][j][l][r]=-1;
    for(int pos=1;pos<=m;pos++)
    {
        for(int l=1;l<=n;l++)
        {
            row[0][pos][l][l]=toright[l][pos];
            for(int r=l+1;r<=n;r++)
            {
                if(r>l&&mp[r-1][pos]!=mp[r][pos]) break;
                row[0][pos][l][r]=min(row[0][pos][l][r-1],toright[r][pos]);
            }
        }
    }
    for(int pos=1;pos<=n;pos++)
    {
        for(int l=1;l<=m;l++)
        {
            col[0][pos][l][l]=todown[pos][l];
            for(int r=l+1;r<=m;r++)
            {
                if(r>l&&mp[pos][r-1]!=mp[pos][r]) break;
                col[0][pos][l][r]=min(col[0][pos][l][r-1],todown[pos][r]);
            }
        }
    }
    for(int turns=0;turns<=10;turns++)
    {
        if(row[turns][1][1][n]==m)
        {
            printf("%d\n",turns);
            return 0;
        }
        for(int pos=1;pos<=m;pos++)
        {
            for(int l=1;l<=n;l++)
            {
                for(int r=l;r<=n;r++)
                {
                    if(row[turns][pos][l][r]==-1) continue;
                    int val=row[turns][pos][l][r];
                    update(row[turns+1][pos][l][r],val);
                    int rightest=max(val,row[turns][val+1][l][r]);
                    update(row[turns+1][pos][l][r],rightest);
                    update(col[turns+1][l][pos][rightest],r);
                }
            }
        }
        for(int pos=1;pos<=n;pos++)
        {
            for(int l=1;l<=m;l++)
            {
                for(int r=l;r<=m;r++)
                {
                    if(col[turns][pos][l][r]==-1) continue;
                    int val=col[turns][pos][l][r];
                    update(col[turns+1][pos][l][r],val);
                    int downest=max(val,col[turns][val+1][l][r]);
                    update(col[turns+1][pos][l][r],downest);
                    update(row[turns+1][l][pos][downest],r);
                }
            }
        }
    }
    return 0;
}



Compiler output:
a.cc: In function 'int main()':
a.cc:62:38: error: no matching function for call to 'min(short int&, int&)'
   62 |                 row[0][pos][l][r]=min(row[0][pos][l][r-1],toright[r][pos]);
      |                                   ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:2:
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:62:38: note:   deduced conflicting types for parameter 'const _Tp' ('short int' and 'int')
   62 |                 row[0][pos][l][r]=min(row[0][pos][l][r-1],toright[r][pos]);
      |                                   ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:62:38: note:   mismatched types 'std::initializer_list<_Tp>' and 'short int'
   62 |                 row[0][pos][l][r]=min(row[0][pos][l][r-1],toright[r][pos]);
      |                                   ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:74:38: error: no matching function for call to 'min(short int&, int&)'
   74 |                 col[0][pos][l][r]=min(col[0][pos][l][r-1],todown[pos][r]);
      |                                   ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:74:38: note:   deduced conflicting types for parameter 'const _Tp' ('short int' and 'int')
   74 |                 col[0][pos][l][r]=min(col[0][pos][l][r-1],todown[pos][r]);
      |                                   ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:74:38: note:   mismatched types 'std::initializer_list<_Tp>' and 'short int'
   74 |                 col[0][pos][l][r]=min(col[0][pos][l][r-1],todown[pos][r]);
      |                                   ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:93:50: error: cannot bind non-const lvalue reference of type 'int&' to a value of type 'short int'
   93 |                     update(row[turns+1][pos][l][r],val);
      |                            ~~~~~~~~~~~~~~~~~~~~~~^
a.cc:21:18: note:   initializing argument 1 of 'void update(int&, int)'
   21 | void update(int &a,int b) {a=max(a,b);}
      |             ~~~~~^
a.cc:94:37: error: no matching function for call to 'max(int&, short int&)'
   94 |                     int rightest=max(val,row[turns][val+1][l][r]);
      |                                  ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:94:37: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'short int')
   94 |                     int rightest=max(val,row[turns][val+1][l][r]);
      |                                  ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:94:37: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   94 |                     int rightest=max(val,row[turns][val+1][l][r]);
      |                                  ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:95:50: error: cannot bind non-const lvalue reference of type 'int&' to a value of type 'short int'
   95 |                     update(row[turns+1][pos][l][r],rightest);
      |                            ~~~~~~~~~~~~~~~~~~~~~~^
a.cc:21:18: note:   initializing argument 1 of 'void update(int&, int)'
   21 | void update(int &a,int b) {a=max(a,b);}
      |             ~~~~~^
a.cc:96:57: error: cannot bind non-const lvalue reference of type 'int&' to a value of type 'short int'
   96 |                     update(col[turns+1][l][pos][rightest],r);
      |                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
a.cc:21:18: note:   initializing argument 1 of 'void update(int&, int)'
   21 | void update(int &a,int b) {a=max(a,b);}
      |             ~~~~~^
a.cc:108:50: error: cannot bind non-const lvalue reference of type 'int&' to a value of type 'short int'
  108 |                     update(col[turns+1][pos][l][r],val);
      |                            ~~~~~~~~~~~~~~~~~~~~~~^
a.cc:21:18: note:   initializing argument 1 of 'void update(int&, int)'
   21 | void update(int &a,int b) {a=max(a,b);}
      |             ~~~~~^
a.cc:109:36: error: no matching function for call to 'max(int&, short int&)'
  109 |                     int downest=max(val,col[turns][val+1][l][r]);
      |                                 ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:109:36: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'short int')
  109 |                     int downest=max(val,col[turns][val+1][l][r]);
      |                                 ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:109:36: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
  109 |                     int downest=max(val,col[turns][val+1][l][r]);
      |                                 ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:110:50: error: cannot bind non-const lvalue reference of type 'int&' to a value of type 'short int'
  110 |                     update(col[turns+1][pos][l][r],downest);
      |                            ~~~~~~~~~~~~~~~~~~~~~~^
a.cc:21:18: note:   initializing argument 1 of 'void update(int&, int)'
   21 | void update(int &a,int b) {a=max(a,b);}
      |             ~~~~~^
a.cc:111:56: error: cannot bind non-const lvalue reference of type 'int&' to a value of type 'short int'
  111 |                     update(r