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

/**
 * Created by roi on 07.04.2016.
 */
public class Task5 {

    void solve(IOReader io) {
        n = io.nextInt();
        m = io.nextInt();

        field = new char[n * 2][m * 2];
        for (int i = 0; i < n; i++) {
            String line = io.readLine();
            for (int j = 0; j < m; j++) {
                char c = line.charAt(j);
                field[i][j] = field[i + n][j] = field[i][j + m] = field[i + n][j + m] = c;
            }
        }

        int dummy = 0;
        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < m - 1; j++) {
                if (check(i, j)) {
                    dummy++;
                }
            }
        }


        int[] row = new int[n];
        int[] col = new int[m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {

                if (check(i, j)) {
                    row[i]++;
                    col[j]++;
                }

            }
        }


        int ans = dummy;

        int prev = dummy;
        for (int i = 0; i < n - 1; i++) {
            int cur = prev;
            if (i > 0)
                for (int j = 0; j < m - 1; j++) {
                    if (check(i - 1, j)) {
                        cur--;
                    }

                    if (check(i + n - 2, j)) {
                        cur++;
                    }
                }


            int cc = cur;
            for (int j = 0; j < m; j++) {

                if (j > 0) {
//                    for (int ii = i; ii < i + n - 1; ii++) {
//                        if (check(ii, j - 1)) {
//                            cc--;
//                        }
//
//                        if (check(ii, j + m - 2)) {
//                            cc++;
//                        }
//                    }

                    cc -= col[j - 1];
                    if (check(i + n - 1, j - 1)) {
                        cc++;
                    }
                    cc += col[(j + m - 2) % m];
                    if (check(i + n - 1, (j + m - 2) % m)) {
                        cc--;
                    }

                }

                if (cc > ans) {
                    ans = cc;
                }

            }


            if (cur > ans) {
                ans = cur;
            }
            prev = cur;
        }

        io.println(ans);
    }

    int n, m;
    char[][] field;

    boolean check(int i, int j) {
//        System.err.println(i + " " + j);
        return field[i][j] == field[i + 1][j] && field[i][j] == field[i][j + 1] && field[i][j] == field[i + 1][j + 1];
    }




    public static void main(String[] args) {
        Task5 solver = new Task5();
        IOReader io = new IOReader("video");
        solver.solve(io);
        io.out.close();
    }

    static class IOReader {
        BufferedReader in;
        StringTokenizer tok;
        PrintWriter out;

        IOReader(String file) {
            try {
                in = new BufferedReader(new FileReader(file + ".in"));
                out = new PrintWriter(file + ".out");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        String nextToken() {
            if (tok == null || !tok.hasMoreTokens()) {
                try {
                    tok = new StringTokenizer(in.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                    return null;
                }
            }
            return tok.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(nextToken());
        }

        String readLine() {
            try {
                return in.readLine();
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }

        void println(long x) {
            out.println(x);
        }
    }

}
