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

public class a1 {
	String fileName = "video";

	public void solve() throws IOException {
		int n = nextInt();
		int m = nextInt();

		int a[][] = new int[n][m];

		for (int i = 0; i < n; i++) {
			String s = nextToken();

			for (int j = 0; j < m; j++) {
				a[i][j] = (int) s.charAt(j) - (int) '0';
			}
		}

		long sum = 0;

		long cnt1[] = new long[n];
		long cnt2[] = new long[m];

		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				int i1 = (i + 1) % n;
				int j1 = (j + 1) % m;

				if (a[i][j] == a[i1][j] && a[i1][j] == a[i1][j1] && a[i1][j1] == a[i][j1]) {
					sum++;
					cnt1[i]++;
					cnt2[j]++;
				}
			}
		}

		long ans = 0;

		for (int i = 0; i < n; i++) {
			int cur = 0;

			for (int j = 0; j < m; j++) {
				int i1 = (i + 1) % n;
				int j1 = (j + 1) % m;

				int add = 0;
				
				if (a[i][j] == a[i1][j] && a[i1][j] == a[i1][j1] && a[i1][j1] == a[i][j1]) {
					add++;
				}
				
				ans = Math.max(ans, sum + add - cnt1[i] - cnt2[j]);
			}

		}
		
		out.println(ans);
	}

	public void run() {
		try {
			br = new BufferedReader(new FileReader(fileName + ".in"));
			out = new PrintWriter(fileName + ".out");

			solve();

			out.close();
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(1);
		}
	}

	BufferedReader br;
	StringTokenizer in;
	PrintWriter out;

	public String nextToken() throws IOException {
		while (in == null || !in.hasMoreTokens()) {
			in = new StringTokenizer(br.readLine());
		}
		return in.nextToken();
	}

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

	public double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}

	public long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	public static void main(String[] args) throws IOException {
		Locale.setDefault(Locale.US);
		new a1().run();
	}

}