using System;
using System.Linq;

namespace Okruzno_takmicenje___2022_02_26
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string[] temp = Console.ReadLine().Split(' ');
            int n = int.Parse(temp[0]);
            int m = int.Parse(temp[1]);
            int[] a = new int[n];
            int[] b = new int[n];
            int[] c = new int[m];
            int[] d = new int[m];
            for (int i = 0; i < n; i++)
            {
                temp = Console.ReadLine().Split(' ');
                a[i] = int.Parse(temp[0]);
                b[i] = int.Parse(temp[1]);
            }
            for (int i = 0; i < m; i++)
            {
                temp = Console.ReadLine().Split(' ');
                c[i] = int.Parse(temp[0]);
                d[i] = int.Parse(temp[1]);
            }
            int[,] polje = new int[n, m];
            int c1 = 0;
            int c2 = 0;
            bool flag = true;
            while (flag)
            {
                flag = false;
                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < m; j++)
                    {
                        if (polje[i, j] < b[i] && polje[i, j] < d[j])
                        {
                            polje[i, j]++;
                            b[i]--;
                            d[i]--;
                            c1++;
                            flag = true;
                        }
                    }
                }
            }
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    if (polje[i, j] > 0) c2++;
                }
            }
            Console.WriteLine();
            Console.WriteLine(c1);
            Console.WriteLine(c2);
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    Console.Write(polje[i, j] + " ");
                }
                Console.WriteLine();
            }
        }
    }
}