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

public class death_ak_n2c2 {

    final static int[] DX = {0, 1, 0, -1};
    final static int[] DY = {1, 0, -1, 0};

    static int[][] used;
    static int revision;
    static int[][] a;
    static int n, m;

    static boolean inBounds(int x, int y) {
        return 0 <= x && x < n && 0 <= y && y < m;
    }

    int dfs(int x, int y, int c1, int c2) {
        if (used[x][y] == revision)
            return 0;
        used[x][y] = revision;
        int res = 1;
        for (int dir = 0; dir < 4; dir++) {
            int x2 = x + DX[dir];
            int y2 = y + DY[dir];
            if (inBounds(x2, y2) && (a[x2][y2] == c1 || a[x2][y2] == c2)) {
                res += dfs(x2, y2, c1, c2);
            }
        }
        return res;
    }

    int count(int c1, int c2) {
        n = a.length;
        m = a[0].length;
        revision++;
        int res = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (used[i][j] != revision && (a[i][j] == c1 || a[i][j] == c2)) {
                    int now = dfs(i, j, c1, c2);
                    res = Math.max(now, res);
                }
            }
        }
        return res;
    }

    void solve() throws IOException {
        n = in.nextInt();
        m = in.nextInt();
        a = new int[n][m];
        HashSet<Integer> colors = new HashSet<>();
        revision = 1;
        used = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                a[i][j] = in.nextInt();
                colors.add(a[i][j]);
            }
        }
        int ans = 0;
        int c1Ans = -1, c2Ans = -1;
        for (int c1 : colors) {
            for (int c2 : colors) {
                int now = count(c1, c2);
                if (now > ans) {
                    ans = now;
                    c1Ans = c1;
                    c2Ans = c2;
                }
            }
        }
        out.println(ans);
        out.println(c1Ans + " " + c2Ans);
    }

    static MyScanner in;
    static PrintWriter out;

    public static void main(String[] args) throws IOException {
        in = new MyScanner(new FileReader("death.in"));
        out = new PrintWriter("death.out");
        new death_ak_n2c2().solve();
        out.close();
    }

    static class MyScanner {
        private StringTokenizer st;
        private BufferedReader br;

        public MyScanner(Reader r) {
            br = new BufferedReader(r);
        }

        public 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());
        }
    }


}
