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

{$R+} {$O+} {$Q-}
uses
	sysutils;
const
	MAXN = 10;
	MAXM = 16;
type
	int = longint;
var
	n, m : int;
	i, j : int;
	a : array [-1..MAXN + 2, -1..MAXN + 2] of int;
	//a : array [0..MAXN + 1, 0..MAXN + 1] of int;
	was : array [0..MAXN + 1, 0..MAXN + 1] of boolean;
	
	currentHeight, ansHeight, cnt : int;
	ans : array [1..MAXN, 1..MAXN] of int;
	
	numbers : array [0..5] of boolean;


function isFinalize(x, y : int) : boolean;
begin
	result := not((x > 1) and (a[x - 1][y] = 0) or 
				  (y > 1) and (a[x][y - 1] = 0) or 
				  (x < n) and (a[x + 1][y] = 0) or 
				  (y < m) and (a[x][y + 1] = 0));
end;

function isValid(x, y : int) : boolean;
var
	k : int;
begin
	result := true;	
	if (not was[x][y]) then 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;

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

procedure dfs(x, y : int);
var
	i, j : int;
begin
	if ((x = n + 1) and (y = 1)) then begin
		
		if (currentHeight > ansHeight) then begin
			ansHeight := currentHeight;
			for i := 1 to n do begin
				for j := 1 to m do begin
					ans[i][j] := a[i][j];
				end;
			end;
		end;

	end else if (was[x][y]) then begin

		currentHeight := currentHeight + a[x][y];
		if (y = m) then begin
			dfs(x + 1, 1);
		end else begin
			dfs(x, y + 1);
		end;
		currentHeight := currentHeight - a[x][y];

	end else begin
		for i := 1 to 5 do begin
			a[x][y] := i;
			currentHeight := currentHeight + i;

			if not (isFinalize(x - 1, y) and (not isValid(x - 1, y)) or
					isFinalize(x, y) and (not isValid(x, y)) or
					isFinalize(x, y - 1) and (not isValid(x, y - 1))) then begin
				if (y = m) then begin
					dfs(x + 1, 1);
				end else begin
					dfs(x, y + 1);
				end;
			end;

			currentHeight := currentHeight - i;
			a[x][y] := 0;
		end;
	end;
end;

procedure writeAnswer();
var
	i, j, k : int;
begin
	writeln(ansHeight);
	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;

	ansHeight := 0;
	currentHeight := 0;

	dfs(1, 1);
	writeAnswer();	
end.
