Language: C++


import java.util.*;
import java.io.*;

public class Main {
    public static void main(String [] args) {
        Solver s = new Solver();
        s.solve();
    }
}

class Solver {
    Reader in = new Reader ();
    Writer out = new Writer ();
    final int logn = 17;
    char [][] s = new char [200][200];
    int n, m;
    int lr [][][][] = new int [2][200][200][200]; 
    int ud [][][][] = new int [2][200][200][200];
    int rowCon[][] = new int [200][200];
    int colCon[][] = new int [200][200];

    void solve () {
        n = in.nextInt();
        m = in.nextInt();
        
        for(int i = 0; i < n; i++) {
            char txt[] = in.next().toCharArray();
            for(int j = 0; j < m; j++) {
                s[i][j] = txt[j];
            }
        }
        // need to calc x = 0

        int c = 0;
        int ans = -1;

        for(int i = 0; i < n; i++) {
            for(int j = m-1; j >= 0; j--) {
                rowCon[i][j] = 1;
                if(j + 1 < m && s[i][j] == s[i][j + 1]) {
                    rowCon[i][j] += rowCon[i][j + 1];
                }
            }
        }
        for(int j = 0; j < m; j++) {
            for(int i = n-1; i >= 0; i--) {
                colCon[i][j] = 1;
                if(i + 1 < n && s[i][j] == s[i + 1][j]) {
                    colCon[i][j] += colCon[i + 1][j];
                }
            }
        }
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < m; j++) {
                lr[c][i][j][j] = colCon[i][j];
                for(int k = j + 1; k < m; k++) {
                    if(s[i][k] == s[i][k - 1]) {
                        lr[c][i][j][k] = Math.min(lr[c][i][j][k - 1], colCon[i][k]); 
                    } else {
                        lr[c][i][j][k] = 0;
                    }
                }
            }
        }
        for(int i = 0; i < m; i++) {
            for(int j = 0; j < n; j++) {
                ud[c][i][j][j] = rowCon[i][j];
                for(int k = j + 1; k < n; k++) {
                    if(s[k][i] == s[k - 1][i]) {
                        ud[c][i][j][k] = Math.min(ud[c][i][j][k - 1], rowCon[k][i]);
                    } else {
                        ud[c][i][j][k] = 0;
                    }
                }
            }
        }
        for(int x = 1; x <= logn; x++) {
            if(lr[c][0][0][m-1] == n) {
                ans = x-1;
                break;
            }
            c ^= 1;
            for(int i = 0; i < n; i++) {
                for(int j = 0; j < m; j++) {
                    int now = n - 1;
                    for(int k = j; k < m; k++) {
                        int shift = lr[c ^ 1][i][j][k];
                        lr[c][i][j][k] = shift + lr[c ^ 1][i + shift][j][k];
                        while(now >= i && 
                            ud[c ^ 1][j][i][now] + ud[c ^ 1][j + ud[c ^ 1][j][i][now]][i][now] < (k - j + 1)) {
                            --now;
                        }
                        lr[c][i][j][k] = Math.max(lr[c][i][j][k], now - i + 1);
                    }
                }   
            }
            for(int i = 0; i < m; i++) {
                for(int j = 0; j < n; j++) {
                    int now = m - 1;
                    for(int k = j; k < n; k++) {
                        int shift = ud[c ^ 1][i][j][k];
                        ud[c][i][j][k] = shift + ud[c ^ 1][i + shift][j][k];
                        while(now >= i &&
                            lr[c ^ 1][j][i][now] + lr[c ^ 1][j + lr[c ^ 1][j][i][now]][i][now] < (k - j + 1)) {
                            --now;
                        }
                        ud[c][i][j][k] = Math.max(ud[c][i][j][k], now - i + 1);
                    }
                }
            }
            if(lr[c][0][0][m-1] == n) {
                ans = x;
                break;
            }
        }    
        System.out.println(ans);
    }
}

