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

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

public class bloxx_mb_slow {
    private static void myAssert(String msg, boolean exp) {
        if (!exp) {
            throw new AssertionError(msg);
        }
    }

    static int rows;
    static int cols;
    static int[][] pos;
    static int[][] best;
    static int bestSum = -1;

    static final int[] dx = {1, 0, -1, 0};
    static final int[] dy = {0, 1, 0, -1};

    static final int[][] freeRow = new int[7][20];
    static final int[][] freeCol = new int[7][20];
    static final int[] freeCnt = new int[7];

    static void go(int fill, int idx, int sum) {
        if (fill == 6) {
            if (sum > bestSum) {
                bestSum = sum;
                for (int i = 1; i <= rows; ++i) {
                    System.arraycopy(pos[i], 0, best[i], 0, cols + 2);
                }
            }
        } else if (idx == freeCnt[fill]) {
            go(fill + 1, 0, sum);
        } else {
            if (sum + 5 * (freeCnt[fill] - idx + freeCnt[fill + 1]) <= bestSum) {
                return;
            }

            int row = freeRow[fill][idx];
            int col = freeCol[fill][idx];
            if (pos[row][col] != 0) {
                throw new AssertionError();
            }

            if (fill < 5) {
                freeRow[fill + 1][freeCnt[fill + 1]] = row;
                freeCol[fill + 1][freeCnt[fill + 1]] = col;
                ++freeCnt[fill + 1];
                go(fill, idx + 1, sum);
                --freeCnt[fill + 1];
            }

            int used = 1;
            for (int d = 0; d < 4; ++d) {
                used |= 1 << pos[row + dx[d]][col + dy[d]];
            }
            int mask = (1 << fill) - 1;
            if ((used & mask) == mask) {
                pos[row][col] = fill;
                go(fill, idx + 1, sum + fill);
                pos[row][col] = 0;
            }
        }
    }

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new FileReader("bloxx.in"));
        {
            StringTokenizer st = new StringTokenizer(in.readLine());
            rows = Integer.parseInt(st.nextToken());
            cols = Integer.parseInt(st.nextToken());
            myAssert("row count is out of bounds", 1 <= rows && rows <= 10);
            myAssert("column count is out of bounds", 1 <= cols && cols <= 10);
            myAssert("square is out of bounds", rows * cols <= 16);
        }

        pos = new int[rows + 2][cols + 2];
        best = new int[rows + 2][cols + 2];
        for (int i = 0; i < rows; ++i) {
            StringTokenizer st = new StringTokenizer(in.readLine());
            for (int j = 0; j < cols; ++j) {
                int v = Integer.parseInt(st.nextToken());
                myAssert("intial height is out of bounds", 0 <= v && v <= 5);
                pos[i + 1][j + 1] = v;
            }
        }

        freeCnt[1] = 0;
        int initSum = 0;
        for (int i = 1; i <= rows; ++i) {
            for (int j = 1; j <= cols; ++j) {
                if (pos[i][j] == 0) {
                    freeRow[1][freeCnt[1]] = i;
                    freeCol[1][freeCnt[1]] = j;
                    ++freeCnt[1];
                } else {
                    initSum += pos[i][j];
                }
            }
        }

        go(1, 0, initSum);

        PrintWriter out = new PrintWriter("bloxx.out");
        out.println(bestSum);
        List<Integer> aRow = new ArrayList<Integer>();
        List<Integer> aCol = new ArrayList<Integer>();
        List<Integer> aHei = new ArrayList<Integer>();
        for (int h = 1; h <= 5; ++h) {
            for (int row = 1; row <= rows; ++row) {
                for (int col = 1; col <= cols; ++col) {
                    if (best[row][col] == h && pos[row][col] == 0) {
                        aRow.add(row);
                        aCol.add(col);
                        aHei.add(best[row][col]);
                    }
                }
            }
        }
        out.println(aRow.size());
        for (int i = 0; i < aRow.size(); ++i) {
            out.println(aRow.get(i) + " " + aCol.get(i) + " " + aHei.get(i));
        }
        out.close();
    }
}
