// Задача "Игра <<Bloxx city>>"
// 4-я Интернет-олимпиада сезона 2009-2010, усложненный уровень.
// Автор задачи: Владимир Ульянцев, ulyantsev@rain.ifmo.ru
// Автор решения: Владимир Ульянцев, ulyantsev@rain.ifmo.ru

uses
	sysutils;
const
	MAXN = 10;
	MAXM = 25;
type
	int = longint;
var
	n, m : int;
	i, j : int;
	a : array [0..MAXN + 1, 0..MAXN + 1] of int;
	was : array [0..MAXN + 1, 0..MAXN + 1] of boolean;
	
	maxHeight, cnt : int;
	ans : array [0..MAXN + 1, 0..MAXN + 1] of int;
	
	numbers : array [0..100] of boolean;

function isValid(x, y : int) : boolean;
var
	k : int;
begin
	fillchar(numbers, sizeof(numbers), 0);
	numbers[a[x - 1][y]] := true;
	numbers[a[x][y - 1]] := true;
	numbers[a[x + 1][y]] := true;
	numbers[a[x][y + 1]] := true;

	result := true;	
	for k := 1 to a[x][y] - 1 do begin
		if (not numbers[k]) then begin
			result := false;
		end;
	end;	
end;

procedure calcSituation();
var
	i, j, currentHeight : int;
begin
	currentHeight := 0;
	for i := 1 to n do begin
		for j := 1 to m do begin
			currentHeight := currentHeight + a[i][j];
			//writeln(currentHeight);
			if (not was[i][j]) and (not isValid(i, j)) then begin
				exit;
			end;
		end;
	end;

	if (currentHeight > maxHeight) then begin
		//writeln(currentHeight);
		maxHeight := currentHeight;
		for i := 1 to n do begin
			for j := 1 to m do begin
				ans[i][j] := a[i][j];
			//write(a[i][j], ' ');
			end;
		//writeln;
		end;
	end;
end;

procedure dfs(x, y : int);
var
	i : int;
begin
	if ((x = n + 1) and (y = 1)) then begin
		calcSituation();		
	end else if (was[x][y]) then begin
		if (y = m) then begin
			dfs(x + 1, 1);
		end else begin
			dfs(x, y + 1);
		end;
	end else begin
		for i := 1 to 5 do begin
			a[x][y] := i;
			//if ((y > 1) and )

			if (y = m) then begin
				dfs(x + 1, 1);
			end else begin
				dfs(x, y + 1);
			end;

			a[x][y] := 0;
		end;
	end;
end;

procedure writeAnswer();
var
	i, j, k : int;
begin
	writeln(maxHeight);
	writeln(cnt);
	
	for k := 1 to 5 do begin
		for i := 1 to n do begin
			for j := 1 to m do begin
				if ((not was[i][j]) and (ans[i][j] = k)) then begin
					writeln(i, ' ', j, ' ', ans[i][j]);
				end;
			end;
		end;
	end;
	{
	writeln;
	for i := 1 to n do begin
		for j := 1 to m do begin
			write(ans[i][j], ' ');
		end;
		writeln;
	end;
	}

end;

begin
	reset(input, 'bloxx.in');
	rewrite(output, 'bloxx.out');

	read(n, m);
	assert((1 <= n) and (n <= MAXN));
	assert((1 <= m) and (m <= MAXN));
	assert(m * n <= MAXM);

	fillchar(a, sizeof(a), 0);
	fillchar(ans, sizeof(ans), 0);

	cnt := m * n;
	for i := 1 to n do begin
		for j := 1 to m do begin
			read(a[i][j]);
			if (a[i][j] > 0) then begin
				was[i][j] := true;
				cnt := cnt - 1;
			end;
		end;
	end;

	maxHeight := 0;
	dfs(1, 1);
	writeAnswer();	
end.
