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

public class bloxx_aa_ml implements Runnable {

	public static void main(String[] args) {
		new Thread(new bloxx_aa_ml()).start();
	}

	BufferedReader br;
	StringTokenizer st;
	PrintWriter out;
	boolean eof = false;
	Random rand = new Random(this.hashCode());

	@Override
	public void run() {
		Locale.setDefault(Locale.US);
		try {
			br = new BufferedReader(new FileReader(FNAME + ".in"));
			out = new PrintWriter(FNAME + ".out");
			solve();
			out.close();
		} catch (Throwable e) {
			e.printStackTrace();
			System.exit(566);
		}
	}

	String nextToken() {
		while (st == null || !st.hasMoreTokens()) {
			try {
				st = new StringTokenizer(br.readLine());
			} catch (Exception e) {
				eof = true;
				return "0";
			}
		}
		return st.nextToken();
	}

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

	void myAssert(boolean u, String message) {
		if (!u) {
			throw new Error("Assertion failed!!! " + message);
		}
	}

	int inBounds(int x, int l, int r, String name) {
		myAssert(l <= x && x <= r, name + " is not in bounds: " + x
				+ " is not in [" + l + ".." + r + "]");
		return x;
	}

	String FNAME = "bloxx";

	class Map {
		public Map(int[][] a) {
			buildings = new int[a.length * a[0].length];
			sum = 0;
			for (int i = 0; i < a.length; i++) {
				for (int j = 0; j < a[i].length; j++) {
					buildings[i * a[i].length + j] = a[i][j];
					sum += a[i][j];
				}
			}
			prev = null;
			lx = -1;
			ly = -1;
		}

		int[] buildings;
		int sum;
		Map prev;
		int lx, ly;

		@Override
		public int hashCode() {
			return Arrays.hashCode(buildings);
		}

		@Override
		public boolean equals(Object o) {
			return (o instanceof Map)
					&& (Arrays.equals(buildings, ((Map) o).buildings));
		}
	}

	private void solve() throws IOException {
		int n = inBounds(nextInt(), 1, 10, "m");
		int m = inBounds(nextInt(), 1, 10, "n");
		inBounds(n * m, 1, 16, "n * m");
		int[][] a = new int[n][m];
		for (int i = 0; i < a.length; i++) {
			for (int j = 0; j < a[i].length; j++) {
				a[i][j] = inBounds(nextInt(), 0, 5, "h[" + (i + 1) + "]["
						+ (j + 1) + "]");
			}
		}
		Map start = new Map(a);
		HashSet<Map> hs = new HashSet<Map>();
		hs.add(start);
		Queue<Map> q = new LinkedList<Map>();
		q.add(start);
		boolean[] has = new boolean[6];
		while (!q.isEmpty()) {
			Map s = q.poll();
			for (int i = 0; i < a.length; i++) {
				for (int j = 0; j < a[i].length; j++) {
					a[i][j] = s.buildings[i * a[i].length + j];
					// out.print(a[i][j]);
				}
				// out.println();
			}
			// out.println();
			for (int i = 0; i < a.length; i++) {
				for (int j = 0; j < a[i].length; j++) {
					if (a[i][j] == 0) {
						Arrays.fill(has, false);
						for (int dx = -1; dx < 2; dx++) {
							for (int dy = -1; dy < 2; dy++) {
								if (Math.abs(dx) + Math.abs(dy) == 1
										&& 0 <= i + dx && i + dx < a.length
										&& 0 <= j + dy && j + dy < a[i].length) {
									has[a[i + dx][j + dy]] = true;
								}
							}
						}
						int k = 1;
						do {
							a[i][j] = k;
							Map mp = new Map(a);
							if (!hs.contains(mp)) {
								mp.prev = s;
								mp.lx = i;
								mp.ly = j;
								hs.add(mp);
								q.add(mp);
							}
						} while (has[k++]);
						a[i][j] = 0;
					}
				}
			}
		}
		Map max = start;
		for (Map s : hs) {
			if (s.sum > max.sum) {
				max = s;
			}
		}
		out.println(max.sum);
		ArrayList<Integer> r = new ArrayList<Integer>();
		ArrayList<Integer> c = new ArrayList<Integer>();
		ArrayList<Integer> h = new ArrayList<Integer>();
		while (max.lx >= 0) {
			r.add(max.lx);
			c.add(max.ly);
			h.add(max.buildings[max.lx * m + max.ly]);
			max = max.prev;
		}
		Collections.reverse(r);
		Collections.reverse(c);
		Collections.reverse(h);
		out.println(r.size());
		for (int i = 0; i < r.size(); i++) {
			out.println((r.get(i) + 1) + " " + (c.get(i) + 1) + " " + h.get(i));
		}
	}
}