class Reader {
    private StringTokenizer a;
    private BufferedReader b;
    Reader () {
        a = null;
        try {
            b = new BufferedReader (new InputStreamReader (System.in)); // for file IO, replace this with new FileReader ("in.txt")
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public String next () {
        while(a == null || !a.hasMoreTokens()) {
            try {
                a = new StringTokenizer (b.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return a.nextToken();
    }
    public int nextInt() {
        return Integer.parseInt(this.next());
    }
    public long nextLong () {
        return Long.parseLong(this.next());
    }
    public double nextDouble () {
        return Double.parseDouble(this.next());
    }
    public String nextLine() {
        try {
            return b.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
}
class Writer {
    private PrintWriter a;
    private StringBuffer b;
    Writer () {
        try {
            a = new PrintWriter (System.out); // for file IO, replace this with new FileWriter ("out.txt")
        } catch (Exception e) {
            e.printStackTrace();
        }
        b = new StringBuffer ("");
    }
    public void write (Object s) {
        b.append(s);
    }
    public void writeln(Object s) {
        b.append(s).append('\n');
    }
    public void flush () {
        a.print(b);
        a.flush();
        a.close();
    }
}
class Pair implements Comparator <Pair> {
    int first;
    int second;
    Pair  (int a, int b) {
        this.first = a;
        this.second = b;
    }
    Pair (Pair a) {
        this.first = a.first;
        this.second = a.second;
    }
    Pair () {}
    public String toString () {
        return "[" + first + ", " + second + "]";
    }
    public int compare (Pair a, Pair b) {
        if(a.first == b.first) {
            return a.second - b.second;
        } else {
            return a.first - b.first;
        }
    }
}


Compiler output:
a.cc:2:1: error: 'import' does not name a type
    2 | import java.util.*;
      | ^~~~~~
a.cc:2:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:3:1: error: 'import' does not name a type
    3 | import java.io.*;
      | ^~~~~~
a.cc:3:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:5:1: error: expected unqualified-id before 'public'
    5 | public class Main {
      | ^~~~~~
a.cc:13:5: error: 'Reader' does not name a type
   13 |     Reader in = new Reader ();
      |     ^~~~~~
a.cc:14:5: error: 'Writer' does not name a type
   14 |     Writer out = new Writer ();
      |     ^~~~~~
a.cc:15:5: error: 'final' does not name a type
   15 |     final int logn = 17;
      |     ^~~~~
a.cc:16:10: error: expected unqualified-id before '[' token
   16 |     char [][] s = new char [200][200];
      |          ^
a.cc:18:9: error: declaration of 'lr' as multidimensional array must have bounds for all dimensions except the first
   18 |     int lr [][][][] = new int [2][200][200][200];
      |         ^~
a.cc:19:9: error: declaration of 'ud' as multidimensional array must have bounds for all dimensions except the first
   19 |     int ud [][][][] = new int [2][200][200][200];
      |         ^~
a.cc:20:9: error: declaration of 'rowCon' as multidimensional array must have bounds for all dimensions except the first
   20 |     int rowCon[][] = new int [200][200];
      |         ^~~~~~
a.cc:21:9: error: declaration of 'colCon' as multidimensional array must have bounds for all dimensions except the first
   21 |     int colCon[][] = new int [200][200];
      |         ^~~~~~
a.cc:119:2: error: expected ';' after class definition
  119 | }
      |  ^
      |  ;
a.cc: In member function 'void Solver::solve()':
a.cc:24:13: error: 'in' was not declared in this scope; did you mean 'n'?
   24 |         n = in.nextInt();
      |             ^~
      |             n
a.cc:30:17: error: 's' was not declared in this scope
   30 |                 s[i][j] = txt[j];
      |                 ^
a.cc:40:17: error: 'rowCon' was not declared in this scope
   40 |                 rowCon[i][j] = 1;
      |                 ^~~~~~
a.cc:41:33: error: 's' was not declared in this scope
   41 |                 if(j + 1 < m && s[i][j] == s[i][j + 1]) {
      |                                 ^
a.cc:48:17: error: 'colCon' was not declared in this scope
   48 |                 colCon[i][j] = 1;
      |                 ^~~~~~
a.cc:49:33: error: 's' was not declared in this scope
   49 |                 if(i + 1 < n && s[i][j] == s[i + 1][j]) {
      |                                 ^
a.cc:56:17: error: 'lr' was not declared in this scope
   56 |                 lr[c][i][j][j] = colCon[i][j];
      |                 ^~
a.cc:56:34: error: 'colCon' was not declared in this scope
   56 |                 lr[c][i][j][j] = colCon[i][j];
      |                                  ^~~~~~
a.cc:58:24: error: 's' was not declared in this scope
   58 |                     if(s[i][k] == s[i][k - 1]) {
      |                        ^
a.cc:59:42: error: 'Math' was not declared in this scope
   59 |                         lr[c][i][j][k] = Math.min(lr[c][i][j][k - 1], colCon[i][k]);
      |                                          ^~~~
a.cc:68:17: error: 'ud' was not declared in this scope
   68 |                 ud[c][i][j][j] = rowCon[i][j];
      |                 ^~
a.cc:68:34: error: 'rowCon' was not declared in this scope
   68 |                 ud[c][i][j][j] = rowCon[i][j];
      |                                  ^~~~~~
a.cc:70:24: error: 's' was not declared in this scope
   70 |                     if(s[k][i] == s[k - 1][i]) {
      |                        ^
a.cc:71:42: error: 'Math' was not declared in this scope
   71 |                         ud[c][i][j][k] = Math.min(ud[c][i][j][k - 1], rowCon[k][i]);
      |                                          ^~~~
a.cc:78:29: error: 'logn' was not declared in this scope; did you mean 'long'?
   78 |         for(int x = 1; x <= logn; x++) {
      |                             ^~~~
      |                             long
a.cc:79:16: error: 'lr' was not declared in this scope
   79 |             if(lr[c][0][0][m-1] == n) {
      |                ^~
a.cc:88:37: error: 'lr' was not declared in this scope
   88 |                         int shift = lr[c ^ 1][i][j][k];
      |                                     ^~
a.cc:91:29: error: 'ud' was not declared in this scope
   91 |                             ud[c ^ 1][j][i][now] + ud[c ^ 1][j + ud[c ^ 1][j][i][now]][i][now] < (k - j + 1)) {
      |                             ^~
a.cc:94:42: error: 'Math' was not declared in this scope
   94 |                         lr[c][i][j][k] = Math.max(lr[c][i][j][k], now - i + 1);
      |                                          ^~~~
a.cc:102:37: error: 'ud' was not declared in this scope
  102 |                         int shift = ud[c ^ 1][i][j][k];
      |                                     ^~
a.cc:105:29: error: 'lr' was not declared in this scope
  105 |                             lr[c ^ 1][j][i][now] + lr[c ^ 1][j + lr[c ^ 1][j][i][now]][i][now] < (k - j + 1)) {
      |                             ^~
a.cc:108:42: error: 'Math' was not declared in this scope
  108 |                         ud[c][i][j][k] = Math.max(ud[c][i][j][k], now - i + 1);
      |                                          ^~~~
a.cc:112:16: error: 'lr' was not declared in this scope
  112 |             if(lr[c][0][0][m-1] == n) {
      |                ^~
a.cc:117:9: error: 'System' was not declared in this scope
  117 |         System.out.println(ans);
      |         ^~~~~~
a.cc: At global scope:
a.cc:122:12: error: expected ':' before 'StringTokenizer'
  122 |     private StringTokenizer a;
      |            ^~~~~~~~~~~~~~~~
      |            :
a.cc:122:13: error: 'StringTokenizer' does not name a type
  122 |     private StringTokenizer a;
      |             ^~~~~~~~~~~~~~~
a.cc:123:12: error: expected ':' before 'BufferedReader'
  123 |     private BufferedReader b;
      |            ^~~~~~~~~~~~~~~
      |            :
a.cc:123:13: error: 'BufferedReader' does not name a type
  123 |     private BufferedReader b;
      |             ^~~~~~~~~~~~~~
a.cc:132:11: error: expected ':' before 'String'
  132 |     public String next () {
      |           ^~~~~~~
      |           :
a.cc:132:12: error: 'String' does not name a type
  132 |     public String next () {
      |            ^~~~~~
a.cc:142:11: error: expected ':' before 'int'
  142 |     public int nextInt() {
      |           ^~~~
      |           :
a.cc:145:11: error: expected ':' before 'long'
  145 |     public long nextLong () {
      |           ^~~~~
      |           :
a.cc:148:11: error: expected ':' before 'double'
  148 |     public double nextDouble () {
      |           ^~~~~~~
      |           :
a.cc:151:11: error: expected ':' before 'String'
  151 |     public String nextLine() {
      |           ^~~~~~~
      |           :
a.cc:151:12: error: 'String' does not name a type
  151 |     public String nextLine() {
      |            ^~~~~~
a.cc:159:2: error: expected ';' after class definition
  159 | }
      |  ^
      |  ;
a.cc: In constructor 'Reader::Reader()':
a.cc:125:9: error: 'a' was not declared in this scope
  125 |         a = null;
      |         ^
a.cc:125:13: error: 'null' was not declared in this scope
  125 |         a = null;
      |             ^~~~
a.cc:127:13: error: 'b' was not declared in this scope
  127 |             b = new BufferedReader (new InputStreamReader (System.in)); // for file IO, replace this with new FileReader ("in.txt")
      |             ^
a.cc:127:21: error: expected type-specifier before 'BufferedReader'
  127 |             b = new BufferedReader (new InputStreamReader (System.in)); // for file IO, replace this with new FileReader ("in.txt")
      |                     ^~~~~~~~~~~~~~
a.cc:128:18: error: 'Exception' does not name a type
  128 |         } catch (Exception e) {
      |                  ^~~~~~~~~
a.cc:129:13: error: 'e' was not declared in this scope
  129 |             e.printStackTrace();
      |             ^
a.cc: In member function 'int Reader::nextInt()':
a.cc:143:16: error: 'Integer' was not declared in this scope
  143 |         return Integer.parseInt(this.next());
      |                ^~~~~~~
a.cc:143:38: error: request for member 'next' in '(Reader*)this', which is of pointer type 'Reader*' (maybe you meant to use '->' ?)
  143 |         return Integer.parseInt(this.next());
      |                                      ^~~~
a.cc: In member function 'long int Reader::nextLong()':
a.cc:146:16: error: 'Long' was not declared in this scope; did you mean 'long'?
  146 |         return Long.parseLong(this.next());
      |                ^~~~
      |                long
a.cc:146:36: error: request for member 'next' in '(Reader*)this', which is of pointer type 'Reader*' (maybe you meant to use '->' ?)
  146 |         return Long.parseLong(this.next());
      |                                    ^~~~
a.cc: In member function 'double Reader::nextDouble()':
a.cc:149:16: error: 'Double' was not declared in this scope; did you mean 'double'?
  149 |         return Double.parseDouble(this.next());
      |                ^~~~~~
      |                double
a.cc:149:40: error: request for member 'next' in '(Reader*)this', which is of pointer type 'Reader*' (maybe you meant to use '->' ?)
  149 |         return Double.parseDouble(this.next());
      |                                        ^~~~
a.cc: At global scope:
a.cc:161:12: error: expected ':' before 'PrintWriter'
  161 |     private PrintWriter a;
      |            ^~~~~~~~~~~~
      |            :
a.cc:161:13: error: 'PrintWriter' does not name a type
  161 |     private PrintWriter a;
      |             ^~~~~~~~~~~
a.cc:162:12: error: expected ':' before 'StringBuffer'
  162 |     private StringBuffer b;
      |            ^~~~~~~~~~~~~
      |            :
a.cc:162:13: error: 'StringBuffer' does not name a type
  162 |     private StringBuffer b;
      |             ^~~~~~~~~~~~
a.cc:171:11: error: expected ':' before 'void'
  171 |     public void write (Object s