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

public class death_pk {

	PrintWriter out;
	BufferedReader br;
	StringTokenizer st;

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

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

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

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

	class Pair {
		int x, y;

		public Pair(int x, int y) {
			super();
			this.x = x;
			this.y = y;
		}

	}

	class Vertex {
		int number;
		long color;
		int c;
		
		int size;
		Vertex par;

		public Vertex(int number, long color, int size, int c) {
			super();
			this.number = number;
			this.color = color;
			this.size = size;
			this.c = c;
			if (size > ans) {
				ans = size;
				lA = c;
				rA = c;
			}
			par = this;
		}

		Vertex get() {
			if (par != this)
				par = par.get();
			return par;
		}
	}

	Random rnd;

	int ans = Integer.MIN_VALUE;
	int lA, rA;
	
	void union(Vertex a, Vertex b) {
		a = a.get();
		b = b.get();
		if (a == b)
			return;
		if (rnd.nextBoolean()) {
			a.par = b;
			b.size += a.size;
		} else {
			b.par = a;
			a.size += b.size;
		}
		if (a.par.size > ans) {
			ans = a.par.size;
			lA = (int) (a.color / (int)1e9);
			rA = (int) (a.color % (int)1e9);
		}
	}

	public void solve() throws IOException {
		rnd = new Random(31);
		int n = nextInt();
		int m = nextInt();
		int[][] a = new int[n][m];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				a[i][j] = nextInt();
			}
		}

		int[][] color = new int[n][m];
		int cnt = 0;
		ArrayList<Integer> size = new ArrayList<Integer>();
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (color[i][j] == 0) {
					cnt++;
					ArrayDeque<Pair> q = new ArrayDeque<death_pk.Pair>();
					q.add(new Pair(i, j));
					color[i][j] = cnt;
					size.add(0);
					while (!q.isEmpty()) {
						size.set(cnt - 1, size.get(cnt - 1) + 1);
						Pair t = q.removeFirst();
						for (int di = -1; di < 2; di++) {
							for (int dj = -1; dj < 2; dj++) {
								if (!inBounds(t.x, t.y, di, dj, n, m))
									continue;
								if (a[t.x + di][t.y + dj] != a[i][j]
										|| color[t.x + di][t.y + dj] != 0)
									continue;
								color[t.x + di][t.y + dj] = cnt;
								q.addLast(new Pair(t.x + di, t.y + dj));
							}
						}
					}
					if (size.get(cnt - 1) > ans) {
						ans = size.get(cnt - 1);
						lA = a[i][j];
						rA = a[i][j];
					}
				}
			}
		}

		HashMap<Long, Vertex>[] map = new HashMap[cnt];
		for (int i = 0; i < cnt; i++) {
			map[i] = new HashMap<Long, death_pk.Vertex>();
		}

		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (i < n - 1 && a[i][j] != a[i + 1][j]) {
					int l = color[i][j] - 1;
					int r = color[i + 1][j] - 1;
					long c = gen(a[i][j], a[i + 1][j]);
					if (!map[l].containsKey(c))
						map[l].put(c, new Vertex(l, c, size.get(l), a[i][j]));
					if (!map[r].containsKey(c))
						map[r].put(c, new Vertex(r, c, size.get(r), a[i+1][j]));
					union(map[l].get(c), map[r].get(c));
				}
				
				if (j < m - 1 && a[i][j] != a[i][j + 1]) {
					int l = color[i][j] - 1;
					int r = color[i][j + 1] - 1;
					long c = gen(a[i][j], a[i][j + 1]);
					if (!map[l].containsKey(c))
						map[l].put(c, new Vertex(l, c, size.get(l), a[i][j]));
					if (!map[r].containsKey(c))
						map[r].put(c, new Vertex(r, c, size.get(r), a[i][j + 1]));
					union(map[l].get(c), map[r].get(c));
				}
			}
		}
		
		out.println(ans);
		out.println(lA + " " + rA);
	}

	private boolean inBounds(int x, int y, int di, int dj, int n, int m) {
		return Math.abs(di + dj) == 1 && di * dj == 0 && x + di >= 0 && x + di < n && y + dj >= 0 && y + dj < m;
	}

	private long gen(int i, int j) {
		if (i > j) {
			return (long)1e9 * i + j;
		} else {
			return (long)1e9 * j + i;
		}
	}

	public void run() {
		try {
			br = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out);

			br = new BufferedReader(new FileReader("death.in"));
			out = new PrintWriter("death.out");

			solve();

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

	public static void main(String[] args) {
		new death_pk().run();
	}
}
